当EF尝试从App.config加载信息时,我最近开始在我的单元测试(NUnit)代码中遇到以下异常:
System.Runtime.Serialization.SerializationException : Type is not resolved for member [my type name], [my assembly name]
Run Code Online (Sandbox Code Playgroud)
NUnit GUI运行器和R#的VS集成运行器都会发生这种情况.这是一个快速的单元测试,可以重现这个问题:
[Test]
public void Test()
{
// adding
// ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// here fixes things, probably because the configuration is cached
CallContext.LogicalSetData("FooSlot", new Foo()); // removing this line fixes things
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // fails with above exception
}
[Serializable] // removing this gives a different exception complaining that Foo is not Serializable
public class Foo // : MarshalByRefObject // this fixes things
{
}
Run Code Online (Sandbox Code Playgroud)
使用融合日志查看器,我发现失败是由于
FILE_NOT_FOUND …
我有一个带有C#dll项目的Visual Studio解决方案.该解决方案还有一个测试项目,它引用了C#dll项目的输出.引用的项目dll已设置Copy Local = true.
如果我从Visual Studio运行此测试,它可以正常工作.
但是如果我从MSBuild任务运行它,由于某种原因,MSTest不会将引用的C#dll复制到MSTest工作文件夹,因此测试失败.奇怪的是,所有其他引用的库都被复制到MSTest工作文件夹.如果我放了一个
[DeploymentItem(@"PleaseCopyThis.dll")]
Run Code Online (Sandbox Code Playgroud)
在我的测试方法之前,最后它被复制到MSTest工作文件夹,我的测试运行正常.
但是为什么Visual Studio只复制不属于解决方案的引用dll,但不复制引用的项目dll?