Make Moq portable to .NET Core #168

Open
kuhlenh opened this Issue Apr 30, 2015 · 59 comments

Projects

None yet
@kuhlenh
kuhlenh commented Apr 30, 2015

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

@ghost
ghost commented May 4, 2015

Any news on this? This is something myself and a few others would really like to see.

Also happy to help out with any port

@kzu
Member
kzu commented May 4, 2015

We'd definitely appreciate the help @sblackler! Nobody's working on that port for now.

@ghost
ghost commented May 4, 2015

At a quick glance, I'm not sure what we can do about Silverlight support currently. Maybe @kuhlenh could provide some insight on what to do/when/if support will be coming?

Also, when I import all the files into a new class library project, most of the errors seem to be around the Castle dependency. I think that's going to be the biggest stumbling block for porting.

@kzu
Member
kzu commented May 4, 2015

Indeed :(.

So how about getting Castle to port first somehow? ;)

On Mon, May 4, 2015 at 5:34 PM Stuart Blackler notifications@github.com
wrote:

At a quick glance, I'm not sure what we can do about Silverlight support
currently. Maybe @kuhlenh https://github.com/kuhlenh could provide some
insight on what to do/when/if support will be coming?

Also, when I import all the files into a new class library project, most
of the errors seem to be around the Castle dependency. I think that's going
to be the biggest stumbling block for porting.

Reply to this email directly or view it on GitHub
#168 (comment).

@kuhlenh
kuhlenh commented May 4, 2015

I've put your report online for future reference.

If you reach out to Castle, feel free to direct them my way if they need any help. Otherwise, I'm happy to reach out to them for you!

@kzu
Member
kzu commented May 4, 2015

Maybe I can just bring @kkozmic to the discussion :)

@kkozmic
kkozmic commented May 4, 2015

and I appear :)

@kkozmic kkozmic referenced this issue in castleproject/Core May 4, 2015
Closed

Support for .NET core #90

@MattGal
MattGal commented May 6, 2015

In previous exploration of what it's like to port existing content to CoreCLR, I have actually already done a private (not ready for PR) port based off of the Moq4 Silverlight 5 version, and would be interested in doing so in a more publicly sharable fashion.

Having a usable version of Castle.Core that isn't me hacking around would be great. @kkozmic , can you comment how close to reality this might be? Having already done it, I think the Moq port should be pretty reasonable.

@hidori
hidori commented Jun 19, 2015

👍

@jeremymeng

I started experimenting on a port based on the Castle.Core PR by @MattGal (castleproject/Core#92/).

Update I now have a version at https://github.com/jeremymeng/Moq4/tree/netcore-beta5. It works with Asp.Net vNext.

@oadrian2

@jeremymeng Have you pushed a nuget package for beta5?

@richlander

We could. I just replied to the Castle Core thread to see if we can get the code finally merged. That will mitigate the need for a separate package.

Also, beta 5 is now a ways out of date. We should be targeting beta 7.

@kzu kzu referenced this issue Oct 8, 2015
Closed

dnxcore50 support #206

@oadrian2

@richlander Any progress on this, especially now that we're at ASP.Net beta 8 and now feature-complete?

@ghost
ghost commented Oct 22, 2015

Hellloooo....? Any news?

@jeremymeng

Moq is depending on castleproject/Core#90. Hope we are seeing progress soon on the Castle.Core port.

@klings klings referenced this issue in NWebsec/NWebsec Nov 15, 2015
Open

Support ASP.NET Core #59

@jeremymeng

Castle.Core support is around the corner so I will submit a PR earlier to get feedback.

@davunderscorei

Is there any update on Core CLR support?

@jeremymeng

Castle.Core alpha1 package on nuget.org already support .NET Core. I've updated #225 to work with that. Right now everyone is waiting for RC2 release. Once that happens, Castle.Core will be updated to use RC2, and so will Moq.

@nickolasacosta nickolasacosta referenced this issue in milkshakesoftware/PreMailer.Net May 10, 2016
Open

Support for CoreCLR #96

@jeremymeng

I've updated my PR to work with RC2. Moq Team members, please take a look.

@lucabriguglia

Moq team members any news on this? Thanks :-)

@kzu
Member
kzu commented May 29, 2016

https://twitter.com/kzu/status/735944176953655296?s=09

On Sun, May 29, 2016, 8:44 AM Luca Briguglia notifications@github.com
wrote:

Moq team members any news on this? Thanks :-)


You are receiving this because you commented.

Reply to this email directly, view it on GitHub
#168 (comment), or mute
the thread
https://github.com/notifications/unsubscribe/AAKW66RZ0Ne0FuiEFG3lF4RWzQwPPDDdks5qGXw5gaJpZM4EM6yQ
.

@lucabriguglia

thanks @kzu

@kzu
Member
kzu commented May 29, 2016

@jeremymeng did all the hard work :)

Should the PR be merged to a netcore feature branch and pushed to nuget.org
as alpha? We are kinda waiting for .net core to RTM ;)

