lundi 25 avril 2016

NULL Reference when creating a user from a unit test controller

i would like to write a unit test for my user create class. Since hours I try to find a way that works. I have a Controller called "UserAdministrationController" which uses some of the default code from "AccountController".

/// <summary>
/// Gets  a new instance of the ApplicationUserManager class.
/// </summary>
public ApplicationUserManager UserManager
{
    get
    {
        return this.userManager ?? this.HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
    }
    private set
    {
        this.userManager = value;
    }
}

If I call

var result = this.UserManager.Create(user, userModel.Password);

From the Create(CreateUserViewModel usermodel) Class everything works fine. Also the data is sent to the controller when i use the reference in my unit test

var newUserModel = new CreateUserViewModel(...);
var controller = new UserAdministrationController();
// Act
ActionResult actionResult = controller.Create(newUserModel);

I get a System.NullRefferenceExeption in this line:

return this.userManager ?? this.HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();

As far as i know the problem lays in the path and not in a missing userManager. But I can not fix it. Is there something i should change in the constructor of the UserAdministrationController that allows an external access to the current path? Or am I misreading the problem?

Thank you

Aucun commentaire:

Enregistrer un commentaire