Description of the problem
A method to be tested creates objects of some type, let's call it Item
. It initialises these objects' properties with data passed into it.
This method calls another method, which takes an Item
as a parameter. How can it be made sure that this call is made correctly? That is, the following would need to be verified:
- The object being passed into the method is of the correct type.
- The object being passed into the method has correct property values.
- The other method is being called the correct number of times.
Assume we are using NUnit and FakeItEasy.
Code
public class Controller
{
IService service {get; set;}
// dependency injection, can use fakes
public Controller(IService service) {
this.service = service;
}
public void methodToTest(string itemName, string itemDescription, int quantity)
{
Item newItem = new Item
{
name = name,
description = description
}
for (int i = 1; i <= quantity; i++)
{
// need to verify that doSomething() is called with a parameter of type Item
// and that it is called quantity times
// and that newItem has correct parameters
service.doSomething(newItem);
}
}
}