Question

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

Was it helpful?

Solution

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());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top