Using implicit `subject` with `expect` in RSpec-2.11
Asked Answered
C

3

42

With the new expect syntax in rspec-2.11, how is it possible to use the implicit subject? Is there a better way than explicitly referencing subject, like below?

describe User do
  it 'is valid' do
    expect(subject).to be_valid    # <<< can `subject` be implicit?
  end
end
Curvy answered 4/9, 2012 at 9:30 Comment(0)
A
64

If you configure RSpec to disable the should syntax, you can still use the old one-liner syntax, since that doesn't involve should being added to every object:

describe User do
  it { should be_valid }
end

We briefly discussed an alternate one-liner syntax, but decided against it since it wasn't needed and we felt like it might add confusion. You can, however, easily add this yourself if you prefer how 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

With this in place, you could write this as:

describe User do
  expect_it { to be_valid }
end
Abba answered 4/9, 2012 at 14:55 Comment(5)
As of RSpec 2.13.0 the module RSpec::Core::Subject::ExampleMethods no longer exists. Instead use RSpec::Core::MemoizedHelpersBohunk
@Myron I just asked a similar question though I was explicitly concerned with one-line blocks. I'm just curious: is there any talk of eventually deprecating the old one-liner syntax or requiring any additional configuration (aside from what you mention above) to use it?Oceania
There is no old one-liner syntax. The "old" one-liner syntax is still the current one-liner syntax and we have no plans to deprecate it.Abba
I read the brief discussion about expect_it that you guys had, but I didn't see any real objection to it as a complement to what you ended up with except for the fear that you'd get a lot of questions about why expect {...} doesn't work. I hope you'll reconsider it as a standard feature of an upcoming release. I think the presence of "it" in expect_it should be enough to avoid confusion (i.e. to get folks to think of it as an it alternative rather than an expect alternative).Effect
RSpec 2.99 3.0.0.beta2 introduce is_expected. You can use it like this: it { is_expected.to be_valid }. is_expected is substitude for expect(subject).Galton
S
17

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
Slosberg answered 20/12, 2013 at 19:53 Comment(1)
This question was for Rspec 2.11, not Rspec 3 (which is still Beta as of this post)Kalmar
C
12

One could use the new named subject syntax, although it's not implicit.

describe User do
  subject(:author) { User.new }

  it 'is valid' do
    expect(author).to be_valid
  end
end
Curvy answered 4/9, 2012 at 9:30 Comment(2)
You could define the subject as :it and then use expect(it).to be_validAmass
This is inelegant, though, since you're using three lines to express what could be expressed in one line with the old one-liner should syntax, or with custom configuration as explained by Myron above.Oceania

© 2022 - 2024 — McMap. All rights reserved.