5

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

这是例外

Exception encountered: #<NoMethodError: undefined method `expect' for #<Class:0x007fa5bd8e4120>>

这是方法:

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

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

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

我在跑步:

Rails 3 上的 RSpec 2.1.0,带有 guard-spork 0.3.2 和 spork 0.9.0

4

1 回答 1

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
于 2012-07-06T21:25:06.857 回答