Reputation: 4658
I have a product that has a readonly attribute called rfid
class Product < ActiveRecord::Base
attr_readonly :rfid
I have the following spec to check that it is, indeed, readonly
describe Product do
before :each do
@product = FactoryGirl.create(:product)
end
it "'s rfid is immutable'" do
@product.update_attribute(:rfid, 1921)
@product.valid?
expect(@product.errors[:rfid]).to include("marked as readonly")
end
But it fails and returns:
1) Product 's rfid is immutable'
Failure/Error: @product.update_attribute(:rfid, 1921)
ActiveRecord::ActiveRecordError:
rfid is marked as readonly
I tried with
"has already been taken"
and expect(@product.rfid).to eq(1921)
and to have(1).errors
etc. to no avail
As usual, any help greatly appreciated
Upvotes: 0
Views: 1185
Reputation: 24337
Trying to update a read only attribute will raise an ActiveRecord::ActiveRecordError
erorr, rather than add a validation error to the model. You can test that the error is raised with:
expect {
@product.update_attribute(:rfid, 1921)
}.to raise_error(ActiveRecord::ActiveRecordError, 'rfid is marked as readonly')
Upvotes: 3