Last active
August 14, 2024 13:48
-
-
Save AlbertoMonteiro/daeab549df57727ddaa7 to your computer and use it in GitHub Desktop.
Migration from Moq to NSubstitute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This code is licensed under the terms of the MIT license
- Find:
using Moq;
- Replace:
using NSubstitute;
- Find:
new Mock<(.+?)>\((.*?)\)
- Replace:
Substitute.For<$1>($2)
- Find:
\bMock<(.+?)>
- Replace:
$1
- 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
- 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
- Find:
(?<!\.)\b(\w+)(\s\n\s*)?\.Setup\(((\w+) => \4(\..?.+?)\))\)\s*\n*\.Throws
- Replace:
$1.When($3).Throw
- Find:
It.IsAny
- Replace:
Arg.Any
- Find:
It.Is
- Replace:
Arg.Is
- Find:
MoqMockingKernel
- Replace:
NSubstituteMockingKernel
- Find:
using Ninject.MockingKernel.Moq;
- Replace:
using Ninject.MockingKernel.NSubstitute;
- Find:
\.GetMock<(.+?)>\(\)
- Replace:
.Get<(.+?)>()
@Creastoff
Te repo link
https://github.com/AlbertoMonteiro/moq-to-nsubstitute/tree/main
@waznico do you mind doing a PR on this repo?
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.
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
I had to add:
- Find:
Mock.Of<(.+?)>\((.*?)\)
- Replace:
Substitute.For<$1>($2)
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
@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