Is there a workaround for [HostType ("Moles")] when working with anonymous methods in MSpec?

I am using Pex and Moles for my low-level unit testing, but I am also learning MSpec to validate business logic and would like to use Moles for consistency. The problem I think is that MSPec uses anonymous methods, so there is no way to apply the HostType ("Moles") attribute. For example:

Because of = () =>
   employeeList = EmployeeManager.GetUsersByRoles(rolesToLoad);

It should_return_a_list_of_employees = () =>
   employeeList.ShouldNotBeNull();

      

I'm mocking a role provider that gets called inside "GetUsersByRoles" and when I try to run this test through MSpec I get the standard error "Moles requires testing to be included in the process" with instructions to add [HostType ("Moles") ] to my testing method. Is there a workaround or other option available here?

Side note: I downloaded MSMSpec.tt and modified it to include the attribute in the generated VSTests, but I would like to be able to run MSpec tests directly through my own runner or TestDriven.net so that I can get a friendly output for BAS and business owners.

+2


source to share


1 answer


A workaround is to replace the anonymous method with one that is not. Moling Mspec is basically impossible.

Moles are not capable of combining anonymous methods. The reason is that methods must be targeted to be disabled. Anonymous methods are not implicitly addressable because they are generated and referenced at runtime. Simply put, you cannot call an anonymous method through a class because it is ... well ... anonymous.



The Moles Manual states: "Moles can be used to bypass any .NET method, including non-virtual and static methods in private types." Therefore, acting on the assumption that Mols is using reflection to identify members of a class is a safe bet. Anything that cannot be called through a delegate, action, or Func cannot be processed.

+1


source







All Articles