【问题标题】:Rails Tutorial mass assignment security exceptionRails 教程批量分配安全异常
【发布时间】:2012-08-02 14:58:39
【问题描述】:

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

rspec:

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

【问题讨论】:

    标签: ruby-on-rails rspec mass-assignment


    【解决方案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
    

    【讨论】:

    • 它有效!我最好向 Rails 教程的制作者提交错误报告
    猜你喜欢
    • 1970-01-01
    • 2014-02-17
    • 1970-01-01
    • 2016-01-09
    • 2011-03-29
    • 2014-01-31
    • 2013-07-26
    • 2012-08-17
    • 1970-01-01
    相关资源
    最近更新 更多