7

I use AutoFixture with AutoMoq. I try to create a fake instance of a class which has a property with a getter but without a setter. I expect AutoFixture to configure the mock so it will return the given value even there is no setter.

My code is something like that:

var data = new List<Data>() { new Data() };
var userManager = fixture.Build<IRepository>()
      //.With(x => x.Data, data)
        .CreateAnonymous();
Mock.Get(userManager).Setup(x => x.Data).Returns(data);

Unfortunately, the "With" method does not work in this case because auto fixture says that Data does not have any setter which why I have to set the value afterwards with a direct call to the mock.

Is there a way that auto fixture can do this by its own so I do not need the last line of code?

Edit: I made a mistake, the code example does not work. It should be

var data = new List<Data>() { new Data() };
var userManager = fixture.CreateAnonymous<IRepository>();
Mock.Get(userManager).Setup(x => x.Data).Returns(data)

Nevertheless, it would be good if there would be a with method for fake instance.

HerrLoesch
  • 1,023
  • 1
  • 13
  • 24
  • In some cases, you may want Moq's `SetupGet` rather than `Setup` (but that's obv not your problem). FWIW I'm pretty sure AF's built in stuff doesnt deal with what you're looking for (which seems to be less establishing state and more being able to fudge/substitute the read side). Can you confirm you're not dealing with objects that have a clear way to push in state (e.g. via ctor params?) – Ruben Bartelink Mar 11 '13 at 20:59
  • Related: http://stackoverflow.com/questions/12963019/how-do-i-get-autofixture-automoq-to-return-results-from-injected-services-in-an – Mark Seemann Mar 11 '13 at 21:14

1 Answers1

7

AutoFixture.AutoMoq doesn't set up your Test Doubles for you.

If you want to avoid having to specify a setup for IRepository.Data in each and every test case, you can package the setup in a Customization.

public class RepositoryCustomization : ICustomization
{
    public void Customize(IFixture fixture)
    {
        fixture.Register(() =>
        {
            var td = new Mock<IRepository>();
            td.SetupGet(r => r.Data).Returns(fixture.CreateMany<Data>());
            return td.Object;
        });
    }
}

With that, the following test passes:

[Fact]
public void AutoProperty()
{
    var fixture = new Fixture().Customize(new RepositoryCustomization());
    var repo = fixture.Create<IRepository>();
    Assert.NotEmpty(repo.Data);
}

In theory it would be possible to write automated code that reflects over the members of an interface and sets up a return value for each member, but IMO, this should never be default behaviour for AutoFixture.AutoMoq.

Community
  • 1
  • 1
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • 1
    Actually, I would like to enhance the "With" method. But it's not a problem if it's not possible. Thank you for your answer. – HerrLoesch Mar 14 '13 at 10:21
  • In which way would you like to enhance the `With` method. What's missing from it? – Mark Seemann Mar 14 '13 at 11:41
  • 1
    @MarkSeemann I believe he's saying that the default is fine as it is, but preferably there would be something similar to `.With(x => x.Data, data)` that could act as if it assigned to that specific property which has no setter (when using AutoMoq). – Cameron S Jul 03 '13 at 22:54
  • FWIW, here's a related, but slightly different, post about auto-filling properties on Mocks: http://blog.ploeh.dk/2013/04/08/how-to-automatically-populate-properties-with-automoq – Mark Seemann Jul 04 '13 at 05:29