I'm playing with unit-testing in Ruby. There is a situation that I don't know if is good enough or if I am doing the wrong abstraction.
I have two classes: Transaction
and Account
. An Account
could have 0..* Transaction
s. The only constraint is shall not have duplicated transactions for an Account
.
it "add a duplicated transaction will fails" do
transaction = double(:transaction, id: '124-acd-345')
expect(transaction).to receive(:==).and_return true
subject.add_transaction transaction
subject.add_transaction transaction.clone
expect(subject.transactions.count).to eq 1
end
Account#add_transaction
piece:
def add_transaction(transaction)
@transactions << transaction if transactions.select { |t| t == transaction }.empty?
end
Transaction#==
piece:
def ==(other)
other.id == self.id
end
The point is: Transaction#==
is already unit tested in their test class. I know that transaction ==
works properly. So, my approach in Account#add_transaction
was Test Behavior (with that expect(transaction)
...) and Test Value, checking if there is only one transaction into object.
IMO, I didn't like this solution, but I can't find anything better to do. Tips?