devlooped / moq

The most popular and friendly mocking framework for .NET
Other
5.94k stars 802 forks source link

Make Moq portable to .NET Core #168

Closed kuhlenh closed 7 years ago

kuhlenh commented 9 years ago

This is Kasey from the .NET team and we are trying to help authors make their libraries portable across different platforms (especially .NET Core).

.NET Core is a modular implementation of the .NET Framework that can run on any platform (Windows, Mac, Linux). Side-by-side hosting enables apps to run no matter what Framework version the host has installed (if any). Developers include their dependencies (.NET libraries and your libraries) in their app via NuGet packages. .NET Core is a great new option for web and cloud developers and is the same underlying .NET runtime and framework used by ASP.NET 5.

The .NET team has made many .NET Framework libraries available as part of .NET Core and ASP.NET 5. With that work largely done, we need more open source libraries to support .NET Core so that developers can easily both move existing apps and create new apps that run on .NET Core. Our goal is to make app developers successful, building these new apps that target .NET Core and that run on Windows, OS X and Linux.

Check out the .NET Portability Analyzer. This tool gives you a detailed report on how portable your library is across platforms and will sometimes provide recommendations. This is an easy way for you to assess the effort required to support .NET Core and other platforms with Moq.

Please @mention me in this issue to discuss porting Moq to .NET Core. I'm happy to help get you started. If you have broader questions or concerns, you can also file an issue on the dotnet/corefx repo.

Thanks, Kasey

jeremymeng commented 8 years ago

@redowl3 What version of dotnet/nuget.exe are you using? I took your project.json and ran dotnet restore but didn't get any errors. Also some comments:

redowl3 commented 8 years ago

Thanks for the response. I'm using 1.0.0-preview2-003121 and you're correct that does build, but we're trying to test an asp.net app that is using net461 (as we using unported 3rd party libraries) and although the project builds we cannot reference anything from the asp.net project to allow us to test.

{ "version": "1.0.0-*", "testRunner": "xunit", "dependencies": { "xunit": "2.2.0-beta2-build3300", "dotnet-test-xunit": "2.2.0-preview2-build1029", "Moq": "4.6.38-alpha" },

"frameworks": { "netcoreapp1.0": { "imports": [ "dnxcore50" ], "dependencies": { "Microsoft.NETCore.App": { "version": "1.0.0-", "type": "platform" } } }, "net461": { "dependencies": { "MyProjectNamespace.Web": "1.0.0-", } } } }

jeremymeng commented 8 years ago

@redowl3 could you please share a simple project/git repro that reproduces this issue?

redowl3 commented 8 years ago

Hi Jeremy, thanks again for getting back in touch. Not sure how to share a project but I've created an ASP.NET Core Web Application (.Net Framework) and then a Class Library (.Net Core) which has the project.json as above.

jeremymeng commented 8 years ago

@redowl3 you could publish the simple project to github.

gerryLowry commented 8 years ago

thank you to Daniel (kzu) for pointing me to this issue.

TL:DR (but i hope that you will)

Yesterday (2016-09-08) I discovered that the xUnit.net tests for ASP.NET Core 1.0 Identity are successfully using moq.

example: https://github.com/aspnet/Identity/blob/dev/test/Microsoft.AspNetCore.Identity.Test/UserManagerTest.cs

        [Fact]
        public async Task CreateCallsStore()
        {
            // Setup
            var store = new Mock<IUserStore<TestUser>>();
            var user = new TestUser { UserName = "Foo" };
            store.Setup(s => s.CreateAsync(user, CancellationToken.None)).ReturnsAsync(IdentityResult.Success).Verifiable();
            store.Setup(s => s.GetUserNameAsync(user, CancellationToken.None)).Returns(Task.FromResult(user.UserName)).Verifiable();
            store.Setup(s => s.SetNormalizedUserNameAsync(user, user.UserName.ToUpperInvariant(), CancellationToken.None)).Returns(Task.FromResult(0)).Verifiable();
            var userManager = MockHelpers.TestUserManager<TestUser>(store.Object);

            // Act
            var result = await userManager.CreateAsync(user);

            // Assert
            Assert.True(result.Succeeded);
            store.VerifyAll();
        }

MORE INFORMATION

[A] my current focus is on writing xUnit.net unit and integration tests directly against ASP.NET Core Identity data created via the AccountController the ASP.NET Core 1.0 Web API has generated.

ideally i would like to do this without mocking but have hit many roadblocks.
How to xUnit test against AccountController's actual ASP.NET Core 1.0 Identity tables without mocking?

[B] https://github.com/gerryLowry/EF_Core_testing_experiments/tree/master/RawCoreAPIxUnit

^^^^ one might find the above code is useful for experimenting. ^^^^

[C] many articles say it is possible to use moq, xUnit.net, and ASP.NET Core 1.0 together.

please see issue #284: moq breaks ASP.NET Core xUnit.net project for Core 1.0 Web API application #284

ideally, for more that just moq, it would be wonderful if one touches one's project.json and need not worry that everything will change state from working code to OMG

[D] FWIW, many articles regarding unit testing against the AccountController are somewhat trivial.

REFERENCES (expect imperfections):

Some to the challenges with unit testing .NET Core 1.0 is the newness of the RTM (2016.06.27).

GitHub ASP.NET Core Schedule and Roadmap

2016.06.27 https://blogs.msdn.microsoft.com/dotnet/2016/06/27/announcing-net-core-1-0/

http://stackoverflow.com/questions/29755101/unit-test-identitydbcontext

ASP.NET - Writing Clean Code in ASP.NET Core with Dependency Injection Steve Smith MSDN Magazine May 2016

Docs » MVC » Controllers » Testing Controller Logic Steve Smith

Caution ~~ back up your code before you try this technique THE POWER OF THE GLOBAL.JSON David Pine

MOQ ON .NET CORE Armen Shimoon 2016-02-22

NeelBhatt commented 8 years ago

Hello guys,

I have wrote blog post on Moq in Asp.Net core which is as below:

https://neelbhatt40.wordpress.com/2016/10/15/moq-in-asp-net-core/

I saw this was hot topic on Stackoverflow so decided to write post on this so that more people can know about this.

If anyone wants to give feedback on this then please do and that is the reason I am writing this here.

jeremymeng commented 8 years ago

No no no you are sending the wrong information. m oq.netcore is obsolete. I don't know why you couldn't find Moqon nuget.org, but it's there https://www.nuget.org/packages/Moq/4.6.38-alpha

Sent from Outlook Mobilehttps://aka.ms/blhgte.


From: NeelBhatt notifications@github.com Sent: Friday, October 14, 2016 11:50:52 PM To: moq/moq4 Cc: Jeremy Meng; Mention Subject: Re: [moq/moq4] Make Moq portable to .NET Core (#168)

Hello guys,

I have wrote blog post on Moq in Asp.Net core which is as below:

https://neelbhatt40.wordpress.com/2016/10/15/moq-in-asp-net-core/https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fneelbhatt40.wordpress.com%2F2016%2F10%2F15%2Fmoq-in-asp-net-core%2F&data=01%7C01%7CJeremy.Meng%40microsoft.com%7C191e31ff3ef74cc75a7108d3f4c79bd6%7C72f988bf86f141af91ab2d7cd011db47%7C1&sdata=4VOsmHRPiICsMBBOwpvk%2FGH%2Fj8Ix0KdAh3cnWRuOJts%3D&reserved=0

I saw this was hot topic on Stackoverflow so decided to write post on this so that more people can know about this.

If anyone wants to give feedback on this then please do and that is the reason I am writing this here.

You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fmoq%2Fmoq4%2Fissues%2F168%23issuecomment-253967026&data=01%7C01%7CJeremy.Meng%40microsoft.com%7C191e31ff3ef74cc75a7108d3f4c79bd6%7C72f988bf86f141af91ab2d7cd011db47%7C1&sdata=kHjTUPtr4qZmrRPpUqnUe94DCOmmhzlSKyfb8MGiRGY%3D&reserved=0, or mute the threadhttps://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAHO4X6v0v5br_Gn6-ydtAWqmCC9PFcTxks5q0HfMgaJpZM4EM6yQ&data=01%7C01%7CJeremy.Meng%40microsoft.com%7C191e31ff3ef74cc75a7108d3f4c79bd6%7C72f988bf86f141af91ab2d7cd011db47%7C1&sdata=FpPC8rQLVfi5P3oF3hnKxT3sOTjwpMpMm5A%2FkcMBJ1E%3D&reserved=0.

NeelBhatt commented 8 years ago

Okay thanks Jeremy, I will update that 👍

raffaeu commented 7 years ago

@jeremymeng Your link is broken, I can only find this on your Blog: Moq in ASP NET Core

Is this one the Blog post? I guess so because I followed it and now I can use NET 1.1 together with Moq

NeelBhatt commented 7 years ago

Yes @raffaeu that link is correct and updated.

raffaeu commented 7 years ago

Thanks @NeelBhatt, sorry I though the blog post was from @jeremymeng, I didn't pay attention ☺️

NeelBhatt commented 7 years ago

That is fine @raffaeu :)

kzu commented 7 years ago

Done!