【发布时间】:2017-11-04 09:52:11
【问题描述】:
所以我有一个单元测试,它使用一个名为ImpositionCalculatorManager 的类中的方法。在这个类中,我使用依赖注入,因此我可以访问另一个名为 ImpositionCalculatorRepository 的类上的方法。
ImpositionCalculatorManager 看起来像这样:
public class ImpositionCalculatorManager : IImpositionCalculatorManager {
private readonly IImpositionCalculatorRepository _impositionCalculatorRepository;
public ImpositionCalculatorManager(IImpositionCalculatorRepository impositionCalculatorRepository) {
_impositionCalculatorRepository = impositionCalculatorRepository;
}
public ComboBoxItem[] ReturnInkDataSource() {
return _impositionCalculatorRepository.ReturnInkDataSource();
}
public ComboBoxItem[] ReturnCoatingDataSource() {
return _impositionCalculatorRepository.ReturnCoatingDataSource();
}
}
}
在我的单元测试中,我模拟了 ImpositionCalculatorManager 并将模拟版本 ImpositionCalculatorRepository 传递到管理器类的构造函数中。
但是我收到错误Constructor arguments cannot be passed for interface mocks。
这是我的单元测试夹具的样子:
public class ImpositionCalculatorPresenterTestFixture {
private ImpositionCalculatorPresenter _impositionCalculatorPresenter;
private readonly SystemVariablesPresenter _systemVariablesPresenter;
private readonly Mock<IImpositionCalculatorManager> _mockImpositionCalculatorManager;
private readonly Mock<ISystemVariablesView> _mockSystemVariablesView;
private readonly Mock<IPrintingDesignManager> _mockPrintingDesignManager;
private readonly Mock<ISystemVariablesManager> _mockSystemVariablesManager;
private readonly Mock<IImpositionCalculatorRepository> _mockImpositionCalculatorRepo;
private Mock<IImpositionFormView> _mockView;
public ImpositionCalculatorPresenterTestFixture() {
_mockImpositionCalculatorRepo = new Mock<IImpositionCalculatorRepository>();
//Error occurs on the below line
_mockImpositionCalculatorManager = new Mock<IImpositionCalculatorManager>(_mockImpositionCalculatorRepo.Object);
_mockSystemVariablesView = new Mock<ISystemVariablesView>();
_mockPrintingDesignManager = new Mock<IPrintingDesignManager>();
_mockSystemVariablesManager = new Mock<ISystemVariablesManager>();
_systemVariablesPresenter = new SystemVariablesPresenter(_mockSystemVariablesView.Object, _mockSystemVariablesManager.Object);
}
[TestMethod]
public void PopulateInkDataSources_ApplicationOnLoad_InkDataSourcesPopulatedWithDataFromJSON() {
//Arrange
SetupImpostionPresenter();
_mockView.SetupProperty(r => r.InkSideOneDataSource);
_mockView.SetupProperty(r => r.InkSideTwoDataSource);
_mockImpositionCalculatorManager.Setup(r => r.ReturnInkDataSource())
.Returns<ComboBoxItem[]>
(x => new ComboBoxItem[] {
new ComboBoxItem("Test", 1 ),
new ComboBoxItem("Test 2", 2)
});
//Act
_mockView.Raise(r => r.FormOnLoad += null, new EventArgs());
//Assert
Assert.IsTrue(_mockImpositionCalculatorManager.Object.ReturnInkDataSource()
== _mockView.Object.InkSideOneDataSource && _mockImpositionCalculatorManager.Object.ReturnInkDataSource()
== _mockView.Object.InkSideTwoDataSource
);
}
private void SetupImpostionPresenter() {
_mockView = new Mock<IImpositionFormView>();
_impositionCalculatorPresenter = new ImpositionCalculatorPresenter(_mockView.Object,
_mockImpositionCalculatorManager.Object, _mockSystemVariablesManager.Object, _systemVariablesPresenter,
_mockPrintingDesignManager.Object);
}
}
}
我看过堆栈溢出,人们说这是因为接口没有构造函数,只有类有,我不应该向构造函数传递任何东西,但是当我删除构造函数参数时从模拟中,我在尝试从管理器设置方法时收到错误Parameter count mismatch。
我只想模拟经理,这样我就可以设置一个方法来返回我已经设置的值。
为什么这不起作用?
【问题讨论】:
-
看起来你在这里缺少模拟声明
private readonly Mock<IImpositionCalculatorPresenter> _impositionCalculatorPresenter; -
我的演示者不是一个接口,我必须将我的所有方法都虚拟化,我不想这样做。无论如何,这样做会有所作为吗?
标签: c# unit-testing constructor moq