在单元测试中使用 Moq 时出现问题
我一直在探索模拟对象在单元测试中的使用,并一直在尝试 .NET 的 Moq 框架。我在尝试测试从数据库返回域对象的服务层方法时遇到一些问题。
这是我的设置:
[SetUp]
public void DoSetupTasks()
{
mockDao = new Mock<IHibernateDao>();
_hibernateService = new HibernateService(mockDao.Object);
mockDomainObject = new Mock<DomainBase>();
dmBase = new DomainBase()
{
Id = 5
};
}
这是我遇到问题的单元测试。 FindById() 方法根据给定的 ID 和类型返回 DomainBase 对象。
[Test]
public void TestFindById()
{
mockDomainObject.Setup(dmb => dmb.Id.Equals(It.IsAny<long>())).Returns(true);
mockDao.Setup(dao => dao.FindById(
It.IsAny<long>(),
It.IsAny<Type>()
)).Returns(mockDomainObject.Object);
_hibernateService.FindById(dmb.Id, typeof(DomainBase));
mockDomainObject.VerifySet(dmb => dmb.Id = dmBase.Id);
}
当我运行单元测试时,它抛出以下异常:
Exception: Invalid setup on a non-virtual (overridable in VB) member: dmb => dmb.Id.Equals(It.IsAny
我承认,我对这个框架非常不熟悉。我一直在尝试遵循一些教程,但我一直无法弄清楚。
I've been exploring the use of mock objects in unit testing and have been trying out the Moq framework for .NET. I'm having some issues in attempting to test a service-layer method that returns a domain object from a database.
Here is my setup:
[SetUp]
public void DoSetupTasks()
{
mockDao = new Mock<IHibernateDao>();
_hibernateService = new HibernateService(mockDao.Object);
mockDomainObject = new Mock<DomainBase>();
dmBase = new DomainBase()
{
Id = 5
};
}
Here is the unit test I am having problems with. The method FindById()
returns a DomainBase object based on a given ID and Type.
[Test]
public void TestFindById()
{
mockDomainObject.Setup(dmb => dmb.Id.Equals(It.IsAny<long>())).Returns(true);
mockDao.Setup(dao => dao.FindById(
It.IsAny<long>(),
It.IsAny<Type>()
)).Returns(mockDomainObject.Object);
_hibernateService.FindById(dmb.Id, typeof(DomainBase));
mockDomainObject.VerifySet(dmb => dmb.Id = dmBase.Id);
}
When I run the unit test, it throws the following exception:
Exception: Invalid setup on a non-virtual (overridable in VB) member: dmb => dmb.Id.Equals(It.IsAny<Int64>())
I'll admit, I'm pretty unfamiliar with the framework. I've been trying to follow some tutorials on it, but I haven't been able to get it figured out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试更像这样的事情:
您不需要模拟您的域对象 - 通常最好模拟您的数据层和服务,然后在被测试的逻辑代码和您的逻辑代码之间传递真实的域对象(包含示例/测试数据)模拟服务层。
编辑:要测试保存和删除方法,您需要执行类似的操作。请注意,当调用模拟方法时,我们如何使用 Moq 库的 Callback() 方法来运行一些代码,并且在这个回调中,我们断言传递给我们方法的对象是我们期望的对象:
Try something more like this:
You shouldn't need to mock your domain objects - you're generally better off mocking your data layer and services and then passing real domain objects (containing sample/test data) between your logic code under test and your mocked service layers.
EDIT: To test save and delete methods, you'll want to do something like this. Note how we're using the Callback() method of the Moq library to run some code when the mock method is called, and within this callback we're asserting that the object passed to our method was the object we were expecting: