I am doing unit testing of one of my Service layer class(ProductService).That layer serves DI facility to one of controller class(ProductController).And for ProductController unit testing, there is already a unit test class ProductControllerTest.

But for unit test ProductController's service layer(ProductService),i have created one new unit test class(ProductServiceTest).

But i don't know whether i should create a new unit test class to do Service Layer unit testing or not.

Also i want to know the concept of DI(Dependency Injection) and Mocking(MOQ object) in unit testing.Means where to use MOQ in unit testing and when it's necessary to use ?

I have written unit test code for one of the action method of ProductService .And i want to know whether it's correct or not ?

Below is the piece of code of Action Method :-

public class ProductService : IProductService
{
    private readonly IProductDal<Product> _productDal;
    private readonly IUserDal<Users> _userDal;

    public ProductService(IProductDal<Product> ProductDAL)
    {
        _productDal = ProductDAL;
    }


    public List<ProductDto> GetProducts(string SearchInName, string SearchInDescription)
    {
          List<ProductDto> productList = new List<ProductDto>();
          foreach (Product product in _productDal.GetProducts(SearchInName, SearchInDescription))
          {
               productList.Add(Adapter.AdaptOmToDto(product));
          }
          return productList;
    }
}

Below is the piece of code of Unit Testing(ProductServiceTest) class :-

    [TestMethod]
    public void GetProductsTest()
    {
        var productList = new List<ProductDto>();

        // Arrrange            
        var Name = "CreateSave";
        var Description = "CreateSave";

        // Act
        List<ProductDto> output = _productService.GetProducts(Name, Description);

        // Assert       
        Assert.IsNotNull(output);
        Assert.IsInstanceOfType(output, typeof(List<ProductDto>));
    }

Can any one suggest me the best way to unit test such kind of methods ?

有帮助吗?

解决方案

Also i want to know the concept of DI(Dependency Injection) and Mocking(MOQ object) in unit testing

Dependency Injection is the way we make our application adhere to Dependency Inversion Principle, which states that high-level modules should not depend on low-level modules - both should depend on abstractions. Depending on abstractions gives you low-coupling of units in system and ability to give any implementation of abstract dependency to dependent class. And here comes part related to unit-testing - you can give mocked implementation of dependency, which will allow you to verify behavior (see Mocks Aren't Stubs article) of unit under test.

You are already did a good job by making your ProductService depend on abstractions, and you used DI to inject abstraction implementation to service. What you need to test behavior of service is injecting mocked implementation of abstraction, and setting up expected interaction between ProductService and mock:

ProductService _productService;
Mock<IProductDal<Product>> _productDalMock;

[TestInitialize]
public void Setup()
{
    // create mock of dependency and pass mocked object to service
    _productDalMock = new Mock<IProductDal<Product>>();
    _productService = new ProductService(productDalMock.Object);
}

[TestMethod]
public void ShouldNotReturnDtosWhenProductsNotFound()
{
    // Arrrange 
    var name = "CreateSave";
    var description = "CreateSave";
    // setup mocked dal to return empty list of products
    // when name and description passed to GetProducts method
    _productDalMock.Setup(d => d.GetProducts(name, description))
                   .Returns(new List<Product>());

    // Act
    List<ProductDto> actual = _productService.GetProducts(name, description);

    // Assert
    Assert.False(actual.Any());
    // verify all setups of mocked dal were called by service
    _productDalMock.VerifyAll();
}

As you can see in test above, you can check whether product service called its dependency with correct parameters, and you can setup what data should dependency return to service. For next test you can setup mock to return list of predefined products. Then you will verify if appropriate dtos were returned by service.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top