Mock(1)

来源:互联网 发布:公知博客中国 编辑:程序博客网 时间:2024/06/05 17:23
 

A dynamic mock object framework for the .Net platform. It's purpose is to ease testing by allowing the developer to create mock implementations of custom objects and verify the interactions using unit testing.

Rhino.Mocks is an attempt to create easier way to build and use mock objects and allow better refactoring support from the current tools. It's a hybrid approach between the pure Record/Replay of EasyMock.Net's model and NMock's expectation based model. Rhino.Mocks originated from EasyMock.Net and attempt to improve on their model to create easy to use and power mocking framework. It's free for use and modification for open source and commercial software.

Licensing: Rhino Mocks is Free Software which is released under the BSD license.

What does Rhino Mocks offers?

  • Explicit record & replay model for expectations.
  • Working with strongly typed mocks.
  • Expectations based on:
    • Arguments matching
    • Constraints matching
    • Custom callback to verify the expected arguments using your own code
  • Setting actions on methods, return spesific value, or throw an exception.

Why reinvent the wheel?

  • Other frameworks, such as NMock, NMock2 or TypeMock.Net uses string based approach for mocking call. This means that you cannot use the compiler and most refactoring tools in order to catch any mistakes until runtime, if then. After getting burned more than once by this, I decided that I want a mocking framework that would use the objects, and not strings, in order to set up expectations on the mocked object.
  • Why not use EasyMock.Net, then?
    • EasyMock.Net is a port of a Java library, and it uses Remoting proxies to do its work.
    • I didn't like the API that EasyMock.Net had, it had too much roots in Java to be comfortable to work with in .Net
    • I found several places where a the Remoting proxies were not behaving exactly like the real objects. Specifically, they couldn't be copied using Array.Copy();
    • Using remoting proxies limits me to mocking interfaces or classes that inherits from MarshalByRefObject.