编程语言
首页 > 编程语言> > c# – 当更改时,使模拟触发PropertyChanged

c# – 当更改时,使模拟触发PropertyChanged

作者:互联网

我正在使用RhinoMocks,我有一个Mock,它具有我需要表现为属性的属性 – 在设置时更新其值,并在更改属性时触发PropertyChanged.

模拟对象的接口本质上是这样的:

public interface IFoo
{
    event PropertyChangedEventHandler PropertyChanged;
    int Bar { get; set; }
}

在创建模拟时,我设置了PropertyBehavior – 这使它实际更新了它的伪造值:

var mocks = new MockRepository();
var fakeFoo = mocks.DynamicMock<IFoo>();
SetupResult.For(fakeFoo.Bar).PropertyBehavior();

但是当我更新值时,PropertyChanged没有被触发.现在,接口没有实现INotifyPropertyChanged接口,因为它是一个接口..如何触发PropertyChanged?

解决方法:

侦听器和增变器的角色有时可以组合在同一个类中(例如,在适配器中),但是这两个角色不应该一起测试.

在一个测试中,您只需验证您的监听类是否按照设计对PropertyChanged事件做出反应.您不关心导致该属性在该测试中发生变化的原因:

[Test]
public void Updates_Caption_when_Bar_PropertyChanged()
{
   var foo = MockRepository.GenerateStub<IFoo>();
   foo.Bar = "sometestvalue1";
   var underTest = new UnderTest(foo);

   // change property and raise PropertyChanged event on mock object
   foo.Bar = "sometestvalue2";
   foo.Raise(x=>x.PropertyChanged+=null,
       foo,
       new PropertyChangedEventArgs("Bar"));

   // assert that the class under test reacted as designed
   Assert.AreEqual("sometestvalue2", underTest.Caption);

   // or if the the expected state change is hard to verify, 
   // you might just verify that the property was at least read
   foo.AssertWasCalled(x => { var y = foo.Bar; } );
}

在另一个测试中,您验证您的类是否按设计播放其mutator角色:

[Test]
public void Reset_clears_Foo_Bar()
{
   var foo = MockRepository.GenerateStub<IFoo>();
   foo.Bar = "some string which is not null";
   var underTest = new UnderTest(foo);

   underTest.Reset();

   // assert that the class under test updated the Bar property as designed
   Assert.IsNull(foo.Bar);
}

这样,就没有必要像你想要的那样将真实逻辑放入你的模拟对象中.这确实需要您设计可测试性的类;很难将这些测试添加到现有的类中.因此test driven development的做法.

标签:c,mocking,rhino-mocks,propertychanged
来源: https://codeday.me/bug/20190610/1213766.html