0

I'm new to Pex and Moles and i want to make use of parametrized unit tests. I am using constructor injection and I want to create a moles stub for my parameter.

public UserLogic(IUserRepository userRepository)
{
    _userRepository = userRepository;
}

The documentation Ive read says Moles will generate a stub type for my repository of SIUserRepository. But I cant figure out how to generate the stub. Would anyone be able to provide an example. Thanks

ministrymason
  • 1,783
  • 2
  • 20
  • 33
  • The type `SIUserRepository` is generated automatically, you just need to add a moles assembly for the assembly containing the type `IUserRepository`. Did you mean to ask "how to create an instance of the stub type"? – Gebb Dec 19 '11 at 16:13

1 Answers1

0

I'll assume you haven't gotten as far as creating a Moles assembly yet. Here's some basic steps to follow;

  1. in your unit test project, expand the references, and right-click the assembly which contains the type IUserRepository - select 'Add Moles Assembly';
  2. you'll now have Moles stubs & moles available for that assembly, under a '.Moles' namespace, so if you had MyAsssembly.SomeNamespace.IUserRepository, you'll now have a stub type available as MyAssembly.SomeNameSpace.Moles.SUserRepository

Now, in some UserLogic_Test method, you can refer to the stub like so;

[TestMethod]
public void UserLogic_Test()
{
    MyAssembly.SomeNameSpace.Moles.SUserRepository mock = new SUserRepository();
    UserLogic o = new UserLogic(mock);
    Assert.AreEqual(1, o.SomeMethod());
}
RJ Lohan
  • 6,497
  • 3
  • 34
  • 54
  • One problem I've run into with Stubs not being generated correctly is when the .moles file corresponding to your project has a attribute. I'm not sure who added it there, but it sure caused me some grief to find! So, if you have done as suggested by RJ, and still aren't getting stubs to generate, make sure to check your .moles file! – kodjeff1 May 07 '14 at 15:28