如何让 StructureMap 的 AutoMocker 来模拟虚假数据?

发布于 2024-09-11 14:46:44 字数 1826 浏览 2 评论 0原文

我目前正在尝试实现 StructureMap 的 AutoMocking 功能,并且我需要帮助来获取模拟的 .

我有一个测试方法如下:

[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);
}

当前测试失败,因为 fundDirectoryViewModel.Groups 为空。

DirectoryResult 的实际实现如下:

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

其中 _groupService.GetGroupsByQuery(query) 使用 IGroupRepository 的接口从数据库读取数据。当然,我不希望我的测试从实际数据库中读取数据,但是有人可以告诉我如何获取它的模拟数据吗?

我需要做什么才能让 AutoMocker 为我模拟虚假数据?

更新:

供参考,这是 GroupService 和 GroupService 的定义。组存储库

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..
    }
}

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..
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

番薯 2024-09-18 14:46:44

我被告知这个问题是错误的。 Automocker 不会这样模拟数据。由我来使用 Rhino Mocks 指定虚假数据。

这有效:

[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")));
}

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")));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文