Handling specimen creation inconsistencies between AutoFixture and Moq

此生再无相见时 提交于 2019-11-29 11:03:44

As there's no extensibility point in Moq that enables AutoFixture to hook in and supply a value of ThingOne, there's not a whole lot you can do.

However, you can use the SetReturnsDefault<T> method of Moq. Modifying the above test would then be like this:

[Theory, BasicConventions]
public void WhyCannotInstantiateProxyOfClass(
    ThingOne one, ThingTwo two, IThingBuilders builder, SomeClass sut)
{
    Assert.IsAssignableFrom<IThings>(one);
    Assert.IsAssignableFrom<IThings>(two);
    Mock.Get(builder).SetReturnsDefault(one); // Add this to make the test pass

    var actual = sut.GetCommands(builder);

    Assert.Equal(1, actual.OfType<ThingOne>().Count());
    Assert.Equal(1, actual.OfType<ThingTwo>().Count());
}

This is a bit easier than having to write a specific Setup/Returns pair, but not much. You could move that code to an AutoFixture Customization, but again, since this is a generic method on a a Mock instance, you'll explicitly need to call this for e.g. ThingOne in order to set the default for that return type. Not particularly flexible.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!