Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I get AutoFixture AutoMoq to return results from injected services in an instantiated object?
    primarykey
    data
    text
    <p>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: </p> <pre><code>void Main() { var fixture = new Fixture().Customize( new CompositeCustomization( new Customization(), new AutoMoqCustomization())); var svc = fixture.CreateAnonymous&lt;Bar&gt;(); Console.Write(svc.GetFoos().Count()); } // Define other methods and classes here public class Bar { public IQueryable&lt;Foo&gt; GetFoos() { return _rep.Query&lt;Foo&gt;(); } 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&lt;Foo&gt;() .With(x =&gt; x.Name, "FromCustomize") .CreateMany(2) .AsQueryable(); fixture.Register&lt;IQueryable&lt;Foo&gt;&gt;(() =&gt; f); } } public interface IRepository { IQueryable&lt;T&gt; Query&lt;T&gt;(); } </code></pre> <p>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: </p> <pre><code>var mock = fixture.Freeze&lt;Mock&lt;IRepository&gt;&gt;(); mock .Setup(x =&gt; x.Query&lt;Foo&gt;()) .Returns(fixture.CreateAnonymous&lt;IQueryable&lt;Foo&gt;&gt;); </code></pre> <p>Thanks. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload