【问题标题】:Moq returns null and BadRequestObjectResultMoq 返回 null 和 BadRequestObjectResult
【发布时间】:2022-01-07 16:36:09
【问题描述】:

我是单元测试新手,并试图模拟 postContinent。但是给nullBadRequestObjectResult

大陆控制器测试

 public class ContinentControllerTests {
 // RepoMocks
 private readonly Mock<IContinentRepository> _continentRepoMock = new Mock<IContinentRepository>();
 private readonly Mock<ICountryRepository> _countryRepoMock = new Mock<ICountryRepository>();
 private readonly Mock<ICityRepository> _cityRepoMock = new Mock<ICityRepository>();

 // Controller
 private readonly ContinentController _continentController;

     public ContinentControllerTests() {
     _continentServiceMock = new ContinentService(_continentRepoMock.Object);
     _continentController = new ContinentController(new ContinentService(_continentRepoMock.Object), new CountryService(_countryRepoMock.Object), new CityService(_cityRepoMock.Object));
     }
     [Fact]
     public void PostContinent_ValidInput_ReturnsCreateAtAction() {
     // Arrange
     _continentRepoMock
        .Setup(repo => repo.HeeftContinent("Test"))
        .Returns(false);
    _continentRepoMock
       .Setup(repo => repo.BestaatContinent(new Continent("Test", new List<Country>())))
       .Returns(false);
     _continentRepoMock
       .Setup(repo => repo.VoegContinentToe(new Continent("Test", new List<Country>())))
       .Returns(new Continent(1, "Test", new List<Country>()));
     // Act
     var response = _continentController.PostContinent(new ContinentInputDTO { Name = "Test" });

     // Assert
     Assert.IsType<CreatedAtActionResult>(response.Result);
     }
 }

大陆控制器

 public class ContinentController : ControllerBase {
     private string _hostURL = $"http://localhost:5000/api/continent";
     private string _riverURL = $"http://localhost:5000/api/river";

     private ContinentService _continentService;
     private CountryService _countryService;
     private CityService _cityService;

     public ContinentController(ContinentService continentService, CountryService countryService, CityService cityService) {
         _continentService = continentService;
         _countryService = countryService;
         _cityService = cityService;
     }

     [HttpPost]
     public ActionResult<ContinentOutputDTO> PostContinent([FromBody] ContinentInputDTO continentDto) {
         try {
             if (_continentService.HeeftContinent(continentDto.Name)) { return BadRequest("Continent naam moet unique zijn!"); }
             var mappedContinent = MapToDomain.MapToContinentDomain(continentDto);
             Continent continent = _continentService.VoegContinentToe(mappedContinent);
             return CreatedAtAction(nameof(GetContinent), new { continentId = continent.Id },
             MapFromDomain.MapFromContinentDomain(_hostURL, continent));
         }
         catch (Exception ex) { return BadRequest(ex.Message); }
     }
 }

大陆服务

public class ContinentService {
     private readonly IContinentRepository _repo;
     public ContinentService(IContinentRepository repo) { _repo = repo;}

      public Continent VoegContinentToe(Continent c) {
         if (c == null) throw new ContinentServiceException("VoegContinentToe : continent is null");
         if (_repo.BestaatContinent(c)) throw new ContinentServiceException("VoegContinentToe : continent bestaat reeds");
         try {return _repo.VoegContinentToe(c);}
         catch (Exception ex) { throw new ContinentServiceException("VoegContinentToe: ", ex);}
    }
}

错误:

消息: Assert.IsType() 失败
预期:Microsoft.AspNetCore.Mvc.CreatedAtActionResult
实际:Microsoft.AspNetCore.Mvc.BadRequestObjectResult

【问题讨论】:

标签: c# asp.net api unit-testing moq


【解决方案1】:

问题出在您的Setup 函数中。仅当您重写了 equals 函数或者它们是完全相同的引用时,引用类型才相等。

所以通过设置new关键字,它永远不会匹配执行时间对象。

尝试 MOQ 中的It.IsAny&lt;T&gt; 功能进行验证。

在此处查看示例:https://documentation.help/Moq/3CF54A74.htm

// Throws an exception for a call to Remove with any string value.
mock.Setup(x => x.Remove(It.IsAny<string>())).Throws(new InvalidOperationException());

示例。适用于所有设置。

_continentRepoMock
       .Setup(repo => repo.BestaatContinent(It.IsAny<Continent>()))
       .Returns(false);

【讨论】:

  • 您好@athanasios,感谢您的解释。但我真的不知道该放在哪里以及如何放置。
  • 查看更新的答案以获取使用示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-26
  • 2023-03-05
  • 1970-01-01
  • 2010-12-19
  • 2011-09-27
相关资源
最近更新 更多