Skip to content

Instantly share code, notes, and snippets.

@asaladino
Last active February 21, 2019 16:37
Show Gist options
  • Select an option

  • Save asaladino/e5b856b06a1ae4936def136672161073 to your computer and use it in GitHub Desktop.

Select an option

Save asaladino/e5b856b06a1ae4936def136672161073 to your computer and use it in GitHub Desktop.
UserManager in my unit tests.
using System.Web;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using System.Net.Http;
using Microsoft.Owin;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Data.Entity.Migrations;
namespace MyWebsite.Repositories.Tests
{
[TestClass()]
public class MyRepositoryTests
{
[TestMethod()]
public void CreateTest()
{
var db = new ApplicationDbContext();
var userStore = new UserStore<ApplicationUser>(db);
var userManage = new UserManager<ApplicationUser>(userStore);
// Find all my users.
var users = db.Users.FirstOrDefault();
// Create a new user.
var user = new ApplicationUser
{
UserName = "testUser1",
Email = "test@codingsimply.com",
EmailConfirmed = true
};
userManage.Create(user, "testpassword1!");
// Add the user to some roles.
db.Roles.AddOrUpdate(r => r.Name, new IdentityRole { Name = UserEditViewModel.ADMIN });
db.SaveChanges();
userManage.AddToRole(user.Id, UserEditViewModel.ADMIN);
db.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment