1
\$\begingroup\$

How can I verify that ohterClassMock.MethodToTest() was called with contracts?

That it was called with contract, that have Id = 1 and Param2 = 34 and also with contract2, which has Id = 2 and Param2 = 56.

This is my code:

GoodClass goodClass = new GoodClass(ohterClassMock);
...
var contracts = new List<Contract>();
var contract = new Contract{ Id = 1, Param2 = 34 };
contracts.Add(contract);
var contract2 = new Contract { Id = 2, Param2 = 56 };
contracts.Add(contract2);

goodClass.DoSomething(contracts);

ohterClassMock.Verify(mock => mock.MethodToTest(It.IsAny<contracts>()));

I know that it can be tested for every item in the collection:

ohterClassMock.Verify(mock => mock.MethodToTest(It.Is<Contract>(contract => contract.Id == 1)));
...

but maybe there is some other syntax to call it in one line of code?

Updated:

goodClass.DoSomething(contracts);

calls

foreach(Contract contract in contracts)
{
   ohterClassMock.MethodToTest(contract);
}
\$\endgroup\$

migrated from stackoverflow.com Mar 15 '13 at 10:42

This question came from our site for professional and enthusiast programmers.

  • 2
    \$\begingroup\$ Not very clear. What do you want to test exactly? That the contracts were not modified? \$\endgroup\$ – Henk Holterman Mar 15 '13 at 10:22
  • \$\begingroup\$ @HenkHolterman - I want to test, that ohterClassMock was called with contracts. And to test that all the items of contracts have the right value. \$\endgroup\$ – MikroDel Mar 15 '13 at 10:24
  • \$\begingroup\$ So you want to test the Test? The call is right there. You should be wondering about the results of DoSomething(). \$\endgroup\$ – Henk Holterman Mar 15 '13 at 10:27
  • \$\begingroup\$ @HenkHolterman - I want to test, that if I call goodClass.DoSomething(contracts) - ohterClassMock.MethodToTest(contract) will be called with all the items from contracts Collection and that all this item have the valid parameters \$\endgroup\$ – MikroDel Mar 15 '13 at 10:36
  • 2
    \$\begingroup\$ You shouldn't test that a function calls another function correctly. You should test that A) function #1 returns or manipulates the contracts correctly, without knowing anything about how it does it, and B) that the second function does the right thing when given a correctly manipulated set of input. \$\endgroup\$ – Bobson Mar 15 '13 at 15:21
2
\$\begingroup\$

Strictly to answer the question at hand, you have local variables for the individual contracts already, so you can just re-use them:

ohterClassMock.Verify(mock => mock.MethodToTest(It.Is<Contract>(c => contract.Equals(c))));
ohterClassMock.Verify(mock => mock.MethodToTest(It.Is<Contract>(c => contract2.Equals(c))));

This assumes you have implemented IEquatable<Contract> and/or overridden Object.Equals on your Contract object.

Given the behavior of most test and mocking frameworks, it will probably save you a lot of grief to go ahead and override Object.ToString so that failed tests will print out nicer expected/actual values than the fully-qualified type names.

Also, as an aside, you can create your list with a collection initializer if you do so after building your individual contracts:

var contracts = new List<Contract> { contract, contract2 };

Or, if your method takes in IEnumerable<Contract>, it may be even simpler to use:

var contracts = new [] { contract, contract2 };
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.