gpt4 book ai didi

c# - MVC Controller HttpStatusCode 上的单元测试不等于

转载 作者:行者123 更新时间:2023-11-30 14:08:13 24 4
gpt4 key购买 nike

我正在对一个简单的 Controller 进行单元测试,检查在传入空 ID 时它是否返回 400 代码。但是,当我对此进行测试时,返回的结果并不等于 400 错误代码。

我的代码:

int? nullID = null;
var edit = controller.Edit(nullID) as ActionResult;

var result = new HttpStatusCodeResult(400, null);

Assert.AreEqual(edit, result);

当我调试测试时,我得到了预期的结果:

编辑结果:

Edit Result

预期结果:

Expected Result

我哪里错了?

最佳答案

Assert.AreEqual(a,b)Assert.IsTrue(Object.Equals(a,b)) 相同

HttpStatusCodeResult没有实现 Equals 所以调用转到默认的 Object.Equals() 而默认为 Object.RefrenceEquals(a,b))

If the current instance is a reference type, the Equals(Object) method tests for reference equality, and a call to the Equals(Object) method is equivalent to a call to the ReferenceEquals method. Reference equality means that the object variables that are compared refer to the same object.

关于c# - MVC Controller HttpStatusCode 上的单元测试不等于,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36546726/

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