gpt4 book ai didi

c# - NUnit 测试因 System.IO.FileNotFoundExeption 而失败

转载 作者:太空狗 更新时间:2023-10-30 00:56:33 25 4
gpt4 key购买 nike

我的 Windows Phone 7 项目中的一个 NUnit 测试一直失败并出现以下错误:

Error   4   Test 'MyProject.Tests.Unit.Service.TaskServiceTests.Save_CallsWebService_WhenCalledWithTaskAndSession' failed: System.IO.FileNotFoundException : Could not load file or assembly 'System.Runtime.Serialization, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system cannot find the file specified.
at Newtonsoft.Json.Serialization.DefaultContractResolver.InitializeContract(JsonContract contract)
at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract(Type objectType) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\DefaultContractResolver.cs:line 279
at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\DefaultContractResolver.cs:line 545
at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type type) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\DefaultContractResolver.cs:line 188
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.GetContractSafe(Object value) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalWriter.cs:line 84
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalWriter.cs:line 67
at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\JsonSerializer.cs:line 451
at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Formatting formatting, JsonSerializerSettings settings) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\JsonConvert.cs:line 591
at Newtonsoft.Json.JsonConvert.SerializeObject(Object value) in d:\Development\Releases\Json\Working\Src\Newtonsoft.Json\JsonConvert.cs:line 528
at MyProject.Core.Utility.Extensions.ToJson(Object value) in C:\Workspace\MyProject\MyProject.Service\Utility\Extensions.cs:line 15
at MyProject.Service.Service.TaskService.Save(Task task, ISession session) in C:\Users\jearl\Dropbox\Code\Workspace\MyProject\MyProject.Service\Service\TaskService.cs:line 34
at MyProject.Tests.Unit.Service.TaskServiceTests.Save_CallsWebService_WhenCalledWithTaskAndSession() in C:Workspace\MyProject\MyProject.Tests.Unit\Service\TaskServiceTests.cs:line 43 C:\Workspace\MyProject\MyProject.Service\Utility\Extensions.cs 15

此问题是由我编写的使用 JSON.NET 将 POCO 序列化为 JSON 的扩展方法引起的。 JSON.NET 似乎依赖于 System.Runtime.Serialization。

我已经尝试将 JSON.NET 和 System.Runtime.Serialization 引用添加到我的测试项目中,但这并没有解决问题。

我正在测试的程序集使用 RestSharp,它又依赖于 JSON.NET。我也尝试将对 System.Runtime.Serialization 的引用直接添加到正在测试的程序集,但这也失败了。

有什么建议吗?

更新:我正在尝试按照下面评论中的建议追踪该文件,但 Filemon 已停用。它的替代品 Process Explorer 没有帮助,因为它似乎只显示当前由 devenv.exe 进程打开的文件。搜索对有问题的文件的引用只会产生两个不相关的引用。有没有办法查看我的测试运行记录,以便我知道它在哪里寻找 .dll?

更新 2:感谢您的建议。通过使用 Nuget 将 RestSharp 和 JSON.NET 引用直接添加到单元测试项目,我解决了 IO 问题。现在我收到以下错误:

Error   5   Test 'MyProject.Tests.Unit.Service.TaskServiceTests.Save_CallsWebService_WhenCalledWithTaskAndSession' failed: System.InvalidProgramException : Common Language Runtime detected an invalid program.
at System.Xml.Linq.XDocument..ctor()
at RestSharp.Serializers.XmlSerializer.Serialize(Object obj)
at RestSharp.RestRequest.AddBody(Object obj, String xmlNamespace)
at RestSharp.RestRequest.AddBody(Object obj)
at MyProject.Service.Service.TaskService.Save(Task task, ISession session) in C:\MyProject\MyProject.Service\Service\TaskService.cs:line 34
at MyProject.Tests.Unit.Service.TaskServiceTests.Save_CallsWebService_WhenCalledWithTaskAndSession() in C:\MyProject\MyProject.Tests.Unit\Service\TaskServiceTests.cs:line 43 C:\MyProject\MyProject.Service\Service\TaskService.cs 34

在网络上搜索此内容主要返回对 TFS 开发的引用。太好了。

最佳答案

将单元测试项目的 WP7 项目引用中的所有系统程序集的“复制本地”设置为 true。我们在我们的项目中同时使用了 Netwonsoft 的 Json 和 NUnit,没有任何问题。

干杯,乔恩

关于c# - NUnit 测试因 System.IO.FileNotFoundExeption 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7445023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com