gpt4 book ai didi

c# - 最小起订量设置返回空

转载 作者:行者123 更新时间:2023-11-30 23:28:36 24 4
gpt4 key购买 nike

因此,我开始在我正在从事的 Web Api 项目上学习实现 UniTesting。发生的事情是,当我设置一个模拟调用时,这个调用返回 null 而不是我要返回的值。我不知道为什么类似的设置可以工作,但这个却不行。

这是我的测试类

namespace API.Tests.Web
{
[TestClass]
public class MaterialsControllerTest
{
private MaterialsController controller;
private Mock<IRTWRepository> repository;
private Mock<IModelFactory> factory;
private Mock<IRTWAPIIdentityService> identityService;
List<MaterialAccepted> materials;
MaterialAccepted material;

[TestInitialize]
public void Initialize()
{
repository = new Mock<IRTWRepository>();
factory = new Mock<IModelFactory>();
identityService = new Mock<IRTWAPIIdentityService>();
controller = new MaterialsController(repository.Object);
material = new MaterialAccepted()
{
business = true,
businessService = EnumRecycleCenterService.Dropoff,
residential = false,
residentialService = EnumRecycleCenterService.Pickup,
note = "this a note",
Category = new Category()
{
name = "Books"
}
};

materials = new List<MaterialAccepted>()
{
new MaterialAccepted() { business=true,businessService=EnumRecycleCenterService.Dropoff,residential=false,residentialService=EnumRecycleCenterService.Pickup,note="this a note"},
new MaterialAccepted() { business=false,businessService=EnumRecycleCenterService.Dropoff,residential=true,residentialService=EnumRecycleCenterService.Pickup,note="this a note"},
};
}

[TestMethod]
public void Post_ShouldReturnBadRequestWhenMaterialAcceptedModelValidationFails()
{
//arrange
repository.Setup(r => r.RecycleCenterRepository.Get(3)).Returns(() => new RecycleCenter());
controller.ModelState.AddModelError("error", "unit test error");
//act

var actionResult = controller.Post(2, new MaterialAcceptedModel());

Assert.IsInstanceOfType(actionResult, typeof(BadRequestResult));
}
}
}

这是我要测试的 Controller 中的操作

[HttpPost]
[Route("api/recyclecenters/{rcid}/materials/")]
public IHttpActionResult Post(int rcid, [FromBody]MaterialAcceptedModel model)
{
try
{
if (model != null)
{
var recycleCenter = TheRepository.RecycleCenterRepository.Get(rcid);

if (recycleCenter == null)
return NotFound();

if (!ModelState.IsValid)
return BadRequest(ModelState);

var entity = TheModelFactory.Parse(model);

if (entity == null) return BadRequest("Could not read material accepted in body");

if (TheRepository.MaterialAcceptedRepository.Get(recycleCenter.RecycleCenterId, entity.Category.name) != null)
return Conflict();

recycleCenter.Materials.Add(entity);

if (TheRepository.SaveAll())
{
string locationHeader = Url.Link("Materials", new { rcid = rcid, name = model.category.ToLower() });
return Created<MaterialAcceptedModel>(locationHeader, TheModelFactory.Create(entity));
}
return BadRequest("Could not save to the database");
}
return BadRequest();
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}

如果我运行这个测试,它会失败,因为它返回一个 NotFoundResult 的实例类型而不是 BadRequestResult,这是因为测试方法停止在这一行

if (recycleCenter == null)
return NotFound();

但是这个测试应该在这条线上停止

 if (!ModelState.IsValid)
return BadRequest(ModelState);

为什么会这样

 repository.Setup(r => r.RecycleCenterRepository.Get(3)).Returns(() => new RecycleCenter());

当它应该返回一个新的 RecycleCenter 时返回 null

最佳答案

看起来您正在为 rcid = 3 设置存储库 mock,并使用 rcid = 2 在 Controller 中调用存储库。

        //arrange
repository.Setup(r => r.RecycleCenterRepository.Get(3)).Returns(() => new RecycleCenter());
controller.ModelState.AddModelError("error", "unit test error");
//act

var actionResult = controller.Post(2, new MaterialAcceptedModel());

尝试用 rcid = 3 调用它

        var actionResult = controller.Post(3, new MaterialAcceptedModel());

或将 Moq 设置参数更改为 It.IsAny<int>()

关于c# - 最小起订量设置返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35931706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com