title: C# Unit testing with Fake database context using a HashSet (pluralsight code)(New Q)
body: <p><strong>The short version:</strong></p>
  <p>In this video, Mr. Scott Allen <a href="http://www.pluralsight-training.net/microsoft/players/PSODPlayer.aspx?author=scott-allen&amp;name=mvc3-building-tdd&amp;mode=live&amp;clip=3&amp;course=aspdotnet-mvc3-intro" rel="noreferrer">explains how to test a controller</a>.
  But he does not show the full code of the class: <code>FakeDbContext</code>.
  Is there someone who can help me finish it? He shows the class at, 06:15 min in the video for &quot;testing controllers&quot;.</p>
  <p><strong>The long version</strong></p>
  <p>At school I have a elective where we learn C#. My exam project is a ASP site using MVC3.
  To learn it fast, i have seen videos from PluralSight. My question is about some code that are in <a href="http://www.pluralsight-training.net/microsoft/players/PSODPlayer.aspx?author=scott-allen&amp;name=mvc3-building-tdd&amp;mode=live&amp;clip=3&amp;course=aspdotnet-mvc3-intro" rel="noreferrer">this video</a>
  He explains how to test controllers. So i tried:
  I have made a controller which has a simple index method:</p>
  <pre><code>public class Round1Controller : Controller
  {
      IDbContext _db;
  
      public Round1Controller()
      {
          _db = new Entities();
      }
      
      public Round1Controller(IDbContext db)
      {
          _db = db;
      }
      
      public ActionResult Index()
      {
          var model = _db.ELECTIVES.ToList();
  
          return View(model);
      }
  </code></pre>
  <p>As you can see i have already tried to make a context.
  The index method is the one i want to TEST.
  The next thing he does is to make a class called, <code>FakeDbContext</code>, in the test project.</p>
  <p>But sadly he only show a part of the code, and i have used a lot of hours trying to figure out how he creates a <code>get</code> method to a <code>HashSet</code>.</p>
  <p>Here is the code that you can see from the video:</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using EESS.Models;
  
  namespace EESS.Tests
  {
  class FakeDbContext : IDbContext
  {
      public IQueryable&lt;Restaurant&gt; Restaurants
      {
          get { return _map.Get&lt;Restaurant&gt;().asQueryable(); }
          set { _map.Use&lt;Restaurant&gt;(value); }
      }
      public IQueryable&lt;Review&gt; Reviews
      {
          get { return _map.Get&lt;Review&gt;().asQueryable(); }
          set { _map.Use&lt;Review&gt;(value); }
      }
  
      public int SaveChanges()
      {
          ChangesSaved = true;
          return 0;
      }
  
      public bool ChangesSaved { get; set; }
  
      public T Attach&lt;T&gt;(T entity) where T : class
      {
          _map.Get&lt;T&gt;().Add(entity);
          return entity;
      }
      public T Add&lt;T&gt;(T entity) where T : class
      {
          _map.Get&lt;T&gt;().Add(entity);
          return entity;
      }
  
      public T Delete&lt;T&gt;(T entity) where T : class
      {
          _map.Get&lt;T&gt;().Remove(entity);
          return entity;
      }
  
      SetMap _map = new SetMap();
  
      class SetMap : KeyedCollection&lt;Type, object&gt;
      {
          public HashSet&lt;T&gt; Use&lt;T&gt;(IEnumerable&lt;T&gt; sourceData)
          {
              var set = new HashSet&lt;T&gt;(sourceData);
              if (Contains(typeof(T)))
              {
                  Remove(typeof(T));
              }
              Add(set);
              return set;
          }
      }
  }
  }
  </code></pre>
  <p>To end the long version, my problem is i get a error on <code>_Map.Get</code>.
  <code>Does not contain a definition or extension method</code>.</p>
  <p><strong>EDIT! PART 2:</strong></p>
  <p>After @xelibrion great answer it finally worked.</p>
  <p>But then another problem come up.</p>
  <p>The IDbContext class looks like this:</p>
  <pre><code>public interface IDbContext
      {
          IQueryable&lt;ELECTIVES&gt; ELECTIVES { get; }
          int SaveChanges();
          T Attach&lt;T&gt;(T entity) where T : class;
          T Add&lt;T&gt;(T entity) where T : class;
          T Delete&lt;T&gt;(T entity) where T : class;
      }
      
  </code></pre>
  <p>When i add this interface to my Entities Class it offcouse expect me to implement the methods.
  PluralSight implements them like this:</p>
  <pre><code>public DbSet&lt;ELECTIVES&gt; electives { get; set; }
  
      IQueryable&lt;ELECTIVES&gt; IDbContext.ELECTIVES
      {
          get { return electives; }
      }
  
      int IDbContext.SaveChanges()
      {
          return SaveChanges();
      }
      
      T IDbContext.Add&lt;T&gt;(T entity)
      {
          return Set&lt;T&gt;().Add(entity);
      }
      
      T IDbContext.Delete&lt;T&gt;(T entity)
      {
          return Set&lt;T&gt;().Remove(entity);
      }
      
      T IDbContext.Attach&lt;T&gt;(T entity)
      {
          
          var entry = Entry(entity);
          entry.State = System.Data.EntityState.Modified;
          return entity;
          return Set&lt;T&gt;().Add(entity);
      }
  </code></pre>
  <p>But my &quot;_dbModel.Designer.cs&quot; class from my entity model does not know what Set and Entry is, and only suggest i make a method stub. There are a lot more code in this class, so if its need just ask for the rest :) I have changed Restaurants to Electives since thats the table name i my DB.</p>
  <p>Is it a &quot;using&quot; i have forgotten? I have seen the video again, and he does not have a method stub in his DB class.</p>