我假设MOQ会自动为任何嵌套依赖项创建Mocks。
我正在测试一个ASP.Net MVC控制器:
public class TransactionController : Controller
{
private readonly ITransactionService _transactionService;
private readonly SearchPanelVmBuilder _searchPanelVmBuilder;
private readonly TransactionVmsBuilder _transactionVmsBuilder;
public TransactionController(TransactionVmsBuilder transactionVmsBuilder, ITransactionService transactionService, SearchPanelVmBuilder searchPanelVmBuilder)
{
_transactionVmsBuilder = transactionVmsBuilder;
_transactionService = transactionService;
_searchPanelVmBuilder = searchPanelVmBuilder;
}
// other methods omitted for brevity
public PartialViewResult SearchPanel()
{
var vm = _searchPanelVmBuilder.BuildVm();
return PartialView("_SearchPanel", vm);
}
}
单元测试代码:
[Fact]
public void SeachPanel_Calls_BuildSearchPanelVm()
{
// Arrange
var mockTransService = new Mock<ITransactionService>();
var mockTransVmsBuilder = new Mock<TransactionVmsBuilder>();
var mockSearchPanelVmBuilder = new Mock<SearchPanelVmBuilder>();
var controller = new TransactionController(mockTransVmsBuilder.Object, mockTransService.Object, mockSearchPanelVmBuilder.Object);
// Act
controller.SearchPanel();
// Assert
mockSearchPanelVmBuilder.Verify(x => x.BuildVm());
}
MOQ抱怨:
无法实例化类的代理:MCIP.Web.UI.ViewModelBuilders.Singular.SearchPanelVmBuilder。 找不到无参数构造函数。
无法实例化代理的类:
public class SearchPanelVmBuilder
{
private readonly ITransactionTypeService _transactionTypeService;
private readonly TransactionTypeVmBuilder _transactionTypeVmBuilder;
private readonly UserProvider _userProvider;
public SearchPanelVmBuilder(
UserProvider userProvider,
ITransactionTypeService transactionTypeService,
TransactionTypeVmBuilder transactionTypeVmBuilder
)
{
_userProvider = userProvider;
_transactionTypeService = transactionTypeService;
_transactionTypeVmBuilder = transactionTypeVmBuilder;
}
public virtual SearchPanelVm BuildVm()
{
return new SearchPanelVm
{
Userlist = _userProvider.GetOperators(),
TransactionTypes =
_transactionTypeService.GetAll().Select(x => _transactionTypeVmBuilder.BuildVmFromModel(x)).ToList()
};
}
}
其相应的依赖项:
public class UserProvider
{
private static int retryCount;
public virtual List<string> GetOperators()...
public virtual List<string> GetGroupsForUser(WindowsIdentity identity)...
}
public interface ITransactionTypeService
{
List<TransactionType> GetAll();
}
public class TransactionTypeVmBuilder
{
public virtual TransactionTypeVm BuildVmFromModel(TransactionType transactionType)...
}
我做错了吗?
我是否必须明确告诉MOQ自动模拟嵌套依赖项?
或者我必须明确设置嵌套的Mocks - 就像这样:
var mockUserProvider = new Mock<UserProvider>();
var mockTransTypeService = new Mock<ITransactionTypeService>();
var mockTransactionTypeVmBuilder = new Mock<TransactionTypeVmBuilder>();
var mockSearchPanelVmBuilder = new Mock<SearchPanelVmBuilder>(mockUserProvider.Object, mockTransTypeService.Object, mockTransactionTypeVmBuilder.Object);
答案 0 :(得分:2)
是的,你的假设是正确的。因为类SearchPanelVmBuilder
没有为此类提供无参数构造函数Mock,所以不能像这样创建:
var mockSearchPanelVmBuilder = new Mock<SearchPanelVmBuilder>()
导致异常:Could not find a parameterless constructor
。
通过提供所有参数来创建模拟,如下所示:
UserProvider userProvider = new UserProvider();
Mock<ITransactionTypeService> transactionTypeService = new Mock<ITransactionTypeService>();
TransactionTypeVmBuilder transactionTypeVmBuilder = new TransactionTypeVmBuilder();
// Use constructor with parameters here because SearchPanelVmBuilder
// doesn't have parameterless constructor
var mockSearchPanelVmBuilder = new Mock<SearchPanelVmBuilder>(
userProvider, transactionTypeService.Object, transactionTypeVmBuilder);
var mockTransService = new Mock<ITransactionService>();
var mockTransVmsBuilder = new Mock<TransactionVmsBuilder>();
var controller = new TransactionController(
mockTransVmsBuilder.Object,
mockTransService.Object,
mockSearchPanelVmBuilder.Object);
然后可以创建控制器TransactionController
的实例,并可以在其上调用方法SearchPanel
。
答案 1 :(得分:1)
在这种情况下,您要测试的只是调用BuildVM并返回结果,因此您不需要模拟内部依赖项。在调用SearchPanel方法之前,您需要在Arrange部分中为BuildVM方法设置返回值。
mockSearchPanelVmBuilder.Setup(x => x.BuildVM()).Returns(mockSearchPanelVM);
因为如果不设置返回值,则模拟类和虚方法将运行实际实现。使用接口时,将抛出一个错误,指示您应设置返回值。