我是單元測驗的新手,并試圖模擬postContinent. 但是給出null和BadRequestObjectResult。
大陸控制器測驗
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
uj5u.com熱心網友回復:
問題出在你的Setup功能上。僅當您重寫了 equals 函式或者它們是完全相同的參考時,參考型別才相等。
因此,通過使用 new 關鍵字設定它,它永遠不會匹配執行時間物件。
嘗試It.IsAny<T>MOQ的功能進行驗證。
在此處查看示例: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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/405281.html
標籤:
