views:

30

answers:

1

I'm currently trying to implement StructureMap's AutoMocking functionality and I need help with getting the mocked .

I have a Test method as follows:

[Test]
public void DirctoryResult_Returns_Groups()
{
    var autoMocker = new RhinoAutoMocker<GroupController>(MockMode.AAA);

    GroupController controller = autoMocker.ClassUnderTest;

    var directoryResult = controller.DirectoryResult("b");

    var fundDirectoryViewModel = (FundDirectoryViewModel)directoryResult.ViewData.Model;

    Assert.IsNotNull(fundDirectoryViewModel.Groups);
}

Currently the test is failing because fundDirectoryViewModel.Groups is null.

The real implementation of DirectoryResult is as follows:

private readonly IGroupService _groupService;
public PartialViewResult DirectoryResult(string query)
{
    return PartialView(new FundDirectoryViewModel
    {
        Groups =_groupService.GetGroupsByQuery(query)
    });
}

where _groupService.GetGroupsByQuery(query) uses an interface to IGroupRepository to read data from the database. Of course, I don't want my test to read data from the actual database, but can somebody tell me how to get mock data for it?

What do I need to do to get the AutoMocker to mock the fake data for me?

update:

for reference, this is the definition of GroupService & GroupRepository

public class GroupService : IGroupService
{
    private readonly IGroupRepository _groupRepository;

    public GroupService(IGroupRepository groupRepository)
    {
        _groupRepository = groupRepository;
    }

    public IList<CompanyGroupInfo> GetGroupsByQuery(string query)
    {
        return _groupRepository.GetGroupsByQuery(query);
    }
}

public class GroupRepository : DataUniverseRepository, IGroupRepository
{
    public GroupRepository(ISession session)
    {
        _session = session;
    }

    public IList<CompanyGroupInfo> GetGroupsByQuery(string query)
    {
        // dig into the database and return stuff with _session..
    }
}
A: 

I've been informed that the question was wrong. Automocker doesn't mock data like that. It's up to me to specify the fake data with Rhino Mocks.

This works:

[Test]
public void DirctoryResult_Returns_Groups()
{
    var service = autoMocker.Get<IGroupService>();
    service.Expect(srv => srv.GetGroupsByQuery(Arg<string>.Is.Anything))
        .Return(new List<CompanyGroupInfo>
                    {
                        new CompanyGroupInfo(),
                        new CompanyGroupInfo(),
                        new CompanyGroupInfo()
                    });

    service.Replay();

    var directoryResult = _controller.DirectoryResult("b");

    var fundDirectoryViewModel = (FundDirectoryViewModel)directoryResult.ViewData.Model;

    Assert.That(fundDirectoryViewModel.Groups.Count, Is.EqualTo(3));

    service.AssertWasCalled(srv => srv.GetGroupsByQuery(Arg<string>.Is.Equal("b")));
}
DaveDev