使用rspec'expect'速记的意外行为

时间:2014-02-10 22:00:36

标签: ruby-on-rails rspec ruby-on-rails-4 rspec-rails

我在Rspec中看到两种expect语法格式之间的不同行为:

  

expect(subject).to ...

     

     

it { is_expected.to ... }

这有效......

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

然而,这不是......

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

我得到的错误:

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)>'

是什么给出了?

  • rails(4.0.2)
  • rspec-rails(2.14.1)

2 个答案:

答案 0 :(得分:3)

is_expected将在RSpec 3.0.0.beta2中引入。它只能从github master那里获得。

答案 1 :(得分:1)

我建议你这样做:

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

its the current recommended way