简体   繁体   中英

some questions on unit tests and rhino mocks

I am new to writing unit tests so learning as I go along. I am using Rhino Mocks with MsTest frameworks and have some questions.

Where can I find some documentation on rhino mocks, outlining its features with explanations, so I can print off and examine?

I have seen numerous examples of the 2 following techniques regarding expectations, which is the better or more apt for a particular situation?

Expect.Call( delegate { mockTestClass.MethodToMock(param) } ).Return(true);

or

var mockTestClass = MockRepository.GenerateMock<TestClass>();

mockTestClass.Expect( m => m.MethodToMock(param) ).Return( true );

Next I am wondering what the difference between stubbing and mocking a class is and what should be used in which scenarios?

Thanks for any help.

Where can I find some documentation on rhino mocks, outlining its features with explanations, so I can print off and examine?

Rhino Mocks docs are here and a good wiki is here . In general, be careful with anything mentioning versions 3.3 or lower, as it will be somewhat outdated.

which is the better or more apt for a particular situation?

The second way is more "fluent" and recommended.

what the difference between stubbing and mocking

Here are some resources about this:

BTW if you're just getting started with these concepts I think you'll be better off using NUnit and Moq. Here's an introduction with examples.

If you want to check that specific method was called and return value does not matter I would suggest using AAA syntax and AssertWasCalled() method:

// Arrange
var mock = MockRepository.GenerateMock<ICustomType>();
var service = new MyService(mock);

// Act
service.DoSomething();

// Assert 
// ensures that SomeMethod of the mock was called 
// whilst service.DoSomething() call
mock.AssertWasCalled(m => m.SomeMethod());

Difference between stubs and mocks from RhinoMocks framework perspectives are following:

  • Expectations can be set for both Mocks and Stubs but they are verified only for Mocks, for instance you would not be able specify Repeat().Any() and so on for stub
  • Stubs never fail when accessing methods or properties, all properties and methods return default(T) for given return type T , properties of Stub is normal properties

More details regarding difference between Mocks and Stubs see in a good set of links provided in the Mauricio's answer, I would suggest Martin Fowler's work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM