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();
}
}
}
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:
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);
}
}
}
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
using Microsoft.Moles.Framework;
is needed.