Inspired by jklina's answer/comment, I found out that using update_attribute
helps in this case, and I can update the field without triggering the before validation callbacks.
So the test turns into:
require 'spec_helper'
describe Ticket do
before do
@ticket=Ticket.create
end
subject{@ticket}
it{ should respond_to(:code)}
...
describe ", duplicate codes" do
before do
dup_ticket=@ticket.dup
dup_ticket.update_attribute(:code, @ticket.code)
dup_ticket.update_attribute(:sec_code, @ticket.sec_code)
dup_ticket.update_attribute(:series, @ticket.series)
end
it{ should_not be_valid}
end
end
Hence reflecting the same value for code
, sec_code
, and series
at the ActiveRecord table. And the test does what it's expected it to.