【问题标题】:How to test for mass assignment error如何测试质量分配错误
【发布时间】:2012-06-21 03:53:31
【问题描述】:

似乎我想做与大多数人在测试 Rails 时要求的相反...

如何测试 FOR ActiveModel::MassAssignmentSecurity::Error。我已经知道了,只是想知道如何使它使我的测试用例变成绿色而不是红色。

当前测试:

describe Tag do
  let(:tag) { FactoryGirl.create(:tag) }
  subject { tag }

  describe "when tag name is sent directly" do
    before { tag = Tag.new(name: "New") } # <- what I want to raise the error
    it { should_not be_valid } # <- doesn't make it to this line
  end
end

我应该如何将其构建为适当的测试?我试过了:

    lambda do
      tag = Tag.new(name: "NEW")
    end.should raise_error

但这对我也不起作用,我明白了

Exception encountered: #<NoMethodError: undefined method `raise_error' for #<Class:0x00000102525c08>>

【问题讨论】:

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


    【解决方案1】:

    啊,必须把它放在一个它{}中:

    it do
      lambda { tag = Tag.new(name: "NEW") }.should raise_error
    end
    

    这解决了我的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      相关资源
      最近更新 更多