我有一个名为rfid的只读属性的产品
class Product < ActiveRecord::Base
attr_readonly :rfid我有下面的规范来检查它是否是只读的
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但它失败并返回:
1) Product 's rfid is immutable'
Failure/Error: @product.update_attribute(:rfid, 1921)
ActiveRecord::ActiveRecordError:
rfid is marked as readonly我试过用"has already been taken"、expect(@product.rfid).to eq(1921)、to have(1).errors等方法,但都没有用。
像往常一样,任何帮助都非常感谢。
发布于 2015-02-13 00:25:27
尝试更新只读属性将引发ActiveRecord::ActiveRecordError erorr,而不是向模型添加验证错误。您可以通过以下方法测试是否引发错误:
expect {
@product.update_attribute(:rfid, 1921)
}.to raise_error(ActiveRecord::ActiveRecordError, 'rfid is marked as readonly')https://stackoverflow.com/questions/28489688
复制相似问题