1

我一直在学习 Ruby on Rails 教程。我遇到了一个问题,让测试通过检查要抛出的批量分配安全异常。我不确定为什么我会遇到这个测试失败,或者如何解决它。

规格:

describe "accessible attributes" do
    it "should not allow access to user_id" do
      expect do
        Micropost.new(user_id: user.id)
      end.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
    end    
  end

失败:

  1) Micropost accessible attributes should not allow access to user_id
     Failure/Error: expect { Micropost.new(user_id: user.id) }.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
       expected ActiveModel::MassAssignmentSecurity::Error, got #<NoMethodError: undefined method `call' for #<RSpec::Expectations::ExpectationTarget:0x8af2bb8>>
     # ./spec/models/micropost_spec.rb:23:in `block (3 levels) in <top (required)>
4

1 回答 1

1

尝试使用to而不是should您期望的 raise_error 匹配器。

describe "accessible attributes" do
  it "should not allow access to user_id" do
    expect do
      Micropost.new(user_id: user.id)
    end.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
  end    
end
于 2012-08-02T15:21:25.583 回答