On Sun, May 29, 2016, 9:38 AM Luca Briguglia notifications@github.com
wrote:

thanks @kzu https://github.com/kzu


You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
#168 (comment), or mute
the thread
https://github.com/notifications/unsubscribe/AAKW65_66562jhwGgkOxrPhVeCPUmeK1ks5qGYjbgaJpZM4EM6yQ
.

@neoKushan

@kzu I would say so, yes. For all of my projects, Moq is the only thing that currently requires a separate nuget feed to build. It's not a biggie, but would certainly be nice :)

@jeremymeng

Should the PR be merged to a netcore feature branch and pushed to nuget.org
as alpha?

Yes please. It would make other people's life easier!

@rmarinho
rmarinho commented Jun 6, 2016

Yes please @kzu :)

@kzu
Member
kzu commented Jun 6, 2016
@lucabriguglia

Thank you very much Daniel!

@nickolasacosta
nickolasacosta commented Jul 5, 2016 edited

I am on .net core RTM and see this version is not compatible with "netstandard1.6" because of Castle.Core 4.0.0-alpha001.

Issue here:castleproject/Core#191

@nickolasacosta nickolasacosta referenced this issue in castleproject/Core Jul 5, 2016
Closed

.net core netstandard1.6 #191

@martinnormark

Hey - I see that Castle Core published a pre-release package two days ago which should fix the targeting issue: https://www.nuget.org/packages/Castle.Core/4.0.0-beta001

castleproject/Core#205

Is it possible to get a pre-release of Moq published to test what @NickolasAcosta was doing?

@PivotalAnimal

I am not familiar with this code any but would be glad to help with a port. Let's branch and organize. Does anyone know of an alternative to this project targeting Core?

@JasonBock

I'm currently trying to use "4.6.25-alpha" in a .NET Core class library project set up for xUnit based on their guidelines:

https://xunit.github.io/docs/getting-started-dotnet-core.html

Here's what project.json looks like:

"dependencies": {
    "xunit": "2.2.0-beta2-build3300",
    "dotnet-test-xunit": "2.2.0-preview2-build1029",
    "FluentAssertions": "4.12.0",
    "Moq": "4.6.25-alpha"
},

"frameworks": {
    "netcoreapp1.0": {
        "dependencies": {
            "Microsoft.NETCore.App": {
                "type": "platform",
                "version": "1.0.0"
            }
        }
    }
}

I'm getting the Castle.Core issue:

Error NU1002 The dependency Castle.Core 4.0.0-alpha001 does not support framework .NETCoreApp,Version=v1.0.

Any chance of getting the nupkg from @jeremymeng pushed to Nuget soon as this will probably fix the issue (I think :))?

@RichardD012

Under frameworks, if you add in the import to dnxcore, it should work.

"netcoreapp1.0": {
      "imports": [
        "dnxcore50",
        "netcoreapp1.0"
      ],
      "dependencies": {
        "Microsoft.NETCore.App": {
          "version": "1.0.0-*",
          "type": "platform"
        },
      }
    }
@JasonBock

Yes @RichardD012 that does work, thanks. Now to see if my unit test code will work :)

@alexellis

Guys - does this or any Moq/Mocking framework exist for DNCore + XUnit?

@neoKushan

@alexellis Yes, you will need to show pre-release packages in Nuget and look at the posts just above yours.

@alexellis

@jeremymeng @neoKushan I got somewhere with this https://github.com/alexellis/dncore-market-rates

It seems like xunit has dependencies that need to bring in more than DNCore itself?

Link to issue: dotnet/docs#828 (comment)

Could not run this through the official .NET Core Docker images.

@jeremymeng

@alexellis I suspect that you are missing the global.json file in the image? It tells dotnet restore where to look for projects. If I import microsoft/dotnet:latest and add the following it works.

COPY ./global.json ./global.json
@jeremymeng

(Totally unrelated to Moq though)

@PivotalAnimal

Thanks! I’ve updated my repository by adding some base test classes from Microsoft Identity that I will use later. So far it is building and the 8 added tests from Microsoft that were able to build are testing successfully.

