ruby on rails - Rspec Test validations of 'on: : update' not working (Rails4/Rspec 3) -
i have model implemented validate prevent change after initial value has been set (during object creation).
it works way:
models/deal.rb
validate :quantity_not_changeable, on: :update def quantity_not_changeable if quantity_changed? errors.add(:quantity, "change of defined qty not possible") end end
this works in app: created new deal, works. try edit changing 'quantity' field, fails. try edit field (apart 'quantity'), works. working.
but rspec test test fails.
actually i know not work because there problem on: : validate.
indeed without 'on: : validate', test passes, of course can't remove 'on: :update', want user not able edit after initial creation of deal).
describe deal let(:admin_user) { factorygirl.create(:admin_user) } before(:each) @attr = { title: "neque porro quisquam est qui dolorem", description: "lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum", quantity: 10 } end describe "my test" describe "test nb of quantity can't changed after initial creation" "fails if initial quantity changed" deal = deal.create(@attr) deal.update(quantity: 15) expect(deal).to have(1).errors_on(:quantity) end end
but rspec error:
failure/error: expect(deal).to have(1).errors_on(:quantity) expected 1 errors on :quantity, got 0
i going use below "print variable": p deal show why sure due on: :update not working in tests.
first: if place 'p deal' before updating it, let's see value of deal object:
describe "my test" describe "test nb of quantity can't changed after initial creation" "fails if initial quantity changed" deal = deal.create(@attr) p deal deal.update(quantity: 15) expect(deal).to have(1).errors_on(:quantity) end end
i following output (we see normal: quantity indeed 10)
#<title: "neque porro quisquam est qui dolorem", description: "lorem ipsum lorem ipsum lorem ipsum lorem ipsum lo...", quantity: 10>
second: let's move 'p deal' after attempt in test update value of 'quantity'
describe "my test" describe "test nb of quantity can't changed after initial creation" "fails if initial quantity changed" deal = deal.create(@attr) deal.update(quantity: 15) p deal expect(deal).to have(1).errors_on(:quantity) end end
now output is:(we see contrary expect due validate on: :update in code, rspec test manage unfortunately update value of quantity):
#<title: "neque porro quisquam est qui dolorem", description: "lorem ipsum lorem ipsum lorem ipsum lorem ipsum lo...", quantity: 15>
so don't understand why in reality, tested manually , can not update, in rspec test suite update!!!
hours understand no success...i tried find online resources , on: :update weak way things. don't know weird bug.
please help.
i guess entry isn't saved when called create
method because of other validation. can try open console rails c
, evaluate following:
deal = deal.create(title: 'mytitle', description: 'mydescription', quantity: 10) deal.persisted?
then if got false in order find out errors when creating:
deal.errors.messages
edit
this test should passes:
it "fails if initial quantity changed" deal = deal.create(@attr) deal.update(quantity: 26) expect(deal.errors.messages).to eq({quantity: ["change of defined qty not possible"]}) end
but these should not:
it "exploited test should not pass because messages hash should not empty" deal = deal.create(@attr) deal.update(quantity: 26) expect(deal.errors.messages.empty?).to eq(true) end "exploited test in other variation" deal = deal.create(@attr) deal.update(quantity: 26) expect(deal.errors.messages).to eq({}) end
Comments
Post a Comment