简体   繁体   中英

using Moq in Asp.Net 5 with dnx50

I am new to moq and have a problem when I use it in my test project with Asp.Net 5 and MVC 6.

I installed Moq by using NuGet and installed Moq 4.2.1510.2205. When I checked project.json, I got

{
  "version": "1.0.0-*",
  "description": "TargetTests Class Library",
  "authors": [ "Admin" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",

  "dependencies": {
    "Target": "1.0.0-*",
    "xunit": "2.2.0-beta1-build3239",
    "xunit.runner.console": "2.2.0-beta1-build3239",
    "xunit.runner.dnx": "2.1.0-rc1-build204"
  },

  "commands": {
    "test": "xunit.runner.dnx"
  },

  "frameworks": {
    "dnx451": {
      "dependencies": {
        "Moq": "4.2.1510.2205"
      }
    },
    "dnxcore50": {
      "dependencies": {
      }
    }
  }
}

When I add using Moq in a cs file, I got the following error

Severity    Code    Description Project File    Line    Suppression State
Error   CS0246  The type or namespace name 'Moq' could not be found (are you missing a using directive or an assembly reference?)   TargetTests.DNX Core 5.0    C:\Users\Admin\Desktop\Target\src\TargetTests\TestBase.cs   6   Active

Can anyone help to see how to solve this problem? I have checked StackOverfollow and tried the method introduced in Mocking framework for asp.net core 5.0 , but failed.

This is because the Moq version you have installed from NuGet does not support DNX platform, and your project is configured to be runnable on that platform by virtue of having a "dnxcore50" component in your project.json file.

If you hover your cursor over a method call to Moq, you should see something like this: 在此处输入图片说明

To fix this, you have two options:

  1. Remove the "dnxcore50" component from your project.json file (your code will only function in enviornments with ASP.Net 4.5.1+ installed as a result).
  2. Add a version of Moq that supports the ASP.Net Core/DNX Framework.

If you'd like to do the latter option, the second answer by Lucas Pyrzyk worked when I tried it out (including the comment below his answer).

First, add a dependency to your project.json to the moq.netcore library.

{
  "version": "1.0.0-*",
    "description": "TargetTests Class Library",
    "authors": [ "Admin" ],
    "tags": [ "" ],
    "projectUrl": "",
    "licenseUrl": "",

    "dependencies": {
      "Target": "1.0.0-*",
      "xunit": "2.2.0-beta1-build3239",
      "xunit.runner.console": "2.2.0-beta1-build3239",
      "xunit.runner.dnx": "2.1.0-rc1-build204"
    },

    "commands": {
      "test": "xunit.runner.dnx"
    },

    "frameworks": {
      "dnx451": {
        "dependencies": {
          "Moq": "4.2.1510.2205"
        }
      },
      "dnxcore50": {
        "dependencies": {
          "moq.netcore": "4.4.0-beta8"
        }
      }
    }

Then, add a file called NuGet.config in the root of your project that is using it. It's contents should look like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="AspNetVNext" value="https://www.myget.org/F/aspnet-contrib/api/v3/index.json" />
    <add key="NuGet" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
</configuration>

Use "Moq": "4.6.25-alpha"

My project.json:

 {
      "version": "1.0.0-*",
      "testRunner": "xunit",
      "dependencies": {
        "xunit": "2.2.0-beta2-build3300",
        "dotnet-test-xunit": "2.2.0-preview2-build1029",
        "Microsoft.AspNetCore.TestHost": "1.0.0",
        "Moq": "4.6.25-alpha",
        "Newtonsoft.Json": "9.0.1",
        "System.Diagnostics.TraceSource": "4.0.0",
        "System.Net.Http": "4.1.0"
      },
      "frameworks": {   
        "netcoreapp1.0": {
          "dependencies": {
            "Microsoft.NETCore.App": {
              "type": "platform",
              "version": "1.0.0"
            }
          },
          "imports": [
            "dotnet5.4",
            "portable-net451+win8"
          ]
        }
      },

      "buildOptions": {
        "copyToOutput": {
          "include": [ "xunit.runner.json" ]
        }
      }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM