0

似乎我想做与大多数人在测试 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>>
4

1 回答 1

0

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

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

这为我解决了问题

于 2012-06-21T04:15:46.653 回答