UPDATE on 15th October 2016:
Moq for .Net core is now available on Nuget and you can get it via below command:
It means the setting mentioned below are not required now.
UPDATE on 20th October 2016:
Moq Nuget version 4.6.38-alpha worksfor .Net core- but e.g. 4.5.3 doesn’t work, that complains that Moq 4.5.3 supports net45 and one or more packages are incompatible with .NETCoreApp V1.0.
See this post for more information.
Old Post:
I was messing around a bit for Unit testing in .Net core and I suddenly remembered about mocking, and thus I decided to use Moq which is the most popular .Net mocking framework.
After few attempts I got below error while compiling:
ASP.NET Core 5.0 error CS0246: The type or namespace name ‘Moq’ could not be found (are you missing a using directive or an assembly reference?)
After searching more on this, I wondered that Moq is not supported by .Net core!!!!!
Though I thought it is not possible as Moq is very popular among developers.
After searching more, I found that ASP.NET team from Microsoft have created a special version of Moq for internal usage.
You need to add special source to your Nuget package manager to be able to locate development version of packages by .Net team.
URL of sources where you could find this packet is here
Ok, but how to use it?
There are currently several ways to use it.
First of all Register a Moq dependencies in project.json:
"dependencies": {
"xunit": "2.2.0-beta2-build3300",
"dotnet-test-xunit": "2.2.0-preview2-build1029",
"moq": "4.6.38-alpha"
}
Now for Nuget, you can perform any of below steps:
- Configure a new NuGet feed to be able to reference the
moq.netcore
package - You can Create a NuGet.config file in project home directory.
If you have decided to create Nuget.config file then add below line of codes in that:
<?xml version="1.0" encoding="utf-8"?>
<add key="AspNetVNext" value="https://www.myget.org/F/aspnetcidev/api/v3/index.json" />
<add key="NuGet" value="https://api.nuget.org/v3/index.json" />
packageSources>
configuration>
And if you have decided to configure a new Nuget feed then make sure configuration matches with below configurations:
That is it!
Now you can use Moq with Asp .Net core.
Happy coding!
“Dnx” frameworks are outdated. See https://docs.microsoft.com/en-us/dotnet/articles/standard/frameworks for more recent info.
LikeLike
Yes I have updated that thanks a lot 🙂
LikeLike
That repo does not contain Moq at this time?
LikeLike
See updated post @Torgeir
LikeLike
I noticed after posting my comment, I read your post before the coffee had started working this morning 😦
Thanks! 🙂
LikeLike
Haha I can understand that 🙂
LikeLike
https://myget-2e16.kxcdn.com/artifacts/aspnet-contrib/nuget/v3/flatcontainer/system.xml.xmldocument/index.json This url does not work. When we dockerize, it’s using this feed and looks like the Nuget package is not updated here. Inside of VS https://www.myget.org/F/aspnet-contrib/api/v3/index.json works just fine. Any ideas?
LikeLike
Hi Surendra, Have you seen update of this post? And also StackOverflow post for this? Try exactly the same way and if you still see issue then let me know.
LikeLike
I’m starting to think the update text at the top should be highlighted better, Neel 😛
LikeLiked by 1 person
Haha yes Torgeir, just did that. Updated the post. Thanks 🙂
LikeLike
There’s definately a great deal to know about this topic.
I love all the points you’ve made.
LikeLike