title: How to mock Entity Framework's FromSqlRaw method?
body: <p>I am writing a Unit Test and need to mock Entity Framework's .FromSqlRaw method. When the method is executed in the class under test, it throws following exception:</p>
  <blockquote>
  <p>System.InvalidOperationException: There is no method
  'FromSqlOnQueryable' on type
  'Microsoft.EntityFrameworkCore.RelationalQueryableExtensions' that
  matches the specified arguments.</p>
  </blockquote>
  <p>Following is class under test:</p>
  <pre><code>public class PowerConsumptionRepository : IPowerConsumptionRepository
      {
          private readonly IDatabaseContext _databaseContext;
          private readonly IDateTimeHelper _dateTimeHelper;
  
          public PowerConsumptionRepository(IDatabaseContext databaseContext, IDateTimeHelper dateTimeHelper)
          {
              _databaseContext = databaseContext;
              _dateTimeHelper = dateTimeHelper;
          }
          public List&lt;IntervalCategoryConsumptionModel&gt; GetCurrentPowerConsumption(string siteId)
          {
              var currentDate = _dateTimeHelper
                  .ConvertUtcToLocalDateTime(DateTime.UtcNow, ApplicationConstants.LocalTimeZone)
                  .ToString(&quot;yyyy-MM-dd&quot;);
              var currentDateParameter = new SqlParameter(&quot;currentDate&quot;, currentDate);
              var measurements = _databaseContext.IntervalPowerConsumptions
                  .FromSqlRaw(SqlQuery.CurrentIntervalPowerConsumption, currentDateParameter)
                  .AsNoTracking()
                  .ToList();
              return measurements;
          }
      }
  </code></pre>
  <p>Unit Test:</p>
  <pre><code>
      public class PowerConsumptionRepositoryTests
      {
          [Fact]
          public void TestTest()
          {
              var data = new List&lt;IntervalCategoryConsumptionModel&gt;
              {
                  new IntervalCategoryConsumptionModel
                  {
                      Id = 1,
                      Hvac = 10                    
                  },
                  new IntervalCategoryConsumptionModel
                  {
                      Id = 1,
                      Hvac = 10
                  }
              }.AsQueryable();
              var dateTimeHelper = Substitute.For&lt;IDateTimeHelper&gt;();
              dateTimeHelper.ConvertUtcToLocalDateTime(Arg.Any&lt;DateTime&gt;(), Arg.Any&lt;string&gt;()).Returns(DateTime.Now);
              var mockSet = Substitute.For&lt;DbSet&lt;IntervalCategoryConsumptionModel&gt;, IQueryable&lt;IntervalCategoryConsumptionModel&gt;&gt;();
              ((IQueryable&lt;IntervalCategoryConsumptionModel&gt;)mockSet).Provider.Returns(data.Provider);
              ((IQueryable&lt;IntervalCategoryConsumptionModel&gt;)mockSet).Expression.Returns(data.Expression);
              ((IQueryable&lt;IntervalCategoryConsumptionModel&gt;)mockSet).ElementType.Returns(data.ElementType);
              ((IQueryable&lt;IntervalCategoryConsumptionModel&gt;)mockSet).GetEnumerator().Returns(data.GetEnumerator());
              var context = Substitute.For&lt;IDatabaseContext&gt;();
              context.IntervalPowerConsumptions = (mockSet);
              var repo = new PowerConsumptionRepository(context, dateTimeHelper);
              var result = repo.GetCurrentPowerConsumption(Arg.Any&lt;string&gt;());
              result.Should().NotBeNull();
          }
      }
  </code></pre>