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

Make Moq portable to .NET Core #168

Closed
kuhlenh opened this issue Apr 30, 2015 · 64 comments
Closed

Make Moq portable to .NET Core #168

kuhlenh opened this issue Apr 30, 2015 · 64 comments

Comments

@kuhlenh
Copy link

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
Copy link

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
Copy link
Contributor

kzu commented May 4, 2015

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

@ghost
Copy link

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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

kzu commented May 4, 2015

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

@kkozmic
Copy link

kkozmic commented May 4, 2015

and I appear :)

@MattGal
Copy link

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
Copy link

hidori commented Jun 19, 2015

👍

@jeremymeng
Copy link
Contributor

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
Copy link

@jeremymeng Have you pushed a nuget package for beta5?

@richlander
Copy link

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 mentioned this issue Oct 8, 2015
@oadrian2
Copy link

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

@ghost
Copy link

ghost commented Oct 22, 2015

Hellloooo....? Any news?

@jeremymeng
Copy link
Contributor

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

@jeremymeng
Copy link
Contributor

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

@davunderscorei
Copy link

Is there any update on Core CLR support?

@jeremymeng
Copy link
Contributor

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.

@jeremymeng
Copy link
Contributor

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

@lucabriguglia
Copy link

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

@kzu
Copy link
Contributor

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
Copy link

thanks @kzu

@alexellis
Copy link

@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
Copy link
Contributor

@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
Copy link
Contributor

(Totally unrelated to Moq though)

@klanglie
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link
Contributor

@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
Copy link

@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
Copy link
Contributor

@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
Copy link

@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
Copy link

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
Copy link
Contributor

@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
Copy link

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
Copy link
Contributor

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

@redowl3
Copy link

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
Copy link
Contributor

@redowl3 you could publish the simple project to github.

@gerryLowry
Copy link

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
Copy link

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
Copy link
Contributor

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
Copy link

Okay thanks Jeremy, I will update that 👍

@raffaeu
Copy link

raffaeu commented Feb 8, 2017

@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
Copy link

Yes @raffaeu that link is correct and updated.

@raffaeu
Copy link

raffaeu commented Feb 8, 2017

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

@NeelBhatt
Copy link

That is fine @raffaeu :)

@kzu
Copy link
Contributor

kzu commented Feb 21, 2017

Done!

@kzu kzu closed this as completed Feb 21, 2017
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

Successfully merging a pull request may close this issue.