使用.NET Core v1进行测试的.NET Core v2.2代码

时间:2019-07-07 03:53:01

标签: c# asp.net .net .net-core asp.net-core-mvc

我创建了一个新的ASP.net Core v2.2项目,并添加了一个NUnit测试项目。当我运行该项目时,它会使用dotnet Core v2.2正确启动。但是,当我运行测试时,收到以下消息,并且测试错误(以在使用dotnet run运行时它们不会出错的方式)。基于为v2.2构建和在v1中运行之间的不匹配,我认为它们已连接。

我不确定如何强制使用v2.2运行测试?

测试运行前的消息:

Test run will use DLL(s) built for framework .NETCoreApp,Version=v1.0 and platform X64. Following DLL(s) do not match framework/platform settings.
NUnitTest.dll is built for Framework 2.2 and Platform AnyCPU.

测试失败消息:

System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.AspNetCore.Mvc.Formatters.Json, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

这是我尝试运行的测试代码:

using NUnit.Framework;
using API.Models;
using System.Threading.Tasks;
using System;
using System.Diagnostics;

namespace Tests
{
    public class Tests
    {
        [SetUp]
        public void Setup()
        {
        }

        [Test]
        public async Task Test1()
        {
            var credentials = new Credentials();
            credentials.setAttributes("publicToken", "refreshTokenSample", 2000);

            var token = await credentials.GetToken();
            Assert.Equals("publicToken", token);
        }
    }
}

复制步骤:

  1. 打开VS 2019
  2. 创建新项目(ASP.NET Core Web应用程序)
  3. 通过右键单击解决方案添加NUnit测试项目(.NET Core), 选择“添加”,然后选择NUnit。
  4. 运行测试。
  5. “测试”的输出显示Test run will use DLL(s) built for framework .NETCoreApp,Version=v1.0 and platform X86. Following DLL(s) do not match framework/platform settings. NUnitTestProject1.dll is built for Framework 2.2 and Platform AnyCPU.

0 个答案:

没有答案