title: NSubstitute DbSet / IQueryable<T>
body: <p>So EntityFramework 6 is a lot better testable then previous versions. And there are <a href="http://msdn.microsoft.com/en-us/data/dn314429#queryTest">some nice examples</a> on the internet for frameworks like Moq, but the case is, I prefer using NSubstitute. I've got the "non-query" examples translated to work with the use of NSubstitute, but I can't get my head around the 'query-test'.</p>
  
  <p>How does Moq's <code>items.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.Provider).Returns(data.Provider);</code> translate to NSubstitute? I thought something like <code>((IQueryable&lt;T&gt;) items).Provider.Returns(data.Provider);</code> but that didn't work. I've also tried <code>items.AsQueryable().Provider.Returns(data.Provider);</code> but that didn't work either.</p>
  
  <p>The exeption I'm getting is:</p>
  
  <blockquote>
    <p>"System.NotImplementedException : The member 'IQueryable.Provider'
    has not been implemented on type 'DbSet<code>1Proxy' which inherits from
    'DbSet</code>1'. Test doubles for 'DbSet`1' must provide implementations of
    methods and properties that are used."</p>
  </blockquote>
  
  <p>So let me quote the code example from the link above. This code sample uses Moq to mock the DbContext and DbSet.</p>
  
  <pre><code>public void GetAllBlogs_orders_by_name()
  {
    // Arrange
    var data = new List&lt;Blog&gt;
    {
       new Blog { Name = "BBB" },
       new Blog { Name = "ZZZ" },
       new Blog { Name = "AAA" },
    }.AsQueryable();
  
    var mockSet = new Mock&lt;DbSet&lt;Blog&gt;&gt;();
    mockSet.As&lt;IQueryable&lt;Blog&gt;&gt;().Setup(m =&gt; m.Provider).Returns(data.Provider);
    mockSet.As&lt;IQueryable&lt;Blog&gt;&gt;().Setup(m =&gt; m.Expression).Returns(data.Expression);
    mockSet.As&lt;IQueryable&lt;Blog&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(data.ElementType);
    mockSet.As&lt;IQueryable&lt;Blog&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(data.GetEnumerator());
  
    var mockContext = new Mock&lt;BloggingContext&gt;();
    mockContext.Setup(c =&gt; c.Blogs).Returns(mockSet.Object);
  
    // ...
  }
  </code></pre>
  
  <p>And this is how far I come with NSubstitute</p>
  
  <pre><code>public void GetAllBlogs_orders_by_name()
  {
    // Arrange
    var data = new List&lt;Blog&gt;
    {
       new Blog { Name = "BBB" },
       new Blog { Name = "ZZZ" },
       new Blog { Name = "AAA" },
    }.AsQueryable();
  
    var mockSet = Substitute.For&lt;DbSet&lt;Blog&gt;&gt;();
    // it's the next four lines I don't get to work
    ((IQueryable&lt;Blog&gt;) mockSet).Provider.Returns(data.Provider);
    ((IQueryable&lt;Blog&gt;) mockSet).Expression.Returns(data.Expression);
    ((IQueryable&lt;Blog&gt;) mockSet).ElementType.Returns(data.ElementType);
    ((IQueryable&lt;Blog&gt;) mockSet).GetEnumerator().Returns(data.GetEnumerator());
  
    var mockContext = Substitute.For&lt;BloggingContext&gt;();
    mockContext.Blogs.Returns(mockSet);
  
    // ...
  }
  </code></pre>
  
  <p>So the question is; How does one Substitute a property of IQueryable (like Provider)?</p>