Handling specimen creation inconsistencies between AutoFixture and Moq

前端 未结 1 443
囚心锁ツ
囚心锁ツ 2020-12-19 12:03

I am using AutoMoqCustomization in my test conventions.

Consider the code below. Everything works great until I add a constructor to one of the concrete classes. W

相关标签:
1条回答
  • 2020-12-19 12:12

    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.

    0 讨论(0)
提交回复
热议问题