Skip to content

Instantly share code, notes, and snippets.

@AlbertoMonteiro
Last active August 14, 2024 13:48
Show Gist options
  • 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<(.+?)>()
@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

@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