-3

I'm using AutoFixture to populate a fairly huge interface object, but I'm getting a null (or default value) for every property. I've tried both:

var fixture = new Fixture();
var input = fixture.Create<Mock<ICustomer>>();

And:

var fixture = new Fixture();
fixture.Register(() => Mock.Of<ICustomer>());
var input = fixture.Create<ICustomer>();

In both cases, none of the properties on the resulting ICustomer are populated. I'm guessing I've missed something obvious?

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
Chris Surfleet
  • 2,462
  • 4
  • 28
  • 40
  • You register new instance of mocked `ICustomer`. So you didn't use benefits of AutoFixture which generate "dummy" values. For mocked instance you still need provide some default values – Fabio Oct 12 '17 at 11:18
  • To those who itch to close this question as a duplicate: reconsider. It's hardly a duplicate, as the accepted answer implies. – Mark Seemann Oct 12 '17 at 12:39

1 Answers1

3

You can use the AutoFixture.AutoMoq package for this:

[Fact]
public void HowToCreateAnAutoConfiguredMoq()
{
    var fixture = new Fixture().Customize(new AutoConfiguredMoqCustomization());
    var c = fixture.Create<ICustomer>();
    Assert.NotEqual(default(string), c.Name);
}

Personally, I'm not a big fan of AutoConfiguredMoqCustomization, because I think it makes the tests too implicit. I believe that explicit is better than implicit, so I normally prefer instead using AutoMoqCustomization instead, and combine that with explicitly configuring those behaviours that matter for the test in question.

See also How to configure AutoMoq to set up all properties.

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • 1
    Lovely! Thanks! I agree on the implicit nature of this stuff, but I'm dealing with over 100 properties so its at the point of 'unmanageable' – Chris Surfleet Oct 12 '17 at 12:08
  • Thanks for explicitly mention of value of explicit test configuration – Fabio Oct 13 '17 at 07:07