title: System.NotSupportedException : Unsupported expression: x => x
body: <p>I'm currently trying to moq my <code>Cafe</code> <code>Get</code> method which will throw a <code>ArgumentNullexception</code> if the cafe ID is not found.</p>
  <h3>Error</h3>
  <blockquote>
  <p>System.NotSupportedException : Unsupported expression: x =&gt; x.Cafe
  Non-overridable members (here: Context.get_Cafe) may not be used in setup / verification expressions.</p>
  </blockquote>
  <p>Is this occurring because moq is unable to handle one of the setup expressions?</p>
  <h3>Unit test</h3>
  <pre class="lang-cs prettyprint-override"><code>[Fact]
  public async Task GetCafeByIdAsync_Should_Throw_ArgumentNullException()
  {
      var cafe = new List&lt;Cafe&gt;()
      {
          new Cafe { Name = &quot;Hanna&quot;, CafeId = 1},
          new Cafe { Name = &quot;Bella&quot;, CafeId = 2 }
      }.AsQueryable();
  
      var mockSet = new Mock&lt;DbSet&lt;Cafe&gt;&gt;();
      mockSet.As&lt;IQueryable&lt;Cafe&gt;&gt;().Setup(m =&gt; m.Provider).Returns(cafe.Provider);
      mockSet.As&lt;IQueryable&lt;Cafe&gt;&gt;().Setup(m =&gt; m.Expression).Returns(cafe.Expression);
      mockSet.As&lt;IQueryable&lt;Cafe&gt;&gt;().Setup(m =&gt; m.ElementType).Returns(cafe.ElementType);
      mockSet.As&lt;IQueryable&lt;Cafe&gt;&gt;().Setup(m =&gt; m.GetEnumerator()).Returns(cafe.GetEnumerator());
  
      var mapper = new MapperConfiguration(cfg =&gt;
      {
          cfg.AddProfile(new AutoMapperProfile());
      }).CreateMapper();
  
      var contextMock = new Mock&lt;Context&gt;();
      contextMock.Setup(x =&gt; x.Cafe).Returns(mockSet.Object); //failing here
  
      var cafeService = new CafeService(contextMock.Object, mapper);
  
      await Assert.ThrowsAsync&lt;ArgumentNullException&gt;(() =&gt; cafeService.Get(2));
  }
  </code></pre>
  <h3>SUT</h3>
  <pre class="lang-cs prettyprint-override"><code>public async Task&lt;VersionResponse&gt; Get(int cafeId)
  {
      var cafe = await _context.Cafe.Where(w =&gt; w.CafeId == cafeId).ToResponse().FirstOrDefaultAsync();
      return new VersionResponse()
      {
          Data = cafe
      };
  }
  </code></pre>