mercredi 27 janvier 2016

How to mock Asp.net identity UserManager to list user of Specific role

I am trying to write a Unit test of index method of my ClientController.

Here is my ClientController:

   public ClientController(ApplicationUserManager clientManager, ApplicationRoleManager roleManager)
    {
        ClientManager = clientManager;
        RoleManager = roleManager;
    }

    private ApplicationRoleManager _roleManager;
    public ApplicationRoleManager RoleManager
    {
        get
        {
            return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>();
        }
        set
        {
            _roleManager = value;
        }
    }

    private ApplicationUserManager _clientManager;
    public ApplicationUserManager ClientManager
    {
        get
        {
            return _clientManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        set
        {
            _clientManager = value;
        }
    }

    public async Task<ActionResult> Index(string filter, string error, string searchName, int? page, int? records)
    {
        List<string> filterCriteria = new List<string>();
        filterCriteria.Add("Choose Email");
        var listClients = new List<ApplicationUser>();
        // Get the list of clients ( users with client role )
        foreach (var user in ClientManager.Users.Where(u => u.IsActive == true && (u.FirstNames.Contains(searchName) || u.LastName.Contains(searchName)
        || searchName == null)).OrderBy(u => u.FirstNames).ToList())
        {
            if (await ClientManager.IsInRoleAsync(user.Id, "Client"))
            {
                listClients.Add(user);
                filterCriteria.Add(user.Email);
            }
        }
        ViewBag.emails = new SelectList(filterCriteria);
        ViewBag.error = error;

        if (filter == null || filter.Equals("Choose Email"))
        {
            return View(listClients.ToList().ToPagedList(page ?? 1, records ?? 15));
        }
        else
        {
            return View();
        }

And here is my attempt to write a unit test of it.

 [TestMethod]
    public void Index_Get_RetrievesAllClientFromRepository()
    {
        // Arrange,
        ApplicationUser Client1 = GetClientNamed("1", 1, 1, DateTime.Now, "Abc", "Abc", "Xyz", "343433443", "abc@xyz.com", "M", "06091980-ABSD");
        var userStore = new Mock<IUserStore<ApplicationUser>>();

        var userManager = new UserManager<ApplicationUser>(userStore.Object);
        userStore.Setup(x => x.CreateAsync(Client1))
            .Returns(Task.FromResult(IdentityResult.Success));

        userStore.Setup(x => x.FindByNameAsync(Client1.UserName))
                    .Returns(Task.FromResult(Client1));

        var roleStore = new Mock<IRoleStore<IdentityRole>>();
        var roleManager = new Mock<ApplicationRoleManager>(roleStore.Object);

        var controller = new ClientController(
            userStore.Object as ApplicationUserManager, roleManager.Object);
        // Act
        var resultTask = controller.Index("Choose Email", "", "", 1, 15);
        resultTask.Wait();
        var result = resultTask.Result;
        var model = (List<ApplicationUser>)((ViewResult)result).Model;
        CollectionAssert.Contains(model, Client1);
    }

userStore.Object always come null. I am quite newbie in unit testing and I have looked for many solution but there isn't such use case. Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire