AutoFoture与AutoMoq和具体对象注入

时间:2013-11-15 14:23:47

标签: c# autofixture moq-3 automoq

我正面临一个与AutoFixtureAutoMoqCustomization相关的奇怪问题,以及它如何处理具体类的自动锁定问题。我怀疑我没有很好地使用它,但想知道问题是什么。首先是她的一些背景。假设我有一个我想测试的课程:

public class IdentityApplicationService
{
    public IdentityApplicationService(
        TenantProvisioningService tenantProvisioningService)
    {
        // guard clause etc.
        _tenantProvisioningService = tenantProvisioningService;
    }
}

及其依赖类TenantProvisioningService(TenantProvisioningService的依赖项在这里不相关,因为它们将被自动模拟,我在测试时并不关心):

public class TenantProvisioningService
{
    readonly IRoleRepository _roleRepository;
    readonly ITenantRepository _tenantRepository;
    readonly IUserRepository _userRepository;

    public TenantProvisioningService(
        ITenantRepository tenantRepository,
        IUserRepository userRepository,
        IRoleRepository roleRepository)
    {
        this._roleRepository = roleRepository;
        this._tenantRepository = tenantRepository;
        this._userRepository = userRepository;
    }
}

这是我的简单测试:

[Fact]
public void ShouldReturnTenantWhenCallingProvisionTenant()
{
    var fixture = new Fixture().Customize(new AutoMoqCustomization());
    var mockTenantProvisioningService =
        fixture.Freeze<Mock<TenantProvisioningService>>();
    var sut = fixture.Create<IdentityApplicationService>();
    var command = new ProvisionTenantCommand(
        "bla",
        "bla SaaS platform",
        "superadmin",
        "superadmin",
        "admin@bla.bla",
        null,
        null,
        null,
        null,
        null,
        null,
        null);
    var tenant = sut.ProvisionTenant(command);

    // some asserts
}

这不起作用,因为当我调用fixture.Create<IdentityApplicationService>()然后在它的构造函数中注入一个具体的TenantProvisioningService而不是你可以在 mockTenantProvisioningService.Object 中找到的代理的。

如果我像这样重写测试(注意夹具注入线),一切都按预期工作(至少由我起作用):)

[Fact]
public void ShouldReturnTenantWhenCallingProvisionTenant()
{
    var fixture = new Fixture().Customize(new AutoMoqCustomization());
    var mockTenantProvisioningService =
        fixture.Freeze<Mock<TenantProvisioningService>>();
    fixture.Inject(mockTenantProvisioningService.Object);
    var sut = fixture.Create<IdentityApplicationService>();
    var command = new ProvisionTenantCommand(
        "bla",
        "bla SaaS platform",
        "superadmin",
        "superadmin",
        "admin@bla.bla",
        null,
        null,
        null,
        null,
        null,
        null,
        null);
    var tenant = sut.ProvisionTenant(command);

    // some asserts
}

所以我的问题是:我做错了还是它应该是这样?如果没有,请告诉我为什么AutoFixture的行为是这样的。

1 个答案:

答案 0 :(得分:6)

正如Mark Seemann points out所说,这是预期的行为。

以下是the original post更新的代码,以便与当前版本的AutoFixture一起使用:

Func<ISpecimenBuilder, bool> concreteFilter = 
    sb => !(sb is MethodInvoker);

var relays = new FilteringRelays(concreteFilter);

var fixture = new Fixture(relays).Customize(
    new AutoMoqCustomization(
        new MockRelay(
            new TrueRequestSpecification())));

FilteringRelays类保持不变。