-
-
Save AlbertoMonteiro/daeab549df57727ddaa7 to your computer and use it in GitHub Desktop.
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<(.+?)>()
@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)
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.
@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
@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
@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