4

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?

Community
  • 1
  • 1
Piotr Cierpich
  • 427
  • 2
  • 11
  • 4
    For Moq there is the adequate *AutoConfiguredMoqCustomization*. I guess you should look at [AutoConfiguredNSubstituteCustomization](https://github.com/AutoFixture/AutoFixture/blob/master/Src/AutoNSubstitute/AutoConfiguredNSubstituteCustomization.cs). – sgnsajgon Nov 12 '14 at 10:52
  • This is it, thanks! It seems I used a slightly outdated version of AutoNSubstitute integration, which is why I couldn't find it. Wish I could mark your comment as an answer.. – Piotr Cierpich Nov 12 '14 at 11:13

1 Answers1

3

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.

sgnsajgon
  • 664
  • 2
  • 13
  • 56