Migration from Moq to FakeItEasy with Resharper Search Patterns

In most of the projects in our company we used the mocking framework Moq. Nowadays we are moving away from Moq to FakeItEasy because FakeItEasy is just faking amazing! Seriously, FakeItEasy provides us a cleaner and more readable syntax. It just has the concept of fakes, no need to call .Object everywhere and many more advantages. Our rule of thumb is that we simply apply the boy-scout rule for the migration process from Moq to FakeItEasy. This means if a developers touches a unit test, which contains Moq Mocks, then he quickly rewrites it to FakeItEasy. But doing that manually is tedious and error prone. Why not use Resharper’s powerful search and replace patterns to rewrite our tests almost automatically? Indeed this can be done and I’ll show you how you achieve it with Resharper.

Resharper has the possibility to organize all Resharper settings in layers and even share them with your team. This allows us to commit important Resharper settings such as the search and replace patterns for the migration from Moq to FakeItEasy into our source control system. Let’s start with a simple test and its production code:

    public interface IDependency
    {
        string Value { get; }

        int Bar(string foo);
    }

    public class Subject
    {
        private readonly IDependency dependency;

        public Subject(IDependency dependency)
        {
            this.dependency = dependency;
        }

        public int Foo()
        {
            string value = this.dependency.Value;

            string inputForBar = string.Format("{0} {1}", value, "Subject");

            return 2 * this.dependency.Bar(inputForBar);
        }
    }

A test for that behavior could look like (using NUnit, FluentAssertions and Moq):

    [TestFixture]
    public class SubjectTest
    {
        private Mock dependency;

        private Subject testee;

        [SetUp]
        public void SetUp()
        {
            this.dependency = new Mock();

            this.testee = new Subject(this.dependency.Object);
        }

        [Test]
        public void Test()
        {
            this.dependency.Setup(x => x.Value).Returns("Dependency");
            this.dependency.Setup(x => x.Bar(It.IsAny())).Returns(4);

            var result = this.testee.Foo();

            this.dependency.Verify(x => x.Bar("Dependency Subject"));
            result.Should().Be(8);
        }
    }

I have created a gist which contains Moq to FakeItEasy search and replace patterns for Resharper. We need to create a settings file and import the patterns into the settings file.

Resharper > Manage Options

ReSharper_Settings_Layers

Right click on solution team shared…


ReSharper_Settings_Layers_Import

Import the file downloaded from the gist above.

File_Solution_MoqToFakeItEasy_team_shared

After the successful import you can double click on the team-shared solution layer and navigate to the custom patterns. You should see something like the following screenshot:

Options_CustomPatterns

You can now tweak the warning level and even modify or add new patterns. Let us go back into our test and see what effect this has:

MoqFieldReplace
You can now replace the field declaration with Alt+Enter.
MoqFieldAssignReplace

Also the field assignment can be replaced with Alt+Enter.

MoqObjectReplace

It removes the annoying .Object call with Alt+Enter.

MoqSetupWithReturnsReplace1

It allows to replace property setups with Alt+Enter.

MoqItIsAnyReplace

Replace It.IsAny calls with Alt+Enter.

MoqVerifyReplace

And of course Verify calls with Alt+Enter.

After that Alt+Enter spree the test above looks like:

    [TestFixture]
    public class SubjectTest
    {
        private IDependency dependency;

        private Subject testee;

        [SetUp]
        public void SetUp()
        {
            this.dependency = A.Fake();

            this.testee = new Subject(this.dependency);
        }

        [Test]
        public void Test()
        {
            A.CallTo(() => this.dependency.Value).Returns("Dependency");
            A.CallTo(() => this.dependency.Bar(A._)).Returns(4);

            var result = this.testee.Foo();

            A.CallTo(() => this.dependency.Bar("Dependency Subject")).MustHaveHappened();
            result.Should().Be(8);
        }
    }

Fancy isn’t it? Wait it gets even fancier.

If you want to get an overview of all the issues in the solution just goto Resharper > Inspect > Code Issues in Solution. This analyzes your whole solution:

CodeIssuesInSolution

But that is not all! If you go back into the Manage Options dialog and click on one of the custom patterns and then search, it will allow to replace all occurrences of a given pattern in the whole solution!

SearchForPattern

Replace_with_Pattern

Happy semi-automatic-boy-scouting :D!

Pss: In case you missed the gist link. Here it is again. Please, if you develop other search and replace patterns for the migration from Moq to FakeItEasy then please provide a pull request on the gist!

About the author

Daniel Marbach

18 comments

Recent Posts