From: Jeremy Meng [mailto:notifications@github.com]
Sent: Wednesday, July 20, 2016 7:08 PM
To: moq/moq4 moq4@noreply.github.com
Cc: Kai Langlie kai@langlie.com; Mention mention@noreply.github.com
Subject: Re: [moq/moq4] Make Moq portable to .NET Core (#168)

https://www.nuget.org/packages/Moq/4.6.36-alpha


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub #168 (comment) , or mute the thread https://github.com/notifications/unsubscribe-auth/ANIqZaqpY-ao6IG_Ba3XcXKjlfyaVBdjks5qXqpEgaJpZM4EM6yQ . https://github.com/notifications/beacon/ANIqZTLTtGuso5AOsWSVAVrAIeSWUu0-ks5qXqpEgaJpZM4EM6yQ.gif

@alexellis

Thanks I added global.json - I completely missed the fact it was not added. Is there a smart way of restoring so that Docker can cache that step rather than repeating it every single build?

@martinnormark

Hi - I'm seeing random test failures when using Verify in PreMailer.Net (PR: milkshakesoftware/PreMailer.Net#122)

This is output from build 186 running this unit test:

    PreMailer.Net.Tests.LinkTagCssSourceTests.GetCSS_CallsWebDownloader_WithSpecifiedDomain [FAIL]
      Moq.MockException : 
Expected invocation on the mock at least once, but was never performed: w => w.DownloadString(It.Is<Uri>(u => u.Scheme == "http" && u.Host == "a.co"))
      No setups configured.
      No invocations performed.
      Stack Trace:
           at Moq.Mock.ThrowVerifyException(MethodCall expected, IEnumerable`1 setups, IEnumerable`1 actualCalls, Expression expression, Times times, Int32 callCount)
           at Moq.Mock.VerifyCalls(Interceptor targetInterceptor, MethodCall expected, Expression expression, Times times)
           at Moq.Mock.Verify[T,TResult](Mock`1 mock, Expression`1 expression, Times times, String failMessage)
           at Moq.Mock`1.Verify[TResult](Expression`1 expression)
        C:\projects\premailer-net\PreMailer.Net\PreMailer.Net.Core.Tests\LinkTagCssSourceTests.cs(38,0): at PreMailer.Net.Tests.LinkTagCssSourceTests.GetCSS_CallsWebDownloader_WithSpecifiedDomain()

And this is from build 189 running another unit test:

    PreMailer.Net.Tests.LinkTagCssSourceTests.GetCSS_CallsWebDownloader_WithSpecifiedBundle [FAIL]
      Moq.MockException : 
Expected invocation on the mock at least once, but was never performed: w => w.DownloadString(It.Is<Uri>(u => u.PathAndQuery == "/Content/css?v=7V7TZzP9Wo7LiH9_q-r5mRBdC_N0lA_YJpRL_1V424E1"))
      No setups configured.
      No invocations performed.
      Stack Trace:
           at Moq.Mock.ThrowVerifyException(MethodCall expected, IEnumerable`1 setups, IEnumerable`1 actualCalls, Expression expression, Times times, Int32 callCount)
           at Moq.Mock.VerifyCalls(Interceptor targetInterceptor, MethodCall expected, Expression expression, Times times)
           at Moq.Mock.Verify[T,TResult](Mock`1 mock, Expression`1 expression, Times times, String failMessage)
           at Moq.Mock`1.Verify[TResult](Expression`1 expression)
        C:\projects\premailer-net\PreMailer.Net\PreMailer.Net.Tests\LinkTagCssSourceTests.cs(49,0): at PreMailer.Net.Tests.LinkTagCssSourceTests.GetCSS_CallsWebDownloader_WithSpecifiedBundle()

Both tests works fine locally on both Mac and Windows, so it could be something to specific to the environment running in AppVeyor.

@benrr101
benrr101 commented Aug 8, 2016

Found a missing runtime package dependency in the beta package released on MyGet. System.Diagnostics.TraceSource is needed at runtime but isn't listed in the dependencies for the package. Please let me know if you need any more info on this issue.

@jeremymeng

@benrr101 Which version of Moq are you using? There was an issue in Castle.Core but it has been fixed in the latest version. The System.Diagnostics.TraceSource dependency should be pulled in by Castle.Core. See the dependency list at https://www.nuget.org/packages/Castle.Core/4.0.0-beta001.

@benrr101

@jeremymeng We're using version moq.netcore 4.4.0-beta8 from myget.org, which has a dependency on Castle.Core >=3.3.4-beta8 (which is the version that got pulled in). Can this dependency get bumped up in the next beta release of moq.netcore?

@jeremymeng

@benrr101 Please use the latest version from NuGet: https://www.nuget.org/packages/Moq/4.6.36-alpha. The version you are using is from ASP.NET myget feed, which is pretty old now.

@benrr101

@jeremymeng you are right! My coworker who suggested using the myget feed version made it seem like this was the only version that worked with .net core. We didn't even think to try the packages on nuget.org. Thanks!

@redowl3
redowl3 commented Sep 5, 2016

I'm having no luck creating a project that references Moq and Xunit after following the advice from above. I'm getting the dependency Castle.Core 4.0.0-alpha001 does not support framework .NETCoreApp,Version=v1.0. error

Any ideas?

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

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

@jeremymeng

@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:

  • The latest version is 4.6.38-alpha
  • You don't need to import netcoreapp1.0 within the netcoreapp1.0 framework section.
@redowl3
redowl3 commented Sep 7, 2016

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

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

@redowl3
redowl3 commented Sep 7, 2016

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

@redowl3 you could publish the simple project to github.

@gerryLowry

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

@ForNeVeR ForNeVeR referenced this issue in ForNeVeR/Jabber-Net Sep 29, 2016
Open

Replace Rhino Mocks with Moq #67

@NeelBhatt

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

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

Okay thanks Jeremy, I will update that 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment