Unit testing 单元测试.Net核心库–;无法加载文件或程序集

Unit testing 单元测试.Net核心库–;无法加载文件或程序集,unit-testing,.net-core,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(stri

我编写了一个简单的类库,使用方法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:

该项目已成功建成

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

每次运行测试时,都会出现以下错误:

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是VisualStudioCommunity2015


提前感谢您的支持。

您可以使用xunit for作为单元测试框架。
我在通过vs2015更新3添加xunit时也遇到了问题,我在

上发布了我的工作。在我的案例中,我发现原因是更新到2017年,csproj的PropertyGroup中有很多不需要的属性。与RuntimeFrameworkVersion一样,NetStandardImplitPackageVersion

尝试使用VS2017 RC。这是使用最新SDK和IDE功能的最新也是唯一的方法。忘记project.json吧。你的类库不应该依赖于
System.Runtime.InteropServices.RuntimeInformation
?我安装了VS2017 RC,效果很好。我猜我的上一个IDE有一些完整性问题(必须修复安装、丢失软件包等),我会标记正确的答案,但奇怪的是,这个选项不可用。
[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.