2010-05-25 70 views

回答

5

這是完全有可能的:

public class OneProperty 
{ 
    virtual public int MyInt 
    { 
     get; 
     set; 
    } 
} 

[Test] 
public void IntWasSet() 
{ 
    var prop = Rhino.Mocks.MockRepository.GenerateMock<OneProperty>(); 

    prop.MyInt = 5; 

    prop.AssertWasNotCalled(x => x.MyInt = Arg<int>.Is.Anything); 

    prop.VerifyAllExpectations(); 
} 

的犀牛嘲笑運行該測試結果3.5中出現以下錯誤:

Errors and Failures: 1) Test Error : InterfacerTests.TestMatchesInterface.IntWasSet Rhino.Mocks.Exceptions.ExpectationViolationException : Expected that OneProperty.set_MyInt(anything); would not be called, but it was found on the actual calls made on the mocked object.

我發現Arg<T>語法from this part of the Rhino documentation

2

將屬性設置爲測試中的某個已知值。調用不會更改屬性的代碼,然後聲明屬性與原來相同。不應該需要使用Rhino Mocks。

+0

在這裏使用Rhino Mocks的優勢在於您可以確定該值未設置。沒有它,所有你可以保證的是該物業的最終價值如預期。在某些情況下,例如制定者有其他副作用的地方,這可能很重要。 – 2014-09-18 20:41:17