I have a piece of logic which is inserting a Product Model into a repository. The piece of logic will be inserting two product models both with different data into the repository and I need to test that this method is only called twice, once for each product.
public interface IProductRepo
{
void AddProduct(IProductModel productModel);
}
public class ProductFactory
{
IProductRepo productRepository;
public ProductFactory(IProductRepo productRepo)
{
this.productRepository = productRepo;
}
public void AddProduct(IProductModel productModel)
{
this.productRepository.AddProduct(productModel);
}
}
[TestMethod]
public void test()
{
IProductModel productOne = Mock.Create<IProductModel>();
Mock.Arrange(() => productOne.Name).Returns("ProductOne");
Mock.Arrange(() => productOne.Price).Returns(99);
IProductModel productTwo = Mock.Create<IProductModel>();
Mock.Arrange(() => productTwo.Name).Returns("ProductTwo");
Mock.Arrange(() => productTwo.Price).Returns(10);
IProductRepo productRepo = Mock.Create<IProductRepo>();
ProductFactory factory = new ProductFactory(productRepo);
factory.AddProduct(productOne);
factory.AddProduct(productTwo);
// Test to see that both of these products being added called the IProductRepo.AddProduct() method.
}
UPDATE
based on comments, then you can call the assert twice. Once for each model.
The assert would compare the args provided for equality when making the assertion.
Original answer
Referencing Asserting Occurrence from documentation,
consider using...
in the minimal example provided above,