Pau*_*aul 7 c# unit-testing moq
我正在编写单元测试并在尝试从抽象类mock中引发事件时收到异常.以下是示例代码:
public abstract class AbstractBase : EntityObject
{}
[TestMethod]
public void MyTest()
{
var mock = new Mock<AbstractBase>();
var notificationMock = entityMock.As<INotifyPropertyChanged>();
var propertyChangedMapper = new PropertyChangedMapper();
bool eventReceived = false;
propertyChangedMapper.MyPropertyChanged +=
(sender, eventArgs) =>
{
eventReceived = true;
};
propertyChangedMapper.Subscribe((AbstractBase)notificationMock.Object);
Assert.IsFalse(eventReceived);
notificationMock.Raise(e=>e.PropertyChanged += null,
new PropertyChangedEventArgs("Property1"));
Assert.IsTrue(eventReceived);
}
Run Code Online (Sandbox Code Playgroud)
显然我可以使用mock on INotifyPropertyChanged并且事件上升得很好,但是PropertyChangedMapper我需要将发送者强制转换AbstractBase为失败的Mock<INotifyPropertyChanged>
编辑:根据建议使用Mock.As<>()似乎是正确的方法,上面唯一的问题是,事件的上升notificationMock与对象的原始模拟无关.码:
notificationMock.Object.PropertyChanged += (s, e) =>
{
var result = "this one is fired as it should";
};
mock.Object.PropertyChanged += (s, e) =>
{
var result = "this one is not called but is actually what I need";
};
notificationMock.Raise(e => e.PropertyChanged += null,
new PropertyChangedEventArgs("Property1"));
Run Code Online (Sandbox Code Playgroud)
如果您将模拟设为多模拟,您也许能够进行所需的转换。由于 Moq 模拟通过泛型参数绑定到单个类型,因此您必须显式地逐步向模拟添加其他接口或超类,然后在测试中使用最终产品。下面是如何执行此操作的快速示例。
var baseMock = new Mock<AbstractBase>();
var inpcMock = baseMock.As<INotifyPropertyChanged>();
// ...setup event...
propertyChangedMapper.Subscribe(inpcMock.Object);
// ... assertions ...
Run Code Online (Sandbox Code Playgroud)