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
Hosam Aly
  • 41,555
  • 36
  • 141
  • 182

3 Answers3

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
Myron Marston
  • 21,452
  • 5
  • 64
  • 63
  • 2
    As of RSpec 2.13.0 the module `RSpec::Core::Subject::ExampleMethods` no longer exists. Instead use `RSpec::Core::MemoizedHelpers` – Phil Ostler Feb 25 '13 at 22:59
  • @Myron I just asked a similar [question](http://stackoverflow.com/questions/16682147/rspec-expect-syntax-and-idiomatic-attribute-spec/16682568?noredirect=1#16682568) 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? – aceofbassgreg May 22 '13 at 02:42
  • 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. – Myron Marston May 22 '13 at 18:51
  • 2
    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). – Peter Alfvin Sep 10 '13 at 14:46
  • 1
    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). – Sebastian Apr 15 '14 at 09:38
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
Javid Jamae
  • 8,741
  • 4
  • 47
  • 62
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
Hosam Aly
  • 41,555
  • 36
  • 141
  • 182
  • 6
    You could define the subject as `:it` and then use `expect(it).to be_valid` – Jamon Holmgren Apr 30 '13 at 17:25
  • 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. – aceofbassgreg May 22 '13 at 02:45