Question

I'm seeing different behavior between the two expect syntax formats in Rspec:

expect(subject).to ...

versus

it { is_expected.to ... }

This works...

describe ApplicationController
  controller do
    def index() render nothing: true end
  end

  before { get :index }

  describe "success flash message" do
    subject { flash[:success] }
    it "should be nil" do
      expect(subject).to be_nil
    end
  end
end

However this does not...

describe ApplicationController
  controller do
    def index() render nothing: true end
  end

  before { get :index }

  describe "success flash message" do
    subject { flash[:success] }
    it { is_expected.to be_nil }
  end
end

The error I am getting:

1) ApplicationController handling flash messages
   Failure/Error: it { is_expected.to be_nil }
   NameError:
     undefined local variable or method `is_expected' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x00>
   # ./spec/some/file.rb:123:in `block (3 levels) in <top (required)>'

What gives?

  • rails (4.0.2)
  • rspec-rails (2.14.1)
Was it helpful?

Solution

is_expected will be introduced in RSpec 3.0.0.beta2. It's only available from github master at this point.

OTHER TIPS

I suggest you do this:

subject { flash }
its([:success]) { should be_nil }

its the current recommended way

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top