リスナーとミューテーターの役割は、同じクラス(アダプターなど)で組み合わせることができますが、両方の役割を一緒にテストしないでください。
PropertyChanged
1つのテストでは、リスニングクラスが設計どおりにイベントに反応することを確認するだけです。そのテストでプロパティが変更された原因は気にしません。
[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; } );
}
別のテストでは、クラスが設計どおりにミューテーターの役割を果たしていることを確認します。
[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);
}
このように、あなたがやろうとしているように、モックオブジェクトに実際のロジックを入れる必要は決してありません。これには、テスト容易性のためにクラスを設計する必要があります。そのようなテストを既存のクラスに追加することは困難です。したがって、テスト駆動開発の実践。