如何测试质量分配错误

时间:2012-06-21 03:53:32

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

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

如何测试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>>

1 个答案:

答案 0 :(得分:0)

Gah,不得不把它放在它里面{}:

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

这解决了我的问题