我目前正在编写一个单元测试框架,最终将运行用Visual Studio编写的标准单元测试。该框架目前无法正确使用访问器。考虑以下测试方法:创建类访问器的实例
[TestMethod()]
public void TestMethod()
{
ExampleMethods_Accessor target = null;
target = new ExampleMethods_Accessor();
target.SomeMethod();
}
在此示例中,访问器已由Visual Studio生成。使用Visual Studio的单元测试环境运行时,单元测试完美地工作正常。不过,我想从我的框架中调用TestMethod()。在“target = new ExampleMethods_Accessor()”行处抛出以下异常:
“Proband.ExampleMethods_Accessor”的类型初始值设定项引发了一个异常。
内部异常:
无法加载文件或程序:先证者,版本= 1.0.0.0,文化=中立,公钥=空...
有没有人如何在Microsoft单元测试的想法框架调用单元测试?我想这可能是由于缺少TestContext对象。在我的情况下,这是“空”。在Visual Studio中启动单元测试时,TestContext对象包含大量信息。难道是,我需要正确初始化它吗?它将如何初始化?
感谢所有帮助, 基督教
编辑:
我一直与存取正在工作的方式进行试验。我使用ILSpy查看Proband_Accessor.dll中生成的代码。事实证明,造成异常的指令是:
SomeClass_Accessor.m_privateType = new PrivateType("Probant, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "Probant.SomeClass");
我修改了单元测试代码是这样的(只是为了测试):
[TestMethod()]
[DeploymentItem("Proband.dll")]
public void SomeMethodTest()
{
ExampleMethods_Accessor target = null;
ExampleMethods c = null;
try
{
Assembly.Load("Proband, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"); // this works fine
PrivateType tx = new PrivateType(typeof(ExampleMethods)); // this works fine as well (also without loading the assembly)
PrivateType t = new PrivateType("Proband, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "Proband.ExampleMethods"); // this causes the exception
c = new ExampleMethods(); // this works fine
target = new ExampleMethods_Accessor(); // this causes the exception as well
}
catch (Exception ex)
{
Console.WriteLine();
}
int actual;
actual = target.SomeMethod();
}
我绝对不明白,为什么“新PrivateType(“先证者,版本......”不起作用。有没有人的想法?
什么是'Proband'装配,以及如何你有* *试图使其可用? –
你到底在写什么?似乎你使用MSTest框架,从你使用[TestMethod]。你正在写某种自定义测试_runner_吗? –
哦,我很抱歉没有解释:proband程序集是包含应该测试的代码的程序集。 ExampleMethods是proband程序集中的一个类,它包含私有方法(“SomeMethod”)。 – Christian