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.