How to populate method's return values with AutoFixture

1k views Asked by At

I would like to auto-generate a method's return values in a non-deterministic manner, i.e. with every call/test run to I expect a method to return random value. For the moment it returns always default values of the method calls:

    public interface IReturn
    {
        bool BoolMethod();
        int IntMethod();
    }

    [Fact]
    public void AllReturnsFromAutofixtureMethodsAreFalse()
    {
        IFixture fixture = new Fixture().Customize(new AutoNSubstituteCustomization());
        IEnumerable<IReturn> theBools = fixture.CreateMany<IReturn>();
        Assert.True(theBools.All(tb => tb.BoolMethod() == false));
        Assert.True(theBools.All(tb => tb.IntMethod() == 0));
    }

In questions like this one can find a way how to achieve similar thing for properties, however not methods. Any idea?

1

There are 1 answers

0
sgnsajgon On BEST ANSWER

I haven't used AutoFixture with NSubstitute customization, however by analogy to Moq library, It seems that AutoConfiguredNSubstituteCustomization class should be used to achieve advanced AutoFixture faking behavior such that you want to. Using it you can auto-generate results from stubbed methods, as well as it is possible to create mock objects chains, injecting freezed objects into chain, an so on.