Recently while going through the automated test cases of a project I came across a piece of code which was something like this.
[TestMethod]
public void UpdateTtWebScResearchIdt()
{
using (ShimsContext.Create())
{
// Some code
SomeNamespace.Moles.MSubCom.StringFormatStringStringArray = (x, y) => "gLibErr";
//Assert
}
}
When I debug this test method, the compiler shows the following error
MolesInvalidOperationException.
At the line where Moles method is stubbed i.e.
SomeNamespace.Moles.MSubCom.StringFormatStringStringArray = (x, y) => "gLibErr";
The detailed message shows this.
"Moles requires tests to be IN an instrumented process. In Visual Studio Unit Test, add the following attribute to your unit test method:
add this attribute
[TestMethod]
[HostType(Moles)]
public void Test()
{
...
}
Extensions are also available for most unit test frameworks. Please refer to the Moles manual.
But adding the aforementioned attribute does not solve my problem either.
I think that the use of moles inside a shimmed method is problematic. I need an another opinion on this (or many for that matter). And if someone can suggest a solution that'd be awesome.
Thanks.
I could not find why it was written like this so I rewrote the entire test method using fakes assembly. Now it is working fine.