Using implicit `subject` with` expect` in RSpec-2.11

With the new expect syntax in rspec-2.11, how can I use an implicit subject ? Is there a better way than explicitly referring to subject as shown below?

 describe User do it 'is valid' do expect(subject).to be_valid # <<< can `subject` be implicit? end end 
+42
syntax ruby rspec rspec2
Sep 04 '12 at 9:30
source share
3 answers

If you configure RSpec to not allow syntax, you can still use the old syntax with one layer, since it should not be added for each object:

 describe User do it { should be_valid } end 

We briefly discussed the alternative syntax with a single layer, but decided against it, because it is not needed, and we felt that this could add confusion. However, you can easily add this if you prefer the way it reads:

 RSpec.configure do |c| c.alias_example_to :expect_it end RSpec::Core::MemoizedHelpers.module_eval do alias to should alias to_not should_not end 

In doing so, you can write this as:

 describe User do expect_it { to be_valid } end 
+64
Sep 04
source share

With Rspec 3.0, you can use is_expected , as described here .

 describe Array do describe "when first created" do # Rather than: # it "should be empty" do # subject.should be_empty # end it { should be_empty } # or it { is_expected.to be_empty } end end 
+17
Dec 20 '13 at 19:53
source share

You can use the new syntax for the named object, although it is not implicit.

 describe User do subject(:author) { User.new } it 'is valid' do expect(author).to be_valid end end 
+12
Sep 04 '12 at 9:30
source share



All Articles