I am trying to test a service class that consumers a repository service. I have customizations set up that I believe should work with my repository service, but instead return default Anonymous results. If you look at the code sample below, I'm trying to get the "Foo" objects that I registered in the Customization Class back when I call the svc.GetFoos method, instead I get nothing:
void Main()
{
var fixture = new Fixture().Customize(
new CompositeCustomization(
new Customization(),
new AutoMoqCustomization()));
var svc = fixture.CreateAnonymous<Bar>();
Console.Write(svc.GetFoos().Count());
}
// Define other methods and classes here
public class Bar
{
public IQueryable<Foo> GetFoos()
{
return _rep.Query<Foo>();
}
public Bar(IRepository rep) { _rep = rep; }
private IRepository _rep;
}
public class Foo
{
public string Name {get;set;}
}
public class Customization : ICustomization
{
public void Customize(IFixture fixture)
{
var f = fixture
.Build<Foo>()
.With(x => x.Name, "FromCustomize")
.CreateMany(2)
.AsQueryable();
fixture.Register<IQueryable<Foo>>(() => f);
}
}
public interface IRepository
{
IQueryable<T> Query<T>();
}
If I add the following code to the Main method after the fixture instantiation, it works how I want, but then I'm manually setting up my mocks, and I'm not sure what AutoFixture AutoMoq is getting me:
var mock = fixture.Freeze<Mock<IRepository>>();
mock
.Setup(x => x.Query<Foo>())
.Returns(fixture.CreateAnonymous<IQueryable<Foo>>);
Thanks.