title: Mocking DbSet<T>.FromSqlRaw in EF Core 5
body: <p>On of the repository methods happens to use <code>FromSqlRaw</code> to fetch the data from SQL Server via a stored procedure.</p>
  <p>I am trying to write unit test around it but no luck so far.</p>
  <p>This is a sample snippet of how far I got</p>
  <pre><code>var fixture = new Fixture();
  var context = new Mock&lt;IMyDbContext&gt;();
  var dbSet = new Mock&lt;DbSet&lt;MyEntity&gt;&gt;();
              
  var data = fixture.Build&lt;MyEntity&gt;().CreateMany().AsQueryable();
  dbSet.As&lt;IQueryable&lt;MyEntity&gt;&gt;().Setup(m =&gt; m.Provider).Returns(data.Provider);
  dbSet.As&lt;IQueryable&lt;MyEntity&gt;&gt;().Setup(m =&gt; m.Expression).Returns(data.Expression);
  dbSet.As&lt;IQueryable&lt;MyEntity&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(data.ElementType);
  dbSet.As&lt;IQueryable&lt;MyEntity&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(data.GetEnumerator());
  context.Setup(c =&gt; c.Set&lt;MyEntity&gt;()).Returns(dbSet.Object);
  
  var repository = new MyRepository(context.Object);
   
  var result = await repository.GetData();
  </code></pre>
  <p>Bit I got stuck with the following exception</p>
  <blockquote>
  <p>System.InvalidCastException : Unable to cast object of type 'System.Linq.Expressions.ConstantExpression' to type 'Microsoft.EntityFrameworkCore.Query.QueryRootExpression'</p>
  </blockquote>
  <p>The project uses EF Core 5.</p>
  <p>Any idea how to solve this cast exception?</p>