TypeInitializationException当使用Moles With HtmlAgilityPack
我正在尝试使用Moles来测试单独程序集中的非静态方法。在没有[HostType(“Moles”)]标签的情况下运行测试时,测试运行良好。当我替换它时,我收到以下错误:TypeInitializationException当使用Moles With HtmlAgilityPack
“'HtmlAgilityPack.HtmlNode'的类型初始值设定项引发异常。”
我附上了以相同方式执行的代码示例。
任何帮助将是伟大的!
类/方法,通过单元测试被称为
using System;
using HtmlAgilityPack;
using System.Web;
namespace HAPAndMoles
{
public class Class1
{
public void fooBar()
{
HtmlDocument foo = new HtmlDocument();
}
}
}
单元测试
using System;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using HAPAndMoles;
using Microsoft.Moles.Framework;
using HtmlAgilityPack;
using System.Web;
namespace HAPAndMoles
{
[TestClass]
public class UnitTest1
{
[TestMethod]
[HostType("Moles")]
public void TestMethod1()
{
Class1 bar = new Class1();
bar.fooBar();
}
}
}
我不知道我理解你的例子,因为其实你不使用痣。
如果您只是想要“鼹鼠”我们自己的非虚拟方法,在测试项目的参考文献中,您只需右键单击测试项目的程序集并选择添加Moles程序集。这将创建一个HAPAndMoles.Moles参考。
然后添加相应的使用,你可以调用你的类“moled”从M开始(Class1 => MCLass1)。我告诉你一个例子测试MCLASS1行为:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using HAPAndMoles;
using HAPAndMoles.Moles;
namespace HAPAndMoles {
[TestClass]
public class UnitTest1
{
[TestMethod]
[HostType("Moles")]
public void TestMethod1()
{
bool called = false;
var bar = new MClass1()
{
fooBar =() => called = true
};
((Class1)bar).fooBar();
Assert.IsTrue(called);
}
}
}
当我想要的mscorlib的痣,我直接在测试项目的引用右键单击我可以添加痣大会的mscorlib。然后
using Microsoft.Moles.Framework;
是必要的。
Jeco ...感谢您的信息。 我把我的例子中的一些东西放到了最小的形式。我原来的代码确实包含了对moled代码的使用。 在阅读您的回复之后,我注意到我忽略为HtmlAgilityPack程序集创建一个痣。 – 2011-05-12 05:13:44
您可以添加引发异常的完整堆栈跟踪,而不仅仅是第一行吗? – 2011-05-11 08:02:41