-
-
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<(.+?)>()
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!
.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.
Amazing job @Creastoff.
About the license, idk how to add it on a gist, if I discover how to it will be MIT license
@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)
@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
@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
Just return
Task.FromResult
and you will be fine