vendredi 23 janvier 2015

How do I write Web Api unit test which I should get info from the UserManager

I've searched stackoverflow and googled four a couple of hours and still not found any solution for my "trivial" problem.


For example, I want to test this web api controller



public IHttpActionResult GetFavorites()
{
var userApplication = UserManager.FindById(User.Identity.GetUserId());
var user = db.Users.FirstOrDefault(u => u.Username == userApplication.UserName);
if (user == null)
return Unauthorized();

return Ok(db.Favorites.Where(fav => fav.UserID == user.UserID));
}


I have tried to mock a context for my controller and also tried to change the Thread.CurrentPrincipal.


Some would be able to help me out on that, what I'm doing wrong?


Aucun commentaire:

Enregistrer un commentaire