title: Unit testing my custom membership provider
body: <p>I have a custom membership provider which connects to a user repository like this:</p>
  
  <pre><code>public class MyMembershipProvider : MembershipProvider {
     [Inject]
     public IUserRepository UserRepository { get; set; }
     ...
     //Required membership methods
  }
  </code></pre>
  
  <p>I am using ninject for my DI. Now I would like to test the provider, and have a mock user repository injected to allow me to do this. So something like:</p>
  
  <pre><code>        ... 
          IList&lt;User&gt; users = new List&lt;User&gt; {
              new User { Email="matt@test.com", 
                         UserName="matt@test.com", 
                         Password="test"
              }
          };
          var mock = new Mock&lt;IUserRepository&gt;();
  
          mock.Setup(mr =&gt; mr.FindByUsername(
              It.IsAny&lt;string&gt;())).Returns((string s) =&gt; users.Where(
              x =&gt; x.UserName.Equals(s,StringComparison.OrdinalIgnoreCase)).Single());
          ...
  </code></pre>
  
  <p>And here is where I am not certain how to proceed, how do I get my mocked repository injected into my provider so that when a unit test that makes calls to the provider uses this mock repository?</p>
  
  <p>Am I asking the right questions here?</p>
  
  <p><strong>EDIT - My final solution</strong></p>
  
  <p>For what it is worth I moved away from using mock to using an InMemory repository to maintain state so the provider would properly test certain functions. Right now I am only using this to test things like my provider. I ended up with:</p>
  
  <p>generic InMemoryRepository:</p>
  
  <pre><code>class InMemoryRepository&lt;TEntity&gt; : IRepository&lt;TEntity&gt; where TEntity : class {
      private int _incrementer = 0;
      public Dictionary&lt;int, TEntity&gt; List = new Dictionary&lt;int, TEntity&gt;();
      public string IDPropertyName {get; set;}
  
      public void Create(TEntity entity) {
          _incrementer++;
          entity.GetType().GetProperties().First(p =&gt; p.Name == IDPropertyName).SetValue(entity, _incrementer, null);
          List.Add(_incrementer,entity);
      }
  
      public TEntity GetById(int id) {
          return List[id];
      }
  
      public void Delete(TEntity entity) {
          var key = (int)entity.GetType().GetProperties().First(p =&gt; p.Name == IDPropertyName).GetValue(entity, null);
          List.Remove(key);
      }
  
      public void Update(TEntity entity) {
          var key = (int)entity.GetType().GetProperties().First(p =&gt; p.Name == IDPropertyName).GetValue(entity, null);
          List[key] = entity;
      }
  }
  </code></pre>
  
  <p>Then my actual user repository - I do not have generic ID fields which is why I am using the IDPropertyName variable:</p>
  
  <pre><code>class InMemoryUserRepository : InMemoryRepository&lt;User&gt;, IUserRepository {
      public InMemoryUserRepository() {
          this.IDPropertyName = "UserID";
      }
  
      public IQueryable&lt;User&gt; Users {
          get { return List.Select(x =&gt; x.Value).AsQueryable(); }
      }
  
      public User FindByUsername(string username) {
          int key = List.SingleOrDefault(x=&gt;x.Value.UserName.Equals(username, StringComparison.OrdinalIgnoreCase)).Key;
          return List[key];
      }
  }
  </code></pre>
  
  <p>My membership test base class:</p>
  
  <pre><code>[TestClass]
  public class MyMembershipProviderTestsBaseClass : IntegrationTestsBase {
      protected MyMembershipProvider _provider;
      protected NameValueCollection _config;
      protected MembershipCreateStatus _status = new MembershipCreateStatus();
  
      [TestInitialize]
      public override void Initialize() {
          base.Initialize();
  
          // setup the membership provider
          _provider = new MyMembershipProvider();
  
          MembershipSection section = (MembershipSection) ConfigurationManager.GetSection("system.web/membership");
          NameValueCollection collection = section.Providers["MyMembershipProvider"].Parameters;
          _provider.Initialize(null, collection);
          _status = new MembershipCreateStatus();
      }
  
      [TestCleanup]
      public override void TestCleanup() {
          base.TestCleanup();
      }
  }
  </code></pre>
  
  <p>Then my test:</p>
  
  <pre><code>    [TestMethod]
      public void Membership_CreateUser() {
          _provider.UserRepository = new InMemoryUserRepository();
          _provider.CreateUser(_email, out _status);
          Assert.AreEqual(MembershipCreateStatus.Success, _status);
      }
  </code></pre>
  
  <p>This answer provided inspiration: <a href="https://stackoverflow.com/a/13073558/1803682">https://stackoverflow.com/a/13073558/1803682</a></p>