Use of Method stubbed in Moles inside a Shims context

85 views Asked by At

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.

2

There are 2 answers

0
Technophilic On BEST ANSWER

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.

3
Peter On

The only problem with your initial code is the missing [HostType(Moles)]. Next you have to be sure you are running your tests with the Visual Studio test runner. If you have installed an other test runner, it could be that it is not loading the Moles host.

moles-requires-tests-to-be-in