单元测试 .Net Core Library – 无法加载文件或程序集



我用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:Gittask_2Task_2testTask_2_TestFileManagerTests.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:Gittask_2Task_2testTask_2_TestFileManagerTests.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 2015COMMON7IDECOMMONEXTENSIONSMICROSOFTTESTWINDOWvstest.executionengine.x86.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:WindowsMicrosoft.NETFrameworkv4.0.30319configmachine.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是Visual Studio Community 2015。

提前感谢您的支持。

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

最新更新