单元测试.Net核心库 - 无法加载文件或程序集

时间:2017-01-05 13:08:00

标签: unit-testing .net-core

我用一个方法 GetNodes 编写了一个简单的类库来检查所选目录的内容。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

public void GetNodes(string directoryPath)
        {
            if (directoryPath == null)
                throw new ArgumentNullException("directoryPath");

            //Remove white-space characters from the start and end of path.
            directoryPath = directoryPath.Trim();

            bool isWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

(...)

}

project.json:

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.3.0"
  },

  "frameworks": {
    "netstandard1.3": {
      "imports": "dnxcore50"
    }
  }
}

项目已成功构建。

然后,为了执行一些单元测试,我创建了一个简单的单元测试项目(.Net Framework 4.6)

[TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void DetectException_WhenCorrectExceptionIsThrown()
        {
            FileManager fm = new FileManager();

            fm.GetNodes(@"c:\");
        }

每次运行测试时都会出现此错误:

Test Name:  DetectException_WhenCorrectExceptionIsThrown
Test FullName:  Task_2_Test.FileManagerTests.DetectException_WhenCorrectExceptionIsThrown
Test Source:    D:\Git\task_2\Task_2\test\Task_2_Test\FileManagerTests.cs : line 25
Test Outcome:   Failed
Test Duration:  0:00:00,0152049

Result StackTrace:  
at Task_2.FileManager.GetNodes(String directoryPath)
   at Task_2_Test.FileManagerTests.DetectException_WhenCorrectExceptionIsThrown() in D:\Git\task_2\Task_2\test\Task_2_Test\FileManagerTests.cs:line 30
Result Message: 
Test method Task_2_Test.FileManagerTests.DetectException_WhenCorrectExceptionIsThrown threw exception System.IO.FileNotFoundException, but exception System.ArgumentNullException was expected. Exception message: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. Nie można odnaleźć określonego pliku.=== Pre-bind state information ===
LOG: DisplayName = System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = file:///D:/Git/task_2/Task_2/test/Task_2_Test/bin/Debug
LOG: Initial PrivatePath = NULL
Calling assembly : Task_2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\VISUAL STUDIO EXPRESS 2015\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\TESTWINDOW\vstest.executionengine.x86.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: Attempting download of new URL file:///D:/Git/task_2/Task_2/test/Task_2_Test/bin/Debug/System.Runtime.InteropServices.RuntimeInformation.DLL.
LOG: Attempting download of new URL file:///D:/Git/task_2/Task_2/test/Task_2_Test/bin/Debug/System.Runtime.InteropServices.RuntimeInformation/System.Runtime.InteropServices.RuntimeInformation.DLL.
LOG: Attempting download of new URL file:///D:/Git/task_2/Task_2/test/Task_2_Test/bin/Debug/System.Runtime.InteropServices.RuntimeInformation.EXE.
LOG: Attempting download of new URL file:///D:/Git/task_2/Task_2/test/Task_2_Test/bin/Debug/System.Runtime.InteropServices.RuntimeInformation/System.Runtime.InteropServices.RuntimeInformation.EXE.

我看到这个错误与System.Runtime.InteropServices有关(我使用这个库来检查当前的操作系统,以验证目录路径的正确语法)。

我不想退出这个功能,但我不知道如何处理这个问题。

顺便说一下。我的Ide是2015年的Visual Studio社区。

提前感谢您的支持。

2 个答案:

答案 0 :(得分:0)

您可以使用xunit作为单元测试框架。 我也有通过vs2015更新3添加xunit的问题,我在http://jickingnotes.blogspot.com/2016/10/add-xunit-test-project-in-net-core.html上发布了我的工作

答案 1 :(得分:0)

在我的情况下,我发现原因是更新到2017年,并且csproj的PropertyGroup中有很多不需要的属性。像RuntimeFrameworkVersion,NetStandardImplitPackageVersion