2012-01-02 21 views
2

我試圖用RhinoMocks聲明某個屬性設置器被調用。但它沒有按預期工作。RhinoMocks:AssertWasCalled在存​​根上不起作用

以下簡化示例說明了此問題。

考慮這個接口:

public interface IMyInterface 
{ 
    string SomeProperty { get; set; } 
} 

現在考慮下面的代碼:

var mock = MockRepository.GenerateStub<IMyInterface>(); 
mock.SomeProperty = "abc"; 

mock.AssertWasCalled(x => x.SomeProperty = Arg<string>.Is.Anything); 

我期待在最後一行的斷言會通過沒有問題。然而,這是投擲ExpectationViolationException與此消息:

「IMyInterface.set_SomeProperty(任何);預期#1,實際#0」。

我不明白爲什麼會發生這種情況。任何人都可以幫忙嗎?

回答

7

GenerateStub<T>返回的對象不記錄屬性和方法調用。如果要聲明setter,getters或methods已被調用,請改爲使用GenerateMock<T>

// Replace 
var mock = MockRepository.GenerateStub<IMyInterface>(); 

// with 
var mock = MockRepository.GenerateMock<IMyInterface>(); 

// and everything should work again.