Rhino Mock(1)
- mock interfaces, delegates and classes, including those with parameterized constructors.
- set expectations on the called methods by using strongly typed mocks instead of strings.
- lends itself easily to refactoring & leaning on the compiler.
- allows a wide range of expectations to be set on a mock object or several mock objects.
- Attention: Rhino Mocks can only mock interfaces, delegates and virtual methods of classes! (Although it is possible to get work around existing code and easily apply Rhino Mocks, see Example of testing an abstract class)
- Rhino Mocks now supports "Recursive mocking" (as of revision 1683). Special guidance (see here) should be followed when using this functionality, however.
使用的规则。
All I need to do is abide by three rules:
- Use the static MockRepository.GenerateMock method. Don’t use an instance of a MockRepository, and don’t use the GenerateStub method.
- If the mocked instance method returns a value, use the Stub method to set up a result for that value. This is an indirect input.
- If the mocked instance method does not return a value (void), use the AssertWasCalled/AssertWasNotCalled methods. This is an indirect output.
一。主要思想。
1。 Record/Replay 方式,在record mode set up expectation . in replay mode verify .(列出预期的方法,调用,验证是否被调用,调用顺序。)
2. Arrange/Act/Assert方式,(类似于传统的nunit方法,准备数据,执行,假设结果)
Rhino Mocks currently support the creation of the following types of mock objects.
- Mock Objects - Strict replay semantics. Created by calling StrictMock()
- Dynamic Mock - Loose replay semantics. Created by calling DynamicMock()
- Partial Mock - Mock only requested methods. Created by calling PartialMock()
Strict replay semantics: Only the methods that were explicitly recorded are accepted as valid. This means that any call that is not expected would cause an exception and fail the test. All the expected methods must be called if the object is to pass verification.
严谨重播语义:只有显示记录的方法才是有效的,对任何未预期的方法进行调用都会导致异常。所有预期的方法都必须被调用,对象才能通过验证。
Loose replay semantics: All method calls during the replay state are accepted. If there is no special handling setup for a given method, a null or zero is returned. All of the expected methods must be called for the object to pass verification.
松散重播语义:所有的方法都可以调用,未预期的调用会返回0 or null 所有的预期方法必须被调用,对象才能通过验证。
Mocking only requested methods: This is available for classes only. It basically means that any non abstract method call will use the original method (no mocking) instead of relying on Rhino Mocks' expectations. You can selectively decide which methods should be mocked.
仅模拟请求的方法:只适用于类。
DynamicMock 用于验证部分方法,StrictMock用于整个接口验证。PartialMock 可以用于测试类,抽象类的部分方法,尤求是模板方法的测试。
DynamicMock和StrictMock的区别仅限于上面提到的,而实际使用时,效应各个方法入 record,之类时 是一样的。