title: Mocking EF core dbcontext and dbset
body: <p>I am using ASP.NET Core 2.2, EF Core and MOQ. When I run the test I am getting this error:</p>
  
  <blockquote>
    <p>Message: System.NotSupportedException : Invalid setup on a non-virtual (overridable in VB) member: x => x.Movies</p>
  </blockquote>
  
  <p>What I am doing wrong?</p>
  
  <pre><code>public class MovieRepositoryTest
  {
      private readonly MovieRepository _sut;
  
      public MovieRepositoryTest()
      {
          var moviesMock = CreateDbSetMock(GetFakeListOfMovies());
          var mockDbContext = new Mock&lt;MovieDbContext&gt;();
          mockDbContext.Setup(x =&gt; x.Movies).Returns(moviesMock.Object);
          _sut = new MovieRepository(mockDbContext.Object);
      }
  
      [Fact]
      public void GetAll_WhenCalled_ReturnsAllItems()
      {
          //Act
          var items = _sut.GetAll();
  
          //Assert
          Assert.Equal(3, items.Count());
      }
  
      private IEnumerable&lt;Movie&gt; GetFakeListOfMovies()
      {
          var movies = new List&lt;Movie&gt;
          {
              new Movie {Id = 1, Title = "Movie 1", YearOfRelease = 2018, Genre = "Action"},
              new Movie {Id = 2, Title = "Movie 2", YearOfRelease = 2018, Genre = "Action"},
              new Movie {Id = 3, Title = "Movie 3", YearOfRelease = 2019, Genre = "Action"}
          };
  
          return movies;
      }
  
      private static Mock&lt;DbSet&lt;T&gt;&gt; CreateDbSetMock&lt;T&gt;(IEnumerable&lt;T&gt; elements) where T : class
      {
          var elementsAsQueryable = elements.AsQueryable();
          var dbSetMock = new Mock&lt;DbSet&lt;T&gt;&gt;();
  
          dbSetMock.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.Provider).Returns(elementsAsQueryable.Provider);
          dbSetMock.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.Expression).Returns(elementsAsQueryable.Expression);
          dbSetMock.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(elementsAsQueryable.ElementType);
          dbSetMock.As&lt;IQueryable&lt;T&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(elementsAsQueryable.GetEnumerator());
  
          return dbSetMock;
      }
    }
  </code></pre>
  
  <p>And this is my DB Context, with the <code>Movie</code> dbSet:</p>
  
  <pre><code>public class MovieDbContext: DbContext
  {
      public MovieDbContext(DbContextOptions&lt;MovieDbContext&gt; options) : base(options)
      {
  
      }
  
      public DbSet&lt;Movie&gt; Movies { get; set; }
  }
  </code></pre>
  
  <p>And the Repository with the method <code>GetAll</code> to be tested:</p>
  
  <pre><code> public class MovieRepository: IMovieRepository
  {
      private readonly MovieDbContext _moviesDbContext;
      public MovieRepository(MovieDbContext moviesDbContext)
      {
          _moviesDbContext = moviesDbContext;
      }
  
      public IEnumerable&lt;Movie&gt; GetAll()
      {
          return _moviesDbContext.Movies;
      }
  }
  </code></pre>