28

I recently installed Visual Studio 2015 and started a project with a web site and a asp class library which will contain the unit tests for the web site. I usually use Moq for mocking but I am no stranger to try a different mocking framework. The problem I am having is that I added Moq as a reference to the unit test project and started using it. Everything seems fine at first until I tried to compile.

When I compiled I got an error message saying:

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?)

I noticed that I could switch between ASP.NET 5.0 and ASP.NET Core 5.0 in the code view and when selecting ASP.NET Core 5.0 I get errors but no when selecting ASP.NET 5.0. I tried searching for an answer but I did not have any luck.

Is the problem that Moq does not work with vnext and I should use a different framework (if so which works?) or can I solve this somehow? My project.json:

{
"version": "1.0.0-*",
"dependencies": {
    "Web": "1.0.0-*",
    "Xunit.KRunner": "1.0.0-beta1",
    "xunit": "2.0.0-beta5-build2785",
    "Moq": "4.2.1409.1722"
},

"frameworks": {
    "aspnet50": {
        "dependencies": {
        }
    },
    "aspnetcore50": {
        "dependencies": {
        }
    }
},
"commands": {
    "test": "Xunit.KRunner"
}

}
0

4 Answers 4

26

ASP.NET team from Microsoft have created a special version of Moq for internal usage. You can find a fork project on the asp.net github page.

How to use that?

  • Register a Moq dependencies in project.json:

    {
        "frameworks": {
            "dnx451": {
                "dependencies": {
                    "Moq": "4.2.1312.1622"
                }
            },
            "dnxcore50": {
                "dependencies": {
                    "moq.netcore": "4.4.0-beta8"
                }
            }
        }
    }
    
  • Create a NuGet.config file in project home directory.

    <?xml version="1.0" encoding="utf-8"?>
        <configuration>
            <packageSources>
                <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 use Moq! :)

3
  • 2
    I had to set the AspNetVNext value to https://www.myget.org/F/aspnet-contrib/api/v3/index.json in order to find moq.netcore.
    – Mike B
    Feb 15, 2016 at 18:03
  • 1
    Additional detail on adding a new NuGet feed to add Moq to dnxcore50 provided in Armen Shimoon's blog (with pictures!)
    – brntsllvn
    Apr 20, 2016 at 19:24
  • 3
    the asp.net github page link at the beginning of this answer is 404 broken.
    – gerryLowry
    Sep 6, 2016 at 19:08
8

I suppose that so far a version of Moq that works with asp.net core 5.0 is not available in the nuget feed, but I think you can work with Asp.Net 5.0.

2
  • 2
    I removed "aspnetcore50": {"dependencies": {}} and it started working Jan 13, 2015 at 11:25
  • Lukasz Pyrzyk has an updated answer below that gets Moq working with aspnetcore50.
    – David Culp
    Feb 3, 2016 at 2:42
4

You should be able to use most libraries with the aspnet50 framework, as this works with the same structures/CLR "style" that was available in previous .net frameworks. This would include Moq/Rhino/Nunit etc etc.

In aspnetcore50 you are using the new CoreCLR, and need to use dependencies that are also switched to work with the new CoreCLR libraries. Xunit has this already as the MS teams did it so that they could unit test, hence it's availability. I'm not aware that there is a mocking framework yet, but am looking for one.

Therefore if you restrict your project to only using the aspnet50 framework by removing aspnetcore50 then you should be fine to use whatever.

Note though that this gives you the restriction that you can only unit test where the main CLR is available. This might be restrictive for you if you intend your app to run on the CoreCLR when deployed (as you're not strictly testing against the same stuff, albeit they should be equivalent).

There's some more discussion on these points here on a github issue about NUnit availability

0

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

You need to add below configurations in project.json

{
    "version": "1.0.0-*",
    "description": "AspNetCoreMoq.Test Class Library",
    "authors": [ "armen" ],
    "tags": [ "" ],
    "projectUrl": "",
    "licenseUrl": "",

    "frameworks": {
        "dnxcore50": {
            "dependencies": {
                "Microsoft.CSharp": "4.0.1-beta-23516",
                "System.Collections": "4.0.11-beta-23516",
                "System.Linq": "4.0.1-beta-23516",
                "System.Runtime": "4.0.21-beta-23516",
                "System.Threading": "4.0.11-beta-23516",
                "AspNetCoreMoq": "",
                "xunit": "2.1.0-rc1-build3168",
                "xunit.runner.dnx": "2.1.0-rc1-build204",
                "moq.netcore": "4.4.0-beta8"
            }
        }
    },
    "commands": {
        "test": "xunit.runner.dnx"
    }

}

And then create Nuget feed with below Url:

https://www.myget.org/F/aspnet-contrib/api/v3/index.json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.