title: Mocking a SignInManager
body: <p>New to unit testing with Moq and xUnit. I am trying to mock a <code>SignInManager</code> that is used in a controller constructor to build a unit test. The documentation that I can find for the <code>SignInManager</code> constructor says it accepts a <code>UserManager</code> and <code>AuthenticationManager</code> object: <a href="https://msdn.microsoft.com/en-us/library/mt173769(v=vs.108).aspx#M:Microsoft.AspNet.Identity.Owin.SignInManager" rel="noreferrer">https://msdn.microsoft.com/en-us/library/mt173769(v=vs.108).aspx#M:Microsoft.AspNet.Identity.Owin.SignInManager</a>`2.</p>
  
  <p>When I try to mock the controller, I get an error saying it was unable to instantiate a proxy of the <code>SignInManager</code> and <code>AuthenticationManager</code> classes.</p>
  
  <p>Error: </p>
  
  <blockquote>
    <p>"Message:
    Castle.DynamicProxy.InvalidProxyConstructorArgumentsException : Can
    not instantiate proxy of class:
    Microsoft.AspNetCore.Identity.SignInManager1[[Models.AppUser,
    , Version=1.0.0.0, Culture=neutral,
    PublicKeyToken=null]]. Could not find a constructor that would match
    given arguments: Castle.Proxies.UserManager`1Proxy
    Castle.Proxies.AuthenticationManagerProxy"</p>
  </blockquote>
  
  <p>The unit test:</p>
  
  <pre><code>public void Can_Send_Password_Reset_Email()
  {
      //Arrange
      //create mock services
      Mock&lt;IEmailService&gt; mockEmailService = new Mock&lt;IEmailService&gt;();
      Mock&lt;ILessonRepository&gt; mockRepo = new Mock&lt;ILessonRepository&gt;();
      Mock&lt;UserManager&lt;AppUser&gt;&gt; mockUsrMgr = GetMockUserManager();
      var mockSignInMgr = GetMockSignInManager();
      Mock&lt;UserValidator&lt;AppUser&gt;&gt; mockUsrVal = new Mock&lt;UserValidator&lt;AppUser&gt;&gt;();
      Mock&lt;PasswordValidator&lt;AppUser&gt;&gt; mockPwdVal = new Mock&lt;PasswordValidator&lt;AppUser&gt;&gt;();
      Mock&lt;PasswordHasher&lt;AppUser&gt;&gt; mockPwdHshr = new Mock&lt;PasswordHasher&lt;AppUser&gt;&gt;();
      Mock&lt;ForgotPasswordModel&gt; model = new Mock&lt;ForgotPasswordModel&gt;();
      model.Object.Email = "joe@example.com";
  
      var user = new AppUser();
      var token = mockUsrMgr.Object.GeneratePasswordResetTokenAsync(user).Result;
  
      //create mock temporary data, needed for controller message
      Mock&lt;ITempDataDictionary&gt; tempData = new Mock&lt;ITempDataDictionary&gt;();
      //create the controller
      //ERROR ON THIS LINE
      AccountController controller = new AccountController(mockUsrMgr.Object, mockSignInMgr.Object, mockUsrVal.Object, mockPwdVal.Object, mockPwdHshr.Object, mockEmailService.Object)
      {
          TempData = tempData.Object
      };
  
      //Act
      //the controller should call the email action method
      controller.PasswordResetEmail(model.Object);
  
      //Assert
      //verify that the email service method was called one time
      mockEmailService.Verify(m =&gt; m.PasswordResetMessage(user, "Test Email"), Times.Once());
  }
  </code></pre>
  
  <p>The SignInManager mocking function:</p>
  
  <pre><code>//create a mock SignInManager class
  private Mock&lt;SignInManager&lt;AppUser&gt;&gt; GetMockSignInManager()
  {
      var mockUsrMgr = GetMockUserManager();
      var mockAuthMgr = new Mock&lt;AuthenticationManager&gt;();
      return new Mock&lt;SignInManager&lt;AppUser&gt;&gt;(mockUsrMgr.Object, mockAuthMgr.Object);
  }
  </code></pre>
  
  <p>The <code>GetMockUserManager()</code> works fine in other unit tests and doesn't appear to be the issue.</p>