我正在创建一个活动的记录对象,我想测试一下我是否正确。
这是我的测试代码:
describe '#transaction process' do
let(:penalty_purchase) { penalty.penalty_purchase }
describe '#transaction' do
it 'a transaction between the seller and the admin, includes the penalty purchase and the sellers sale' do
penalty.purchase= penalty_purchase
args = {sale: sale, purchase: penalty_purchase}
wanted = penalty.transaction_factory.new(args).transaction
expect(penalty.transaction).to eql wanted
end
end
end
继承我测试的代码:
def penalty_transaction
args = {sale: sale, purchase: purchase}
@transaction = transaction_factory.new(args).transaction
end
上面的交易工厂对象包含2个其他活动记录对象,即销售和购买。
出于某种原因,测试失败了:
1) LockedSalePenalty purchase process #transaction process #transaction a transaction between the seller and the admin, includes the penalty purchase and the sellers sale
Failure/Error: expect(penalty.transaction).to eql wanted
expected: #<Transaction id: nil, purchase_id: nil, sale_id: 1, created_at: "2014-08-03 00:23:45", updated_at: nil, seller_id: 1, buyer_id: 2, amount: 20.0, reduced_asset_id: nil>
got: #<Transaction id: nil, purchase_id: nil, sale_id: 1, created_at: "2014-08-03 00:23:45", updated_at: nil, seller_id: 1, buyer_id: 2, amount: 20.0, reduced_asset_id: nil>
(compared using eql?)
我知道对于您创建的对象,您需要为您创建的对象实现自己的相等方法,因此在我的活动记录事务类中:
def eql?(obj)
self.class == obj.class &&
self.attributes == obj.attributes
end
当我比较penalty.transaction和wanted的属性时,它们并不相等,即使它们看起来完全相同。有什么想法吗?
编辑:
问题是创建的at属性有少量不同。