Migrating from Moq
The MockolateM001 code fix rewrites new Mock<T>(…) to T.CreateMock(…), changes the declared type from Mock<T> to T, drops every .Object access and moves setups and verifications to Mock.Setup and Mock.Verify:
Mock<IChocolateDispenser> dispenser = new Mock<IChocolateDispenser>();
dispenser.Setup(m => m.Dispense("Dark", It.IsAny<int>())).Returns(true);
dispenser.Setup(m => m.Dispense("Milk", It.IsAny<int>())).Returns(false);
VendingMachine sut = new VendingMachine(dispenser.Object);
sut.Buy("Dark", 4);
sut.Buy("Milk", 2);
dispenser.Verify(m => m.Dispense("Dark", 4), Times.Once());
dispenser.Verify(m => m.Dispense("Milk", It.IsAny<int>()), Times.Once());
dispenser.Verify(m => m.Dispense("White", It.IsAny<int>()), Times.Never());
Supported Moq constructs
The tables list the constructs the code fix rewrites. Everything else needs manual review. The examples use the IChocolateDispenser interface from the migration overview and the VendingMachine from Getting started; the code fix keeps your variable names, so the mock keeps its name and sut stays the system under test.
Creating mocks
| Moq | Mockolate |
|---|---|
new Mock<T>() | T.CreateMock() |
new Mock<T>(MockBehavior.Loose) | T.CreateMock() |
new Mock<T>(MockBehavior.Strict) | T.CreateMock(Mockolate.MockBehavior.Default.ThrowingWhenNotSetup()) |
mock.Object | mock (the .Object access is dropped) |
The mock is created on the mocked type, and every .Object access is dropped, because the mock already implements the interface:
Mock<IChocolateDispenser> dispenser = new Mock<IChocolateDispenser>();
VendingMachine sut = new VendingMachine(dispenser.Object);
IChocolateDispenser dispenserObject = dispenser.Object;
Strict mocks get a behavior that throws when no setup matches, written as Mockolate.MockBehavior because Moq has a MockBehavior as well:
Mock<IChocolateDispenser> dispenser = new Mock<IChocolateDispenser>(MockBehavior.Strict);
dispenser.Setup(m => m.Dispense("Dark", 1)).Returns(true);
Setups
| Moq | Mockolate |
|---|---|
mock.Setup(m => m.Method(args)) | mock.Mock.Setup.Method(args) |
mock.Setup(m => m.Prop), mock.SetupGet(m => m.Prop) | mock.Mock.Setup.Prop |
mock.SetupProperty(m => m.Prop) | mock.Mock.Setup.Prop.Register() |
mock.SetupProperty(m => m.Prop, value) | mock.Mock.Setup.Prop.InitializeWith(value) |
mock.SetupSequence(…).Returns(a).Returns(b) | mock.Mock.Setup.Method(…).Returns(a).Returns(b).Returns(default(T)).Forever() |
mock.SetupSequence(…).Pass().Throws(e) (void method) | mock.Mock.Setup.Method(…).DoesNotThrow().Throws(e).OnlyOnce() |
.Returns(…), .ReturnsAsync(…), .Throws(…) | kept on the Mockolate setup |
.Callback(…) | .Do(…) |
mock.InSequence(sequence).Setup(…) | mock.Mock.Setup.Method(…) (the InSequence wrapper is removed) |
Callback becomes Do, and a SetupSequence becomes a setup with the same chain of Returns, which Mockolate uses as a sequence. A Mockolate sequence starts over after its last step, while Moq returns the default value, so the code fix ends the chain with .Returns(default(T)).Forever():
Mock<IChocolateDispenser> dispenser = new Mock<IChocolateDispenser>();
int dispensed = 0;
dispenser.Setup(m => m.Dispense("Dark", It.IsAny<int>()))
.Callback((string type, int amount) => dispensed += amount)
.Returns(true);
dispenser.SetupSequence(m => m.Dispense("Milk", It.IsAny<int>()))
.Returns(true)
.Returns(false);
dispenser.SetupProperty(m => m.TotalDispensed, 10);
Verifications
| Moq | Mockolate |
|---|---|
mock.Verify(m => m.Method(args), Times.Once()) | mock.Mock.Verify.Method(args).Once() |
mock.Verify(m => m.Method(args)) | mock.Mock.Verify.Method(args).AtLeastOnce() |
mock.VerifyGet(m => m.Prop, times) | mock.Mock.Verify.Prop.Got() with the count |
mock.VerifySet(m => m.Prop = value, times) | mock.Mock.Verify.Prop.Set(value) with the count |
A non-literal Times argument (e.g. a variable) | the Moq call is kept, with a // TODO comment |
The Times argument becomes a method call at the end of the verification, and a verification without Times checks for at least one call:
Mock<IChocolateDispenser> dispenser = new Mock<IChocolateDispenser>();
dispenser.Verify(m => m.Dispense("Dark", 4));
dispenser.Verify(m => m.Dispense("Milk", It.IsAny<int>()), Times.Exactly(2));
dispenser.Verify(m => m.Dispense("White", It.IsAny<int>()), Times.AtMost(3));
dispenser.VerifyGet(m => m.TotalDispensed, Times.Once());
dispenser.VerifySet(m => m.TotalDispensed = 10, Times.Never());
Argument matchers
| Moq | Mockolate |
|---|---|
It.IsAny<T>() | It.IsAny<T>() |
It.Is<T>(predicate) | It.Satisfies<T>(predicate) |
It.IsInRange(a, b, Range.Inclusive) | It.IsInRange(a, b) |
It.IsRegex(pattern) | It.Matches(pattern).AsRegex().CaseSensitive() |
It.IsRegex(pattern, options) | It.Matches(pattern).AsRegex(options).CaseSensitive() |
It.Ref<T>.IsAny, out parameters | It.IsAnyRef<T>(), It.IsOut(() => value) |
It.IsAny stays as it is, while It.Is with a predicate becomes It.Satisfies and It.IsRegex becomes It.Matches(…).AsRegex(…). It.Matches ignores the case by default, so the code fix appends CaseSensitive() to match the case like Moq does; a RegexOptions.IgnoreCase option still applies. See parameter matching for the Mockolate matchers.
Mock<IChocolateDispenser> dispenser = new Mock<IChocolateDispenser>();
dispenser.Setup(m => m.Dispense(It.Is<string>(type => type.StartsWith("D")), It.IsAny<int>())).Returns(true);
dispenser.Setup(m => m.Dispense(It.IsRegex("^M"), It.IsAny<int>())).Returns(false);
Events
| Moq | Mockolate |
|---|---|
mock.Raise(m => m.Event += null, eventArgs) | mock.Mock.Raise.Event(null, eventArgs) |
mock.Raise(m => m.Event += null, args) (custom delegate) | mock.Mock.Raise.Event(args) |
mock.VerifyAdd(m => m.Event += …, times) | mock.Mock.Verify.Event.Subscribed() with the count |
mock.VerifyRemove(m => m.Event -= …, times) | mock.Mock.Verify.Event.Unsubscribed() with the count |
Raising an event goes through Mock.Raise, and VerifyAdd and VerifyRemove verify the subscriptions:
Mock<IChocolateDispenser> dispenser = new Mock<IChocolateDispenser>();
dispenser.Raise(m => m.ChocolateDispensed += null, "Dark", 2);
dispenser.VerifyAdd(m => m.ChocolateDispensed += It.IsAny<ChocolateDispensedDelegate>(), Times.Once());
dispenser.VerifyRemove(m => m.ChocolateDispensed -= It.IsAny<ChocolateDispensedDelegate>(), Times.Never());
Nested mocks
| Moq | Mockolate |
|---|---|
mock.Setup(m => m.Child.Prop) | mock.Child.Mock.Setup.Prop and a // TODO comment |
Moq creates the mock for Child automatically, Mockolate does not; set up the parent to return a mock for Child yourself (see what needs manual review).
Behavioral differences
The migrated code compiles, but behaves differently from Moq in these cases:
- Class mocks call the base class. A Moq mock calls the base class implementation only with
CallBase = true, while a Mockolate class mock calls it for virtual members by default, even when a setup matches. To keep the Moq behavior, passMockolate.MockBehavior.Default.SkippingBaseClass()toCreateMock(see customizing mock behavior). - Setups no longer check the call order. The code fix removes the
InSequence(…)wrapper. Mockolate checks the order after the calls instead: add aThen(…)chain to the verification. - Failure messages are dropped.
mock.Verify(m => m.Method(args), "message")becomesmock.Mock.Verify.Method(args).AtLeastOnce()without the message.