2010-05-25 71 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