c# - 通过 autofac 模拟后,服务的实际实现被调用
问题描述
我正在为项目编写单元测试用例,
服务类被模拟,甚至在模拟实际实现之后被调用,请建议正确模拟的方法。这样服务方法就被正确地模拟了。G
[TestMethod]
public void CustomerTest()
{
using (var mock = AutoMock.GetLoose())
{
//For testing,Created dummy object of customer having datatable dt
var response = Task.FromResult(customer(dt))
// Arrange - configure the mock
mock.Mock<ICustomerService>().Setup(x => x.GetCustomerDetails(It.IsAny<string>(),It.IsAny<string>())).Returns(response);
var sut = mock.Create<CustomerViewModel>();
// Act
var actual = sut.GetCustomerInfo("12345", "Name");
// Assert - assert on the mock
mock.Mock<ICustomerService>().Verify(x => x.GetCustomerDetails(It.IsAny<string>(),It.IsAny<string>(),Times.Once());
Assert.AreEqual(response, actual);
}
}
我必须模拟服务,以便不会调用实际的服务方法。
解决方案
使服务方法虚拟化,这对我来说对 NUnit 有用。
推荐阅读
- java - 获取 Android 上每个核心的当前 cpu 使用情况
- python - 如何在 Odoo Docker 中关闭 TCP 连接
- javascript - 从一个模型中获取层并将其分配给另一个模型
- python-3.x - 有没有办法将纯色背景扩展到页面的整个宽度以获取 matplotlib 子图中的标题?
- java - 嵌入在 Spring MVC/Rest 中的 React 应用程序:浏览器 GET 请求路由
- php - Laravel:检索属于帖子的类别名称而不是category_id
- php - 将 PHP 和 MySQL 创建的 CSV 保存到服务器
- html - 带有居中图标的 Angular Material mat-nav-list 多行
- android - Jenkins CI/CD 无法发布到 Android Playstore
- sql - 仅选择非空列值并使用 sql 查询仅显示该值