简体   繁体   中英

Unit testing controller error. When I try to use the controller in the unit test, I get this error

When I try to use the controller in the unit testing, I get this error. I am using MS Testing for the unit tests.

Testing code

[TestMethod]
public async Task PostDataReturnOk()
{
        var data = _fixture.Create<ProductCat>();
        _categoryRepository.Setup(re => re.AddCat(It.IsAny<ProductCat>())).Returns(data);
        _Controller = ProductCatsController(_Controller.Object);
}

测试代码

Controller

public ProductCatsController(IProCatRepository repository, IMapper mapper)
{
    _ProCatService = repository;
    _mapper = mapper;
}

构造器

[HttpPost]
[Authorize(Roles = "Admin")]
public ActionResult<CategoryDto> CreateCategory(CreateCatDto Categ)
{
    var catEntity = _mapper.Map<ProductCat>(Categ);
    var NewCategory = _ProCatService.AddCat(catEntity);

    var CatForReturn = _mapper.Map<CategoryDto>(NewCategory);

    return CreatedAtRoute("GetCat", new { id = CatForReturn.Id }, CatForReturn);
}

Post method - this is what I need to test

发布方法

You are missing the new keyword.

_Controller = new ProductCatsController(_Controller.Object);

Further you need to pass in the relevant parameters for ProductCatsController constructor of the types IProCatRepository repository, IMapper mapper

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM