C# MSpec Json.NET反序列化测试在ReSharper中失败,但在NCrunch中通过
我有以下两个单元测试:一个使用MSTest,另一个使用机器规范。在我看来,他们的行为应该是一样的。但是,当第一个在NCrunch和ReSharper测试运行程序中都通过时,第二个在ReSharper中失败C# MSpec Json.NET反序列化测试在ReSharper中失败,但在NCrunch中通过,c#,json.net,resharper,mspec,ncrunch,C#,Json.net,Resharper,Mspec,Ncrunch,我有以下两个单元测试:一个使用MSTest,另一个使用机器规范。在我看来,他们的行为应该是一样的。但是,当第一个在NCrunch和ReSharper测试运行程序中都通过时,第二个在ReSharper中失败 using Machine.Specifications; using Microsoft.VisualStudio.TestTools.UnitTesting; using Newtonsoft.Json; public class TestModel { public strin
using Machine.Specifications;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
public class TestModel
{
public string Name { get; set; }
public int Number { get; set; }
}
// MSTest
[TestClass]
public class DeserializationTest
{
[TestMethod]
public void Deserialized_object_is_the_same_type_as_the_original()
{
TestModel testModel = new TestModel() {Name = "John", Number = 42};
string serialized = JsonConvert.SerializeObject(testModel, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects });
object deserialized = JsonConvert.DeserializeObject(serialized, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects });
// This passes in both test runners
Assert.IsInstanceOfType(deserialized, typeof(TestModel));
}
}
// MSpec
public class When_an_object_is_deserialized
{
static TestModel testModel;
static string serialized;
static object deserialized;
Establish context = () =>
{
testModel = new TestModel() { Name = "John", Number = 42 };
serialized = JsonConvert.SerializeObject(testModel, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects });
};
Because of = () => deserialized = JsonConvert.DeserializeObject(serialized, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects });
// This passes in NCrunch but fails in ReSharper.
It should_be_the_same_type_as_the_original = () => Assert.IsInstanceOfType(deserialized, typeof(TestModel));
}
失败消息为:Assert.IsInstanceOfType失败。所需类型:。实际类型:。
奇怪的是,以下情况确实发生了:
It should_be_the_same_type_as_the_original = () => Assert.IsTrue(testModel.GetType() == typeof(TestModel));
我以这种方式进行反序列化,因为实际的代码需要能够处理在运行时之前类型未知的对象。我假设Json.NET进行这种反序列化的方式有些奇怪,但是为什么两个测试运行者的行为应该不同呢?我正在Visual Studio 2013中使用ReSharper 9.1。很明显,由于NCrunch和ReSharper之间行为上的细微差异,运行时会产生奇怪的效果。失败肯定是告诉您出了什么问题,您不应该将其视为ReSharper或NCrunch中的一个bug 当我在调试器中单步执行MSpec测试时,反序列化的
对象在调试器中显示以下错误:
如果看不到完整的解决方案,就很难确定,但是我看到过当构建输出目录包含一个程序集的多个副本(可能在子目录中)时会发生这种情况。如果不同的零部件在不同的时间引用程序集的不同副本,则有时会认为程序集的类型不相等,即使它实际上是程序集的相同副本。解决方案是确保构建输出中每个程序集只有一个副本,这确保所有内容都引用完全相同的文件。可能是JSON转换器动态加载您的类型并获取错误的程序集,或者可能将其加载到不同的加载上下文中,这意味着其类型与在不同上下文中加载的副本不相等
在MSpec案例中,您的构建环境可能会导致程序集的副本。特别是NCruch,默认情况下不执行生成后事件(通常会显示一个警告),因此如果您在生成后步骤中复制文件,那么这可能是不同行为的一种解释。您可以通过在NCrunch中启用生成后事件并查看是否发生故障来检查这一点
另一个可能的故障排除步骤是使用Fusion日志查看器(fuslogvw.exe)来记录程序集绑定,您应该能够准确地确定要加载的程序集以及加载上下文
更新
我很确定这是JSON转换器在运行时使用程序集导致的程序集绑定问题。在fusion日志中,我找到以下条目:
*** Assembly Binder Log Entry (05/06/2015 @ 02:01:38) ***
The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\Users\Tim\AppData\Local\JetBrains\Installations\ReSharperPlatformVs12_001\JetBrains.ReSharper.TaskRunner.CLR45.x64.exe
--- A detailed error log follows.
LOG: IJW explicit bind. File path:c:\users\tim\VS-Projects\StackOverflow\StackOverflow.30643046\bin\Debug\StackOverflow.30643046.dll.
LOG: IJW assembly bind returned a different path: C:\Users\Tim\AppData\Local\Temp\k3dpwn5u.uii\Machine Specifications Runner\assembly\dl3\6c41c492\c7eea8ec_279fd001\StackOverflow.30643046.dll. Use the file provided.
***装配活页夹日志条目(05/06/2015@02:01:38)***
手术是成功的。
绑定结果:hr=0x0。操作已成功完成。
从以下位置加载的程序集管理器:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
在可执行文件C:\Users\Tim\AppData\Local\JetBrains\Installations\ReSharperPlatformVs12\U 001\JetBrains.ReSharper.TaskRunner.CLR45.x64.exe下运行
---下面是详细的错误日志。
日志:IJW显式绑定。文件路径:c:\users\tim\VS Projects\StackOverflow\StackOverflow.30643046\bin\Debug\StackOverflow.30643046.dll。
日志:IJW程序集绑定返回了不同的路径:C:\Users\Tim\AppData\Local\Temp\k3dpwn5u.uii\Machine Specifications Runner\assembly\dl3\6c41c492\c7eea8ec_279fd001\StackOverflow.30643046.dll。使用提供的文件。
我还发现了这个:
*** Assembly Binder Log Entry (05/06/2015 @ 02:01:38) ***
The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\Users\Tim\AppData\Local\JetBrains\Installations\ReSharperPlatformVs12_001\JetBrains.ReSharper.TaskRunner.CLR45.x64.exe
--- A detailed error log follows.
WRN: The same assembly was loaded into multiple contexts of an application domain:
WRN: Context: Default | Domain ID: 2 | Assembly Name: StackOverflow.30643046, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
WRN: Context: Neither | Domain ID: 2 | Assembly Name: StackOverflow.30643046, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
WRN: This might lead to runtime failures.
WRN: It is recommended to inspect your application on whether this is intentional or not.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
*** Assembly Binder Log Entry (05/06/2015 @ 02:04:41) ***
The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\Users\Tim\AppData\Local\JetBrains\Installations\ReSharperPlatformVs12_001\JetBrains.ReSharper.TaskRunner.CLR45.x64.exe
--- A detailed error log follows.
WRN: The same assembly was loaded into multiple contexts of an application domain:
WRN: Context: Default | Domain ID: 2 | Assembly Name: StackOverflow.30643046, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
WRN: Context: Neither | Domain ID: 2 | Assembly Name: StackOverflow.30643046, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
WRN: This might lead to runtime failures.
WRN: It is recommended to inspect your application on whether this is intentional or not.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
*** Assembly Binder Log Entry (05/06/2015 @ 02:04:42) ***
The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\Users\Tim\AppData\Local\JetBrains\Installations\ReSharperPlatformVs12_001\JetBrains.ReSharper.TaskRunner.CLR45.x64.exe
--- A detailed error log follows.
WRN: The same assembly was loaded into multiple contexts of an application domain:
WRN: Context: Default | Domain ID: 2 | Assembly Name: StackOverflow.30643046, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
WRN: Context: Neither | Domain ID: 2 | Assembly Name: StackOverflow.30643046, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
WRN: This might lead to runtime failures.
WRN: It is recommended to inspect your application on whether this is intentional or not.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
***装配活页夹日志条目(05/06/2015@02:01:38)***
手术是成功的。
绑定结果:hr=0x0。操作已成功完成。
从以下位置加载的程序集管理器:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
在可执行文件C:\Users\Tim\AppData\Local\JetBrains\Installations\ReSharperPlatformVs12\U 001\JetBrains.ReSharper.TaskRunner.CLR45.x64.exe下运行
---下面是详细的错误日志。
警告:同一程序集已加载到应用程序域的多个上下文中:
警告:上下文:默认|域ID:2 |程序集名称:StackOverflow.30643046,版本=1.0.0.0,区域性=中立,PublicKeyToken=null
警告:上下文:NOTE |域ID:2 |程序集名称:StackOverflow.30643046,版本=1.0.0.0,区域性=neutral,PublicKeyToken=null
警告:这可能会导致运行时失败。
警告:建议检查您的应用程序是否有意这样做。
警告:见白皮书http://go.microsoft.com/fwlink/?LinkId=109270 有关此问题的更多信息和常见解决方案。
***装配活页夹日志条目(05/06/2015@02:04:41)***
手术是成功的。
绑定结果:hr=0x0。操作已成功完成。
从以下位置加载的程序集管理器:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
在可执行文件C:\Users\Tim\AppData\Local\JetBrains\Installations\ReSharperPlatformVs12\U 001\JetBrains.ReSharper.TaskRunner.CLR45.x64.exe下运行
---下面是详细的错误日志。
警告:同一程序集已加载到应用程序域的多个上下文中:
警告:上下文:默认|域ID:2 |程序集名称:StackOverflow.30643046,版本=1.0.0.0,区域性=中立,PublicKeyToken=null
警告:上下文:NOTE |域ID:2 |程序集名称:StackOverflow.30643046,版本=1.0.0.0,区域性=neutral,PublicKeyToken=null
警告:这可能会导致运行时失败。
警告:建议检查您的应用程序是否有意这样做。
警告:见白皮书http://go.microsoft.com/fwlink/?LinkId=109270 有关此问题的更多信息和常见解决方案。
***装配活页夹日志条目(05/06/2015@02:04:42)***
手术是成功的。
绑定结果:hr=0x0。操作已成功完成。
从以下位置加载的程序集管理器:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
联合国
*** Assembly Binder Log Entry (05/06/2015 @ 02:01:38) ***
The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\Users\Tim\AppData\Local\JetBrains\Installations\ReSharperPlatformVs12_001\JetBrains.ReSharper.TaskRunner.CLR45.x64.exe
--- A detailed error log follows.
WRN: The same assembly was loaded into multiple contexts of an application domain:
WRN: Context: Default | Domain ID: 2 | Assembly Name: StackOverflow.30643046, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
WRN: Context: Neither | Domain ID: 2 | Assembly Name: StackOverflow.30643046, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
WRN: This might lead to runtime failures.
WRN: It is recommended to inspect your application on whether this is intentional or not.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
*** Assembly Binder Log Entry (05/06/2015 @ 02:04:41) ***
The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\Users\Tim\AppData\Local\JetBrains\Installations\ReSharperPlatformVs12_001\JetBrains.ReSharper.TaskRunner.CLR45.x64.exe
--- A detailed error log follows.
WRN: The same assembly was loaded into multiple contexts of an application domain:
WRN: Context: Default | Domain ID: 2 | Assembly Name: StackOverflow.30643046, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
WRN: Context: Neither | Domain ID: 2 | Assembly Name: StackOverflow.30643046, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
WRN: This might lead to runtime failures.
WRN: It is recommended to inspect your application on whether this is intentional or not.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
*** Assembly Binder Log Entry (05/06/2015 @ 02:04:42) ***
The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable C:\Users\Tim\AppData\Local\JetBrains\Installations\ReSharperPlatformVs12_001\JetBrains.ReSharper.TaskRunner.CLR45.x64.exe
--- A detailed error log follows.
WRN: The same assembly was loaded into multiple contexts of an application domain:
WRN: Context: Default | Domain ID: 2 | Assembly Name: StackOverflow.30643046, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
WRN: Context: Neither | Domain ID: 2 | Assembly Name: StackOverflow.30643046, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
WRN: This might lead to runtime failures.
WRN: It is recommended to inspect your application on whether this is intentional or not.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.