Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Moq in tests but suggested text was NSubstitute #14

Open
rpbeukes opened this issue Jun 14, 2022 · 1 comment
Open

Using Moq in tests but suggested text was NSubstitute #14

rpbeukes opened this issue Jun 14, 2022 · 1 comment

Comments

@rpbeukes
Copy link

rpbeukes commented Jun 14, 2022

Scenario

In my VS2022 test project, I have Moq V4.16.1 installed.
I do not have NSubstitude installed.
I discovered that my test project does reference a package Teststack.Dossier V4.0.0, and that has a dependency on NSubstitute

That is where things got interesting :)

In the MoqIsAnyCompletion.cs

...
var hasMoqReferenced = context.Document.Project.MetadataReferences.Any(r => r.Display.Contains(this._provider.AssemblyName));
...

and in NSubstituteArgAnyCompletion.cs

...
var hasNSubstituteReferenced = context.Document.Project.MetadataReferences.Any(r => r.Display.Contains(this._provider.AssemblyName));
...

both those flags returned true, and then my suggestions somehow default to Args.Any instead of It.Any.

Seems like whoever is last, wins...not too sure.

Anyway, I found a way to make it work, but of course, I only looked at it from my point of view and my project. I want your input too, please 😊.

Suggestion

Read the project .csproj file to determine the packages installed by the developer, and ignore the sub-dependencies.

public static class PackagesHelper
  {
// btw, feel free to refactor, just a quick test
        public static IEnumerable<string> GetProjectNugetPackages(Project project)
        {
            var nugetPackages = new List<string>();
            var csproj = new XmlDocument();
            csproj.Load(project.FilePath);
            var nodes = csproj.SelectNodes("//PackageReference[@Include and @Version]");
            foreach (XmlNode packageReference in nodes)
            {
                var packageName = packageReference.Attributes["Include"].Value;
                nugetPackages.Add(packageName);
            }

            return nugetPackages;
        }
   }

In the MoqIsAnyCompletion.cs

...
var hasMoqReferenced = PackagesHelper.GetProjectNugetPackages(context.Document.Project).Any(x => x.Equals(this._provider.AssemblyName, StringComparison.InvariantCultureIgnoreCase));
...

and in NSubstituteArgAnyCompletion.cs

...
var hasNSubstituteReferenced = PackagesHelper.GetProjectNugetPackages(context.Document.Project).Any(x => x.Equals(this._provider.AssemblyName, StringComparison.InvariantCultureIgnoreCase));
...

Let me know your thoughts.

PS: Like the project, THAXN! 😊

rpbeukes added a commit to rpbeukes/Mocking.Helpers that referenced this issue Jun 16, 2022
Fix incorrect code suggestions.

Related to this [issue](MrLuje#14)

Might not be the best fix, but it works for my scenario.
@rpbeukes
Copy link
Author

Here is my solution, so I can use this little tool daily.
Hopefully, it might help someone too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant