在起订量匹配的安装问题 [英] Problem with matching setup in Moq
问题描述
我一直在使用起订量在过去的一周左右,还没有到今天的任何问题。我在使用越来越 VerifyAll()
来正确地匹配我的模拟的设置有问题。
I've been using Moq for the past week or so and haven't had any issues until today. I'm having a problem with getting VerifyAll()
to properly match the setup of my mock.
我目前正在写单元测试我的应用程序的API。这里的应用程序是如何组织的:
I'm currently writing unit tests for my application's API. Here's how the application is structured:
API< ==>服务< ==> DAO< ==>数据库
考虑到这一点,我嘲讽的服务对象,然后用嘲笑的服务构建的API对象。我已经写了一些单元测试已经没有问题,到现在为止。
With this in mind, I'm mocking the service object and then constructing an API object using the mocked service. I've written a number of unit tests already without problem up until now.
我有两个实例变量是这样的:
I have two instance variables like this:
private Api _api;
private Mock<IHibernateService> mockService;
我初始化这些的设置方法:
I initialize these in a setup method:
[SetUp]
public void DoSetupTasks()
{
mockService = new Mock<IHibernateService>();
_api = new Api(mockService.Object);
}
下面是单元测试失败:
[Test]
public void TestSearchOnAllProperties()
{
mockService
.Setup(service => service.LoadAll(It.IsAny<Type>()))
.Returns(new DomainBase[0]);
var dmbs = _api.SearchOnAllProperties("search term", typeof(DomainBase));
mockService.VerifyAll();
}
该API的 SearchOnAllProperties()
办法将随后打电话到该服务的 LOADALL()
办法(有一些当然,额外的逻辑),所以我想验证它被称为正常。为了澄清,这里是如何 LOADALL()
被称为 SearchOnAllProperties()
:
The API's SearchOnAllProperties()
method will subsequently make a call to the service's LoadAll()
method (with some additional logic of course), so I want to verify that it's being called properly. To clarify, here's how LoadAll()
is being called in SearchOnAllProperties()
:
public IEnumerable<DomainBase> SearchOnAllProperties(string searchTerm, Type type)
{
foreach (DomainBase dmb in _hibernateService.LoadAll(type))
{
// additional logic
}
}
然而,当我运行单元测试,我收到了 MockVerificationException
,指出给定的设置不匹配。我想不通为什么它要调用该服务的 LOADALL()
方法。
However, when I run the unit test, I get a MockVerificationException
stating that the given setup was not matched. I cannot figure out why as it should be calling the service's LoadAll()
method.
推荐答案
一个可能的原因是,在某些时候这种特定的测试方法被调用之前, mockService
被分配以的新实例模拟&LT; IHibernateService&GT;
。如果是这样的话,那么这种测试方法将是调用设置
在错误的情况下,这将继而产生此异常。
One possible cause is that at some point before this particular test method is called, mockService
is being assigned to a new instance of Mock<IHibernateService>
. If that is the case, then this test method would be calling Setup
on the wrong instance, which would then produce this exception.
一个快速的方法来测试这将是使用本地 mockService
和 API
变量,看看测试仍失败:
A quick way to test this would be to use local mockService
and api
variables and see if the test still fails:
[Test]
public void TestSearchOnAllProperties()
{
var localMockService = new Mock<IHibernateService>();
var localApi = new Api(localMockService.Object);
localMockService
.Setup(service => service.LoadAll(It.IsAny<Type>()))
.Returns(new DomainBase[0]);
var dmbs = localApi.SearchOnAllProperties("search term", typeof(DomainBase));
localMockService.VerifyAll();
}
心连心
这篇关于在起订量匹配的安装问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!