moq对象返回方法,应返回空对象

发布于 2024-12-12 01:18:32 字数 837 浏览 0 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

夜血缘 2024-12-19 01:18:32

您没有指出错误是什么,但这应该有效:

unitOfWork.Setup(m => m.PhysicalTests).Returns((IRepository<PhysicalTest>)null);

我怀疑您尝试使用 Returns(null) 调用它,这会导致编译器抱怨,因为 Returns被重载并且它不知道应该调用哪个方法。转换为特定类型可以消除歧义。

You don't indicate what the error was, but this should work:

unitOfWork.Setup(m => m.PhysicalTests).Returns((IRepository<PhysicalTest>)null);

I suspect you tried to call it with Returns(null), which causes the compiler to complain since Returns is overloaded and it doesn't know which method should be called. Casting to a specific type removes the ambiguity.

俏︾媚 2024-12-19 01:18:32

如果您收到如下错误:

在此处输入图像描述

您只需指定“Returns”方法的输入参数即可。
看看我的例子:

_ = _fileStorage.Setup(x => x.LoadDocument(It.IsAny<string>())).Returns(value: null);

If you are receiving an error like this:

enter image description here

You just need to specify the input parameter of 'Returns' method.
Take a look in my example:

_ = _fileStorage.Setup(x => x.LoadDocument(It.IsAny<string>())).Returns(value: null);
不忘初心 2024-12-19 01:18:32

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));

夜光 2024-12-19 01:18:32

Organization 是方法的返回类型:Get

mockCache
    .Setup(cache => cache.Get(It.IsAny<string>(), It.IsAny<string>(),It.IsAny<string>()))
    .Returns(value: null as Organization);

Organization is a return type of method: Get

mockCache
    .Setup(cache => cache.Get(It.IsAny<string>(), It.IsAny<string>(),It.IsAny<string>()))
    .Returns(value: null as Organization);
鹿! 2024-12-19 01:18:32

除了按照接受的答案将成员强制转换为 null 之外,这种方法也应该有效。

unitOfWork.Setup(m => m.PhysicalTests).Returns(() => null);

Along with casting the member to null as mentioned by the accepted answer, this approach should work as well.

unitOfWork.Setup(m => m.PhysicalTests).Returns(() => null);
无妨# 2024-12-19 01:18:32

你可以试试这个:

ref1.Setup(s => s.Method(It.IsAny<Ref2>(), It.IsAny<string>()))
     .Returns((Task<Ref3>)null);

ref1 = Mock Interface
Ref2 = Type request parameter
Ref3 = Type of return method mock

You could try this:

ref1.Setup(s => s.Method(It.IsAny<Ref2>(), It.IsAny<string>()))
     .Returns((Task<Ref3>)null);

ref1 = Mock Interface
Ref2 = Type request parameter
Ref3 = Type of return method mock
明月松间行 2024-12-19 01:18:32

你太接近了,你只需要像这样将返回类型作为泛型类型传递

repository.Setup(r => r.FindById(It.IsAny<int>())).Returns<IRepository<PhysicalTest>>(null);

You're too close, you only need to pass the return type as generic type like so

repository.Setup(r => r.FindById(It.IsAny<int>())).Returns<IRepository<PhysicalTest>>(null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文