Last active
February 21, 2019 16:37
-
-
Save asaladino/e5b856b06a1ae4936def136672161073 to your computer and use it in GitHub Desktop.
UserManager in my unit tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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