views:

441

answers:

3

Can any one suggest a step by step example for using moQ framework.

any guidelines or thumbrules that has to be followed while mocking objetcs . can be much help.

thanks.

+3  A: 

Here's the moq quick-start

Update: To address your comment... A large part of writing testable code involves removing dependencies on classes/resources outside the scope of your control. A very common approach to doing this is by talking to interfaces instead of concrete examples.

It's a little much to describe properly (especially since I just rolled out of bed), so let suggest you pick up a copy of Roy Osherove's 'The Art of Unit Testing'. It's a fairly short book and is filled with good advice and lots of summary information to get you familiar with many of the approaches to unit testing.

STW
In the test case i'm trying to call a method (Which is implemented and programmed to throw an exception) using interface instance. But the test still passes . what does the Setup method acctually do here .(below is the sample code).[Test] public void Empty() { IQueue Qref; Mock<IQueue> MockObj= new Mock<IQueue>(); MockObj.Setup(x => x.IsEmpty).Returns(true); Assert.IsTrue(MockObj.Object.IsEmpty); }
vijaysylvester
I think that would best be asked in a new question, I'm only a somewhat familiar with moq and putting it in a new question will get developers more familiar with the framework to look at it.
STW
+1  A: 

I only started to use Moq recently and I am not sure how much help this will be but if you can get your hands on Chapter 3 of Pro ASP.NET MVC Framework there is a really good step-by-step example of using moq and NUnit with ASP.NET MVC.

Skittles
A: 

Finally i found a convincing step by step example from the link below.The article is explained using a problem - solution approach

http://devpinoy.org/blogs/cruizer/archive/2007/04/03/test-first-demo-developing-a-user-login-facility-part-1.aspx

vijaysylvester