I'm new with MSTest (Visual Studio 2010). Is there any integrated mocking framework that comes with VS2010? Or is there any other good mocking framework that's easy to pick up that I could use?
A mock object is an object that can act as a real object but can be controlled in test code. Moq is a library that allows us to create mock objects in test code. It is also available in NuGet. This library also supports .
The three key steps to using mock objects for testing are: Use an interface to describe the object. Implement the interface for production code. Implement the interface in a mock object for testing.
Trying to mock a method that is called within another method. // code part public virtual bool hello(string name, int age) { string lastName = GetLastName(); } public virtual string GetLastName() { return "xxx"; } // unit test part Mock<Program> p = new Mock<Program>(); p. Setup(x => x. GetLastName()).
Mock is an object to simulates the behavior of a real method/object in a controlled environment for unit testing.
The VS 2010 doesn't come with mocking framework. But you can use either Rhino Mocks (supports .NET 2.0 and 3.5), or Moq (supports .NET 3.5 and .NET 4).
Both frameworks are good, but I prefer Moq, since it feels more natural to me. It uses fluent lambda syntax which feels very clean to me once you get used to it.
Both frameworks are open source.
If you want a commercial one, go with Typemock isolator. This framework can mock virtually anything but not free.
you may want to take a look at pex and moles. I haven't really played with it but if you want to stay faithful to MS tools, take a stab at it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With