|
|
QuickStart
Introduction to Moq
Moq is intended to be simple to use, strong typed (no magic strings!, and therefore full compiler-verified and refactoring-friendly), minimalistic and with a slight bias towards state/classic-testing oriented.
Examples
// ShouldExpectCallReturn var mock = new Mock<ICloneable>(); var clone = new object(); mock.Expect(x => x.Clone()).Returns(clone); Assert.AreSame(clone, mock.Object.Clone());
// ShouldExpectCallWithArgument var mock = new Mock<IFoo>(); mock.Expect(x => x.DoInt(1)).Returns(11); mock.Expect(x => x.DoInt(2)).Returns(22); Assert.AreEqual(11, mock.Object.DoInt(1)); Assert.AreEqual(22, mock.Object.DoInt(2));
// ShouldExpectCallWithReferenceLazyEvaluate
int a = 25;
var mock = new Mock<IFoo>();
mock.Expect(x => x.DoArgument(a.ToString())).Returns(() => a);
a = 10;
Assert.AreEqual(10, mock.Object.DoArgument("10"));// ShouldExpectReturnPropertyValue var mock = new Mock<IFoo>(); mock.Expect(x => x.ValueProperty).Returns(25); Assert.AreEqual(25, mock.Object.ValueProperty);
// ShouldExpectMethodCallWithVariable int value = 5; var mock = new Mock<IFoo>(); mock.Expect(x => x.Duplicate(value)).Returns(() => value * 2); Assert.AreEqual(value * 2, mock.Object.Duplicate(value));
// ShouldMatchAnyArgument var mock = new Mock<IFoo>(); mock.Expect(x => x.Duplicate(It.IsAny<int>())).Returns(() => 5); Assert.AreEqual(5, mock.Object.Duplicate(5)); Assert.AreEqual(5, mock.Objcet.Duplicate(25));
// ShouldMatchPredicateArgument var mock = new Mock<IFoo>(); mock.Expect(x => x.Duplicate(It.Is<int>(value => value < 5 && value > 0))).Returns(() => 1); Assert.AreEqual(1, mock.Object.Duplicate(3)); Assert.AreEqual(0, mock.Object.Duplicate(0));
// ShouldExpectCallWithoutReturnValue var mock = new Mock<IFoo>(); mock.Expect(x => x.Execute()); mock.Object.Execute();
// ShouldThrowIfExpectingThrows var mock = new Mock<IFoo>(); mock.Expect(x => x.Do1()).Throws(new FormatException()); mock.Object.Do1();
// ShouldExecuteCallbackWhenVoidMethodIsCalled var mock = new Mock<IFoo>(); bool called = false; mock.Expect(x => x.Execute()).Callback(() => called = true); mock.Object.Execute(); Assert.IsTrue(called);
// ShouldExecuteCallbackWhenNonVoidMethodIsCalled var mock = new Mock<IFoo>(); bool called = false; mock.Expect(x => x.Do1()).Callback(() => called = true).Returns(1); Assert.AreEqual(1, mock.Object.Do1()); Assert.IsTrue(called);
// ShouldExpectRanges var mock = new Mock<IFoo>(); mock.Expect(x => x.DoInt(It.IsInRange(1, 5, Range.Inclusive))).Returns(1); mock.Expect(x => x.DoInt(It.IsInRange(6, 10, Range.Exclusive))).Returns(2); Assert.AreEqual(1, mock.Object.DoInt(1)); Assert.AreEqual(1, mock.Object.DoInt(2)); Assert.AreEqual(1, mock.Object.DoInt(5)); Assert.AreEqual(2, mock.Object.DoInt(7)); Assert.AreEqual(2, mock.Object.DoInt(9));
Note: the required mock.Object accessor is a consequence of a C# compiler restriction (vote to get it removed at Microsoft Connect)
Head on to the API documentation, download it and have fun! Engage in the discussion group to give us feedback, share your experiences or wishes for vNext!
Sign in to add a comment

I think these examples will be confusing to TDD newcomers because they don't show the mocks being used in actual tests. This shows the Moq API really well, but doesn't provide examples of how people would really use them in tests.
These example are not straight forward and are confusing to new TDD developers. If you can provide the implementation code with sample test code then that would make it lot easier to understand. I do not find them simpler than nmock by just looking at it.
Both very good and valid points. This "quickstart" was put together really quick back when we did the first release, and it wasn't structured as a quickstart actually, just a dump of the unit tests for MoQ itself.
We realize we're lacking in documentation (not API documentation specifically, but general documentation), and we'll try to improve that in the future.
Rather than providing general definitions of what is mocking in general, we should point to existing material on the web about that, but we should definitely put MoQ in that context.
I made an article about Unit Testing and usage of MoQ. It contains a quite complete example on how to utilize MoQ in Testing/TDD scenario.
It does not cover all features of MoQ, nevertheless might serve as a good starting point. Article is in german only :-/. Hope this helps a little anyway.