将 Moles 与 HtmlAgilityPack 结合使用时出现 TypeInitializationException
我正在尝试使用 Moles 在单独的程序集中测试非静态方法。当运行没有 [HostType("Moles")] 标签的测试时,测试运行良好。当我替换它时,我收到以下错误:
“‘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();
}
}
}
I am attempting to use Moles to test a non-static method in a separate assembly. When running the test without the [HostType("Moles")] tag, the test runs fine. When I replace it I receive the following error:
"The type initializer for 'HtmlAgilityPack.HtmlNode' threw an exception."
I have attached code samples that perform in an identical manner.
Any help would be great!
Class/method being called by the unit test
using System;
using HtmlAgilityPack;
using System.Web;
namespace HAPAndMoles
{
public class Class1
{
public void fooBar()
{
HtmlDocument foo = new HtmlDocument();
}
}
}
Unit Test
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();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我是否理解你的例子,因为事实上你不使用 Moles。
如果您只想“Mole”我们自己的非虚拟方法,则在测试项目的引用中,您只需右键单击测试项目的程序集并选择添加 Moles 程序集。这将创建一个 HAPAndMoles.Moles 引用。
然后添加相应的 using ,您就可以将您的类称为以 M 开头的“moled”(Class1 => MCLass1)。我向您展示一个测试 MClass1 行为的示例:
当我想要 mscorlib 的 Moles 时,我直接右键单击测试项目的引用,然后我可以为 mscorlib 添加 Moles 程序集。那么就
需要 了。
I'm not sure I understand your example because in fact you don't use Moles.
If you just want to "Mole" our own non-virtual method, in the references of your test project you just have to right-click on the assembly of the tested project and choose Add Moles Assembly. That will create an HAPAndMoles.Moles reference.
Then add the corresponding using and you can call your class "moled" starting with M (Class1 => MCLass1). I show you an example testing the MClass1 behaviour:
When I want Moles of mscorlib, I right-click directly on the references of the test project and I can Add Moles Assembly for mscorlib. Then the
is needed.