Skip to content

Instantly share code, notes, and snippets.

@AlbertoMonteiro
Last active January 23, 2024 10:11
Show Gist options
  • Star 40 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlbertoMonteiro/daeab549df57727ddaa7 to your computer and use it in GitHub Desktop.
Save AlbertoMonteiro/daeab549df57727ddaa7 to your computer and use it in GitHub Desktop.
Migration from Moq to NSubstitute
Copyright 2015-2023 Alberto Monteiro.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Migration from Moq to NSubstitute

This code is licensed under the terms of the MIT license

Using

  • Find: using Moq;
  • Replace: using NSubstitute;

Instance

  • Find: new Mock<(.+?)>\((.*?)\)
  • Replace: Substitute.For<$1>($2)

Instance

  • Find: \bMock<(.+?)>
  • Replace: $1

Setup

  • Find: (?<!\.)\b(\w+)(\s\n\s*)?\.Setup(Get)?\((\w+) => \4(\.?.+?)\)(?=\.R|\s\n)
  • Replace: $1$5
  • Find: \.Get<(.+?)>\(\)\.Setup\((\w+) => \2(\.?.+?)\)(?=\.R|\s\n)
  • Replace: .Get<$1>()$3
  • Find: \.Get<(.+?)>\(\)\.SetupSequence?\((\w+) => \3(\.?.+?)\)(?=\.R|\s\n)
  • Replace: .Get<$1>()$3
  • Find: (?<!\.)\b(\w+)(\s\n\s*)?\.SetupSequence?\((\w+) => \3(\.?.+?)\)(?=\.R|\s\n)
  • Replace: $1$4
  • Find: \.Get<(.+?)>\(\)\.SetupSequence?\((\w+) => \2(\.?.+?)(\)(?!\)))
  • Replace: .Get<$1>()$3

Verify

  • Find: (?<!\.)\b(\w+)\.Verify\((\w+) => \2(.+?), Times\.(Once(\(\))?|Exactly\((?<times>\d+)\))\)
  • Replace: $1.Received(${times})$3
  • Find: (?<!\.)\b(\w+)\.Verify\((\w+) => \2(.+?), Times\.Never\)
  • Replace: $1.DidNotReceive()$3

Throw

  • Find: (?<!\.)\b(\w+)(\s\n\s*)?\.Setup\(((\w+) => \4(\..?.+?)\))\)\s*\n*\.Throws
  • Replace: $1.When($3).Throw

Arg

  • Find: It.IsAny
  • Replace: Arg.Any
  • Find: It.Is
  • Replace: Arg.Is

MockingKernel

  • Find: MoqMockingKernel
  • Replace: NSubstituteMockingKernel

MockingKernel using

  • Find: using Ninject.MockingKernel.Moq;
  • Replace: using Ninject.MockingKernel.NSubstitute;

MockingKernel GetMock

  • Find: \.GetMock<(.+?)>\(\)
  • Replace: .Get<(.+?)>()
@StephanMoeller
Copy link

Thank you for this!!!

@martincostello
Copy link

It.Is => Arg.Is might also be a useful addition.

@AlbertoMonteiro
Copy link
Author

Tyvm for suggestion @martincostello

@jamescanady
Copy link

Suggestion on a replacement for ReturnsAsync and ThrowsAsync?

@martincostello
Copy link

I've been writing some looks like Moq extension methods for one of my codebases to make it less burdensome to migrate. This might help you @jamescanady:

using System.Linq.Expressions;
using NSubstitute.Core;
using NSubstitute.ExceptionExtensions;

namespace NSubstitute;

/// <summary>
/// This class acts as a bridge from Moq to NSubstitute to allow it to be removed without too much code churn.
/// </summary>
public static class NSubstituteExtensions
{
    public static ConfiguredCall ReturnsAsync<T>(this Task<T> value, T returnThis, params T[] returnThese)
    {
        var task = Task.FromResult(returnThis);
        var tasks = Array.Empty<Task<T>>();

        if (returnThese?.Length > 0)
        {
            tasks = returnThese.Select((p) => Task.FromResult(p)).ToArray();
        }

        return value.Returns(task, tasks);
    }

    public static ConfiguredCall ThrowsAsync<T>(this Task<T> value, Exception exception)
    {
        return value.Throws((_) => throw exception);
    }
}

I'm still working on it, so not fully tested yet as it's still a WIP.

@VisualBean
Copy link

VisualBean commented Aug 10, 2023

Suggestion on a replacement for ReturnsAsync and ThrowsAsync?

Just return Task.FromResult and you will be fine

@abdevnet
Copy link

