C# 使用Rhino模拟更改单元测试InotifyProperty
我有一个实现INotifyPropertyChanged的类,我需要测试这个接口是否正确实现。我想使用Rhino模拟对象来实现这一点C# 使用Rhino模拟更改单元测试InotifyProperty,c#,unit-testing,inotifypropertychanged,rhino-mocks,C#,Unit Testing,Inotifypropertychanged,Rhino Mocks,我有一个实现INotifyPropertyChanged的类,我需要测试这个接口是否正确实现。我想使用Rhino模拟对象来实现这一点 class MyClass : INotifyPropertyChanged { public int X { get => ...; set => ... // should check if value changes and raise event PropertyChanged } }
class MyClass : INotifyPropertyChanged
{
public int X
{
get => ...;
set => ... // should check if value changes and raise event PropertyChanged
}
}
我想测试的是,当X改变值时,事件PropertyChanged被调用一次,并使用适当的参数
MyClass testObject = new MyClass();
// the mock:
PropertyChangedEventHandler a = MockRepository.GenerateMock<PropertyChangedEventHandler>();
testObject.PropertyChanged += a;
// expect that the mock will be called exactly once, with the proper parameters
a.Expect( (x) => ???)
.Repeat()
.Once();
// change X, and verify that the event handler has been called exactly once
testObject.X = testObject.X + 1;
a.VerifyAllExpectations(); ???
MyClass testObject=newmyclass();
//模拟:
PropertyChangedEventHandler a=MockRepository.GenerateMock();
testObject.PropertyChanged+=a;
//预计将使用正确的参数准确地调用mock一次
a、 期望值((x)=>??)
.重复
.一次();
//更改X,并验证事件处理程序是否只调用了一次
testObject.X=testObject.X+1;
a、 验证AllExpections()???
我认为我走的是正确的道路,但我无法让它工作。有时,如果使用真实的东西没有连锁反应,那么真的没有必要使用模拟 下面的简单示例创建委托的实例并验证预期行为 我想测试的是,当X改变值时,事件PropertyChanged被调用一次,并使用适当的参数
[TestClass]
公共类MyClassTests{
[测试方法]
public void应该调用属性更改一次(){
//安排
//商店电话
IDictionary properties=新字典();
PropertyChangedEventHandler处理程序=新的PropertyChangedEventHandler((s,e)=>{
如果(!properties.ContainsKey(e.PropertyName))
添加(例如PropertyName,0);
属性[e.PropertyName]++;
});
MyClass testObject=新的MyClass();
testObject.PropertyChanged+=处理程序;
字符串expectedPropertyName=nameof(MyClass.X);
int expectedCount=1;
//表演
testObject.X=testObject.X+1;
//断言-使用FluentAssertions
properties.Should().ContainKey(expectedPropertyName);
属性[expectedPropertyName].Should().Be(expectedCount);
}
类MyClass:INotifyPropertyChanged{
公共事件PropertyChangedEventHandler PropertyChanged=委托{};
void raisePropertyChanged([CallerMemberName]字符串propertyName=null){
调用(这是新的PropertyChangedEventArgs(propertyName));
}
int x;
公共整数X{
get=>x;
设置{
如果(值!=x){
x=值;
raisePropertyChanged();
}
}
}
}
}
[TestClass]
public class MyClassTests {
[TestMethod]
public void Should_Call_PropertyChanged_Once() {
//Arrange
//Store calls
IDictionary<string, int> properties = new Dictionary<string, int>();
PropertyChangedEventHandler handler = new PropertyChangedEventHandler((s, e) => {
if (!properties.ContainsKey(e.PropertyName))
properties.Add(e.PropertyName, 0);
properties[e.PropertyName]++;
});
MyClass testObject = new MyClass();
testObject.PropertyChanged += handler;
string expectedPropertyName = nameof(MyClass.X);
int expectedCount = 1;
//Act
testObject.X = testObject.X + 1;
//Assert - using FluentAssertions
properties.Should().ContainKey(expectedPropertyName);
properties[expectedPropertyName].Should().Be(expectedCount);
}
class MyClass : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged = delegate { };
void raisePropertyChanged([CallerMemberName]string propertyName = null) {
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
int x;
public int X {
get => x;
set {
if (value != x) {
x = value;
raisePropertyChanged();
}
}
}
}
}