Mocking framework for asp.net core 5.0

徘徊边缘 提交于 2019-12-17 19:18:51

问题


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"
}

}

回答1:


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:


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:


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




回答4:


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



来源:https://stackoverflow.com/questions/27918305/mocking-framework-for-asp-net-core-5-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!