I've been writing some looks like Moq extension methods for one of my codebases to make it less burdensome to migrate. This might help you @jamescanady:

using System.Linq.Expressions;
using NSubstitute.Core;
using NSubstitute.ExceptionExtensions;

namespace NSubstitute;

/// <summary>
/// This class acts as a bridge from Moq to NSubstitute to allow it to be removed without too much code churn.
/// </summary>
public static class NSubstituteExtensions
{
    public static ConfiguredCall ReturnsAsync<T>(this Task<T> value, T returnThis, params T[] returnThese)
    {
        var task = Task.FromResult(returnThis);
        var tasks = Array.Empty<Task<T>>();

        if (returnThese?.Length > 0)
        {
            tasks = returnThese.Select((p) => Task.FromResult(p)).ToArray();
        }

        return value.Returns(task, tasks);
    }

    public static ConfiguredCall ThrowsAsync<T>(this Task<T> value, Exception exception)
    {
        return value.Throws((_) => throw exception);
    }
}

I'm still working on it, so not fully tested yet as it's still a WIP.

This is great stuff. Thank you!

@Creastoff
Copy link

Creastoff commented Aug 10, 2023

.Net CLI which modifies all .cs files in a specified directory using the patterns above.
https://github.com/Creastoff/Moqstitute

@AlbertoMonteiro I have checked for a license on this gist but could not see one - if you add one I will review the usage in the repo. I have also added you as a collaborator to the repo.

@AlbertoMonteiro
Copy link
Author

Amazing job @Creastoff.
About the license, idk how to add it on a gist, if I discover how to it will be MIT license

@Creastoff
Copy link

@AlbertoMonteiro I'm afraid I cannot offer any advice on that - it was just in case you did not wish for this to be used in public tooling. As you have stated you would add an MIT license I will not review the usage in the repo.

(In case you can't tell, I have no idea what licenses are available besides MIT & GNU)

@AlbertoMonteiro
Copy link
Author

@Creastoff I've done a small research and I added the comment in the file also added MIT description as other file in this gist

@waznico
Copy link

waznico commented Aug 11, 2023

@AlbertoMonteiro thanks for your work.
In my case I also had to do the following (maybe you could add it to the gist?):

Mock Object

  • Find: \.Object[,]
  • Replace: ,
  • Find: \.Object[\)]
  • Replace: )
  • Find: \.Object[\.]
  • Replace: .

Returns

  • Find: \.ReturnsAsync\((\.?.+?)\)
  • Replace .Returns(Task.FromResult($1))

Exceptions

  • Find: \.ThrowsAsync\((\.?.+?)\)
  • Replace .Returns(Task.FromException($1))
  • Find: \.Throws\((\.?.+?)\)
  • Replace .Returns($1)

Setup

  • Find: \.As<IQueryable<(\.?.+?)>>\((\.?.+?)\)
  • Replace .AsQueryable<$1>($2)

@martincostello
Copy link

I've put the "Fake Moq" implementation code I wrote in this Gist. It only has as much as I needed for the codebase I was migrating, but it should be a starting point people might find useful.

@Creastoff
Copy link

Creastoff commented Aug 11, 2023

@AlbertoMonteiro For better management of this list of issues, may I suggest that this is converted into a repository?

Some items I have discovered which I have not had time to resolve yet are in my repository labelled with 'regex': https://github.com/Creastoff/Moqstitute/issues?q=is%3Aissue+is%3Aopen+label%3Aregex

@AlbertoMonteiro
Copy link
Author

AlbertoMonteiro commented Aug 11, 2023

@Creastoff
Te repo link

https://github.com/AlbertoMonteiro/moq-to-nsubstitute/tree/main

@waznico do you mind doing a PR on this repo?

@jeffputz
Copy link

This is pretty great. I, however, am pretty terrible with regex and can't come up with one to handle Callback, which is tricky to reliably find.

@Creastoff
Copy link

Creastoff commented Aug 12, 2023

This is pretty great. I, however, am pretty terrible with regex and can't come up with one to handle Callback, which is tricky to reliably find.

@jeffputz you can subscribe to the following issue for eventual resolution AlbertoMonteiro/moq-to-nsubstitute#3

@PrestigiousP
Copy link

I had to add:

  • Find: Mock.Of<(.+?)>\((.*?)\)
  • Replace: Substitute.For<$1>($2)

@KretschiGL
Copy link

KretschiGL commented Aug 31, 2023

Be aware of regex. It.Is killed me 😩
Had to use

  • Find: It\.Is< otherwise had too many matches
  • Find: It\.IsAny

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment