Just inject all dependencies to class you are testing (SUT). You can use constructor or property injection. I would go with constructor injection:
public class ProductController
{
private IProductService productService;
private ICategoryService categoryService;
public ProductController(IProductService productService,
ICategoryService categoryService)
{
this.productService = productService;
this.categoryService = categoryService;
}
public ActionResult GetProductDetails()
{
var categories = categoryService.GetAllCategories().ToList();
// ...
}
}
In your tests you can use setup method (NUnit syntax) to create and pass mocked dependencies to SUT:
private ProductController controller;
private Mock<IProductService> productServiceMock;
private Mock<ICategoryService> categoryServiceMock;
[SetUp]
public void Setup()
{
productServiceMock = new Mock<IProductService>();
categoryServiceMock = new Mock<ICategoryService>();
controller = new ProductController(productServiceMock.Object,
categoryServiceMock.Object);
}
Then you can arrange any of this mocked object in test methods, just before exercising SUT:
[Test]
public void ShouldReturnProductDetails()
{
List<CategoryDto> categories = // ...
categoryServiceMock.Setup(cs => cs.GetAllCategories()).Returns(categories);
var result = controller.GetProductDetails();
// Assert
}