For example I want to test the fact that my multi-threaded method is calling the repository methods n times if I give him n chunks of data from different threads. Of course mocks are not thread safe and even are not supposed to be.
[Test]
public void CanSaveCustomersInParallel()
{
var customers = new List<List<Customer>>
{
new List<Customer>
{
new Customer {FirstName = "FirstName1"},
new Customer {FirstName = "FirstName2"}
},
new List<Customer>
{
new Customer {FirstName = "FirstName3"},
new Customer {FirstName = "FirstName4"}
}
};
_serviceCustomers.ParallelSaveBatch(customers);
_repoCustomers
.Verify(x => x.SaveBatch(It.IsAny<List<Customer>>()), Times.Exactly(2));
}
Of course, this test fails sometimes and sometimes it does not. But it is incorrect in its essence. Can you advise me how to re-write it?
Well, the next stub did the trick:
And the unit test looks the next way: