title: MVC4 Unit test NSubstitute Could not find a call to return from
body: <p>I have a MVC4 web application I'm unit testing right now. It uses entity framework for the database portion. I'm using NSubstitute to mock the database. This code is basically copied and pasted from another site which works fine, so I hope I'm just missing something super simple.</p>
  
  <p>Thanks in advance!</p>
  
  <p>Applications table in SQL:</p>
  
  <pre><code>AppID   | ApplicationName
  ----------------------------
  1       | MyCoolApplication
  2       | MyOtherApplication
  </code></pre>
  
  <p>Entity created the Application class:</p>
  
  <pre><code>public class Application
  {
      public int AppID { get; set; }
      public string ApplicationName { get; set; }
  }
  </code></pre>
  
  <p>The mock section of the unit test looks like this:</p>
  
  <pre><code>var mockDb = Substitute.For&lt;MyCoolApplicationsEntities&gt;();
  
  var applications = new List&lt;Application&gt;
  {
      new Application {AppID = 1, ApplicationName = "MyCoolApplication"},
      new Application {AppID = 2, ApplicationName = "MyOtherApplication"},
  };
  
  var mockApplicationSet = Substitute.For&lt;IDbSet&lt;Application&gt;, DbSet&lt;Application&gt;&gt;();
  mockApplicationSet.Provider.Returns(applications.AsQueryable().Provider);
  mockApplicationSet.Expression.Returns(applications.AsQueryable().Expression);
  mockApplicationSet.ElementType.Returns(applications.AsQueryable().ElementType);
  mockApplicationSet.GetEnumerator().Returns(applications.AsQueryable().GetEnumerator());
  
  mockApplicationSet.When(q =&gt; q.Add(Arg.Any&lt;Application&gt;()))
      .Do(q =&gt; applications.Add(q.Arg&lt;Application&gt;()));
  
  mockApplicationSet.When(q =&gt; q.Remove(Arg.Any&lt;Application&gt;()))
      .Do(q =&gt; applications.Remove(q.Arg&lt;Application&gt;()));
  
  
  mockDb.Applications.Returns(mockApplicationSet); //This is the line creating the error
  </code></pre>
  
  <p>The full error is:</p>
  
  <blockquote>
    <p>Test method
    MyProjectName.Controllers.MyControllerTest.TestOfSectionImTesting
    threw exception: 
    NSubstitute.Exceptions.CouldNotSetReturnDueToNoLastCallException:
    Could not find a call to return from.</p>
    
    <p>Make sure you called Returns() after calling your substitute (for
    example: mySub.SomeMethod().Returns(value)), and that you are not
    configuring other substitutes within Returns() (for example, avoid
    this: mySub.SomeMethod().Returns(ConfigOtherSub())).</p>
    
    <p>If you substituted for a class rather than an interface, check that
    the call to your substitute was on a virtual/abstract member. Return
    values cannot be configured for non-virtual/non-abstract members.</p>
    
    <p>Correct use: </p>
  
  <pre><code>mySub.SomeMethod().Returns(returnValue);
  </code></pre>
    
    <p>Potentially problematic use:</p>
  
  <pre><code>mySub.SomeMethod().Returns(ConfigOtherSub());
  </code></pre>
    
    <p>Instead try: </p>
  
  <pre><code>var returnValue = ConfigOtherSub(); 
  
  mySub.SomeMethod().Returns(returnValue);
  </code></pre>
  </blockquote>
  
  <p>But that doesn't work in my environment because Applications isn't a method. Like I said, this works fine in another site of mine, so it's got to be something basic I'm missing. Nothing I've found online has been helpful with my particular case. I updated to the newest version of NSubstitute and I uninstalled/reinstalled, but still have got nothing.</p>
  
  <p>Again, thanks in advance!</p>