AutoFixture.AutoMoq: setpoint is read-only

I am using AutoFixture with AutoMoq. I am trying to create a fake instance of a class that has a property with a getter, but without a setter. I expect AutoFixture to customize the layout so that it returns the given value, even if there is no customizer.

My code looks something like this:

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, method ā€œCā€ does not work in this case, because auto fixture says that Data does not have any setter, so I have to set the value after that by directly invoking the layout.

Is there any way auto fixture can do this on its own, so I don't need the last line of code?

Edit: I made a mistake, the sample code 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) 

However, it would be nice if there was a method with a fake instance.

+4
source share
1 answer

AutoFixture.AutoMoq does not set your double points for you .

If you do not want to specify a setting for IRepository.Data in each test case, you can package the installation in 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; }); } } 

In this case, the following testing takes place:

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

In theory, one could write automated code that reflects the elements of an interface and sets a return value for each member, but IMO, this should never be the default for AutoFixture.AutoMoq.

+3
source

All Articles