Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

c# - Mocking framework for asp.net core 5.0

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

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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! :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...