moq对象返回方法,应返回空对象
我正在开发一个 Web API,我提出的测试之一是,如果客户端使用物理测试 ID 进行 GET 操作(物理测试是我正在寻找的资源)并且未找到该物理测试,Web API 应返回 404 状态。
现在,我使用 moq 框架进行测试,并且有以下代码:
[TestMethod]
public void then_if_physical_test_not_found_return_not_found_status()
{
var unitOfWork = new Mock<IUnitOfWork>();
var repository = new Mock<IRepository<PhysicalTest>>();
repository.Setup(r => r.FindById(It.IsAny<int>())).Returns();
unitOfWork.Setup(m => m.PhysicalTests).Returns(repository.Object);
var pt = new PhysicalTestResource(unitOfWork.Object);
HttpResponseMessage<PhysicalTest> response = pt.GetPhysicalTest(43);
Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode)
}
我需要 Returns() 方法返回一个 null 对象,如果找不到资源,这将是实际 API 方法将返回的内容。
我尝试在 Returns() 方法中发送 null 作为参数,但没有成功。
I'm developing a Web API, and one of the test I came up with is that, if client makes a GET operation with a Physical Test ID (Physical Test is the resource I'm looking for) and that physical test is not found, the web API should return a 404 status.
Now, I'm using moq framework for my tests and I have the following code:
[TestMethod]
public void then_if_physical_test_not_found_return_not_found_status()
{
var unitOfWork = new Mock<IUnitOfWork>();
var repository = new Mock<IRepository<PhysicalTest>>();
repository.Setup(r => r.FindById(It.IsAny<int>())).Returns();
unitOfWork.Setup(m => m.PhysicalTests).Returns(repository.Object);
var pt = new PhysicalTestResource(unitOfWork.Object);
HttpResponseMessage<PhysicalTest> response = pt.GetPhysicalTest(43);
Assert.AreEqual(HttpStatusCode.NotFound, response.StatusCode)
}
I need the Returns() method to return a null object, which is going to be what the actual API method would return if the resource is not found.
I tried sending null as a parameter in the Returns() method but had no success.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您没有指出错误是什么,但这应该有效:
我怀疑您尝试使用
Returns(null)
调用它,这会导致编译器抱怨,因为Returns
被重载并且它不知道应该调用哪个方法。转换为特定类型可以消除歧义。You don't indicate what the error was, but this should work:
I suspect you tried to call it with
Returns(null)
, which causes the compiler to complain sinceReturns
is overloaded and it doesn't know which method should be called. Casting to a specific type removes the ambiguity.如果您收到如下错误:
您只需指定“Returns”方法的输入参数即可。
看看我的例子:
If you are receiving an error like this:
You just need to specify the input parameter of 'Returns' method.
Take a look in my example:
rt
是方法的返回类型:FindById
repository.Setup(r => r.FindById(It.IsAny())).Returns (Task.FromResult((rt)null));
rt
is a return type of method:FindById
repository.Setup(r => r.FindById(It.IsAny<int>())).Returns(Task.FromResult((rt)null));
Organization
是方法的返回类型:Get
Organization
is a return type of method:Get
除了按照接受的答案将成员强制转换为 null 之外,这种方法也应该有效。
Along with casting the member to null as mentioned by the accepted answer, this approach should work as well.
你可以试试这个:
You could try this:
你太接近了,你只需要像这样将返回类型作为泛型类型传递
You're too close, you only need to pass the return type as generic type like so