1

I have following interface and its repository class

Interface

public interface IIdentityRepository
{
    bool CreateUser(ApplicationUser user, string password);
}

Repository

public class IdentityRepository : IIdentityRepository
{
    ApplicationDbContext dbContext;

    public IdentityRepository()
    {
        dbContext = new ApplicationDbContext(); // if none supplied
    }

    public bool CreateUser(ApplicationUser user, string password)
    {
        var userManager = new UserManager<ApplicationUser>(
            new UserStore<ApplicationUser>(dbContext));
        var idResult = userManager.Create(user, password);

        return idResult.Succeeded;
    }    
}

public class UserManager : UserManager<ApplicationUser>
{
    public UserManager()
        : base(new UserStore<ApplicationUser>(new ApplicationDbContext()))
    {
    }
}

This is the test class I'm trying to write for CreateUser method In this method I'm using AppplicationUser as my model.

[TestClass]
public class IdentityRepositoryTest
{
    private IdentityRepository _identitityRepo;
    private Mock<IIdentityRepository> _identitityRepository;

    private List<ApplicationUser> _users;


    // initialize the test class
    [TestInitialize]
    public void TestSetup()
    {
        _identitityRepository = new Mock<IIdentityRepository>();
        _users = new List<ApplicationUser>();

        _identitityRepository.Setup(m => m.CreateUser(It.IsAny<ApplicationUser>())).Callback<ApplicationUser>(c => _users.CreateUser(c));

        _identitityRepo = new IdentityRepository();
    }

    #region Users

    // check valid number of user/s(1) existing in current DB
    [TestMethod]
    public void IsValidtNumberofUsersExist()
    {
        // Arrange           
        _users.Add(new ApplicationUser { UserName = "Kez" , Email = "kez@gmail.com" });

        // Act
        var result = _identitityRepo.GetAllUsers();
        Assert.IsNotNull(result);

        // Assert
        var numberOfRecords = result.ToList().Count;
        Assert.AreEqual(1, numberOfRecords);
    }

    #endregion
}

But in here I'm having following compile time error

enter image description here

EDIT :

Once I change above error line as follows error went away.

_identitityRepository.Setup(m => m.CreateUser(It.IsAny<ApplicationUser>(),"password")).Callba‌​ck<ApplicationUser>(‌​c => _users.Add(c));

but when I run this test, I'm getting following error

Result Message: Initialization method ProjectName.UnitTest.Common.IdentityRepositoryTest.TestSetup threw exception. System.ArgumentException: System.ArgumentException: Invalid callback. Setup on method with parameters (ApplicationUser,String) cannot invoke callback with parameters (ApplicationUser)..

kez
  • 2,273
  • 9
  • 64
  • 123
  • the `CreateUser(ApplicationUser user, string password)` takes 2 parameters but you are only setting up one parameter. Additionally you need to review your design. it is too tightly coupled to implementation concerns to be test friendly. You will encounter more problems as you go forward. – Nkosi Dec 30 '16 at 05:28
  • @Nkosi okay thanks I changed above line as follows, then error goes away `_identitityRepository.Setup(m => m.CreateUser(It.IsAny(),"password")).Callback(c => _users.Add(c));` to make this loosely coupled where should I change Test class or repository ? – kez Dec 30 '16 at 05:37
  • what do you mean by "loosely coupled". That mock-parameter doesn't make it more tightly coupled. it just takes care of the methods signature. – nozzleman Dec 30 '16 at 06:20
  • @nozzleman Nkosi said that `it is too tightly coupled to implementation concerns to be test friendly` there for I thought make this more loosely coupled, where should I change to reduce tightly coupled situation, is it Test class or repository ? – kez Dec 30 '16 at 06:24
  • @kez fix callback `.Callba‌​ck(‌​(u,p) => _users.Add(u));` or `.Callba‌​ck(‌​(ApplicationUser u,string p) => _users.Add(u));` – Nkosi Dec 30 '16 at 06:38
  • @kez as for tight coupling and implementation concerns. take a look at this answer http://stackoverflow.com/a/38673203/5233410 – Nkosi Dec 30 '16 at 06:43
  • @Nkosi I changed as the code like this `_identitityRepository.Setup(m => m.CreateUser(It.IsAny(), "password")).Callba‌​ck(‌​(ApplicationUser u, string p) => _users.Add(u));` but when I build this its saying following error `Unexpected character '‌` – kez Dec 30 '16 at 09:15

1 Answers1

0
_identitityRepository.Setup(m => m.CreateUser(It.IsAny<ApplicationUser>(),"password"))
    .Callba‌​ck<ApplicationUser>(‌​c => _users.Add(c));

Should be:

_identitityRepository.Setup(m => m.CreateUser(It.IsAny<ApplicationUser>(),"password"))
    .Callba‌​ck<ApplicationUser, string>(‌​(c, s) => _users.Add(c));

https://github.com/Moq/moq4/wiki/Quickstart#callbacks

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64