Tyler
Tyler

Reputation: 11509

How to test for mass assignment error

Seems I want to do the opposite of what most people ask for when testing rails...

How to test FOR ActiveModel::MassAssignmentSecurity::Error. I get this already, just want to know how to make it make my test case green and not red.

current test:

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

How should I structure this as a proper test? I have tried:

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

But that doesn't work for me either, I get

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

Upvotes: 0

Views: 88

Answers (1)

Tyler
Tyler

Reputation: 11509

Gah, had to put it inside an it {}:

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

This solved the problem for me

Upvotes: 0

Related Questions