在rspec异常处理之后,为'expect'获取'NoMethodError'

ova*_*g25 5 rspec ruby-on-rails-3

对于为什么这个RSpec方法不起作用感到困惑.我在这里看到答案:如何使用RSpec的should_raise与任何类型的异常?并尝试了所有提出的组合但由于某种原因,我仍然得到一个NoMethodError.

这是例外

Exception encountered: #<NoMethodError: undefined method `expect' for #<Class:0x007fa5bd8e4120>>
Run Code Online (Sandbox Code Playgroud)

这是方法:

describe "admin should not be accesible" do
expect { User.new(name: "Example Name", email: "email@example.org", password:    "foobar", password_confirmation: "foobar", admin: "true") }.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
Run Code Online (Sandbox Code Playgroud)

我之前得到了这个错误,所以我知道我的方法正在做我想做的事情:

1) User admin should not be accesible
Failure/Error: hey = User.new(name: "Hello", email: "hey@hey.org", password: "foobar", password_confirmation: "foobar", admin: "true")
 ActiveModel::MassAssignmentSecurity::Error:
   Can't mass-assign protected attributes: admin
Run Code Online (Sandbox Code Playgroud)

我在跑步:

Rails 3上的RSpec 2.1.0,防护装置0.3.2和spork 0.9.0

pho*_*oet 13

这是经典之作!你错过了这个it街区!

describe "admin should not be accesible" do
  it "should bla" do
    expect { User.new(name: "Example Name", email: "email@example.org", password:    "foobar", password_confirmation: "foobar", admin: "true") }.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
  end
end
Run Code Online (Sandbox Code Playgroud)

  • 在这个例子中,'expect {} .should`已被弃用,请使用`expect {} .to` (6认同)