186
179

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RSpec の should/stub から expect/allow の早見表

Last updated at Posted at 2013-08-31

RSpec 2.14.0 からは allow, expect_any_instance_of, allow_any_instance_of も使えるようになりました。

should_to_expect.rb
obj.should ...
expect(obj).to ...
 
obj.should_not ...
expect(obj).not_to ...
 
obj.should =~ //
expect(obj).to match(//)

[1, 2, 3].should =~ [3, 2, 1]
expect([1, 2, 3]).to match_array([3, 2, 1])

obj.should > 3
expect(obj).to be > 3
 
lambda { ... }.should raise_error
expect { ... }.to raise_error
 
# RSpec 2.14.0 or later
 
obj.stub(:foo)
allow(obj).to receive(:foo)
 
SomeClass.any_instance.should_receive(:foo)
expect_any_instance_of(SomeClass).to receive(:foo)
 
SomeClass.any_instance.stub(:foo)
allow_any_instance_of(SomeClass).to receive(:foo)

# RSpec 2.99.0.beta1 or later

it { should eq(something) }
it { is_expected.to eq(something) }

# RSpec 3.0.0.beta1 or later

obj.stub_chain(:foo, :bar).and_return(something)
allow(obj).to receive_message_chain(:foo, :bar).and_return(something)
186
179
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
186
179

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?