Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm new to use mocking frameworks and I have a few questions on the things that I am not clear on. I'm using Rhinomocks to generate mock objects in my unit tests. I understand that mocks can be created to verify interactions between methods and they record the interactions etc and stubs allow you to setup data and entities required by the test but you do not verify expectations on stubs.

Looking at the recent unit tests I have created, I appear to be creating mocks literally for the purpose of stubbing and allowing for data to be setup. Is this a correct usage of mocks or is it incorrect if you're not actually calling verify on them?

For example:

user = MockRepository.GenerateMock<User>();
        user.Stub(x => x.Id = Guid.NewGuid());
        user.Stub(x => x.Name = "User1");

In the above code I generate a new user mock object, but I use a mock so I can stub the properties of the user because in some cases if the properties do not have a setter and I need to set them it seems the only way is to stub the property values. Is this a correct usage of stubbing and mocking?

Also, I am not completely clear on what the difference between the following lines is:

user.Stub(x => x.Id).Return(new Guid());

user.Stub(x => x.Id = Guid.NewGuid());
share|improve this question
5  
    
    
new Guid() creates a guid with all zeros and Guid.NewGuid() creates a guid with a unique value. –  kirk.burleson Jul 3 '13 at 0:46
    
Use intention revealing names in your object variables. E.g. mockedUser, dummyUser, stubUser and fakeUser –  Joppe Jul 4 '13 at 16:57
add comment

2 Answers

up vote 2 down vote accepted

Mocks and stubs are abstract concepts. Looking at the docs you can find a way to create a Stub:

MockRepository.GenerateStub<...>

But it seems that they also decided to make it convenient to stub a method of a Mock, like you're doing.

Greg Moeck made a great presentation about the use of mocks: http://www.youtube.com/watch?v=R9FOchgTtLM

share|improve this answer
add comment

I am taking the help from these mentioned links and will modify accordingly as per my understanding:

Good links:

Good explanation by Jim
Explanation by martin frowler
Informative Link, but a little advanced (talks about integration testing)
Ok before going forward there are two things when You are Unit Testing:

  1. Sate Testing
  2. Behaviour Testing

In state testing You are only checking whether the actual method or System Under Test(SUT) is returning the correct value

In behaviour testing You check whether the correct method was called, or in other words whether a correct sequence of methods were called.

Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside the test method.Here You generally do State testing.

Mocks are used to verify interaction between your SUT and its dependencies.You generally do behavior testing here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.