public ContentsController(HimzoDbContext context, UserManager <User> userManager = null) { _context = context; _userManager = userManager; }
public MockHimzoDb() { DbContextOptionsBuilder options = new DbContextOptionsBuilder(); var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = "TestHimzoDb.db" }; var connectionString = connectionStringBuilder.ToString(); var connection = new SqliteConnection(connectionString); options.UseSqlite(connection); MockHimzoDbContext = new HimzoDbContext(options.Options); MockHimzoDbContext.Database.EnsureDeleted(); MockHimzoDbContext.Database.EnsureCreated(); foreach (User user in UserMockList) { MockHimzoDbContext.Users.Add(user); } foreach (Role role in RoleMockList) { MockHimzoDbContext.Roles.Add(role); } foreach (Comment comment in CommentMockList) { MockHimzoDbContext.Comments.Add(comment); } foreach (Image image in ImageMockList) { MockHimzoDbContext.Images.Add(image); } foreach (Content content in ContentMockList) { MockHimzoDbContext.Contents.Add(content); } foreach (Order order in OrderMockList) { MockHimzoDbContext.Orders.Add(order); } MockHimzoDbContext.SaveChanges(); // ================================================================ // Setup UserManager var store = new Mock <IUserStore <User> >(); MockUserManager = new Mock <UserManager <User> >(store.Object, null, null, null, null, null, null, null, null); MockUserManager.Object.UserValidators.Add(new UserValidator <User>()); MockUserManager.Object.PasswordValidators.Add(new PasswordValidator <User>()); MockUserManager.Setup(x => x.IsInRoleAsync(It.IsAny <User>(), It.IsAny <string>())).ReturnsAsync(true); MockUserManager.Setup(x => x.DeleteAsync(It.IsAny <User>())).ReturnsAsync(IdentityResult.Success); MockUserManager.Setup(x => x.CreateAsync(It.IsAny <User>(), It.IsAny <string>())).ReturnsAsync(IdentityResult.Success).Callback <User, string>((x, y) => UserMockList.Add(x)); MockUserManager.Setup(x => x.UpdateAsync(It.IsAny <User>())).ReturnsAsync(IdentityResult.Success); MockUserManager.Setup(x => x.GetUserAsync(It.IsAny <ClaimsPrincipal>())).ReturnsAsync(UserMockList[0]); MockUserManager.Setup(x => x.FindByEmailAsync(It.IsAny <string>())).ReturnsAsync(UserMockList[0]); MockUserManager.Setup(x => x.GetRolesAsync(It.IsAny <User>())).ReturnsAsync(new List <string>() { "User" }); // ================================================================ // Setup SignInManager /*MockSignInManager = new Mock<SignInManager<User>>(MockUserManager.Object, * new Mock<IHttpContextAccessor>().Object, * new Mock<IUserClaimsPrincipalFactory<User>>().Object, * new Mock<IOptions<IdentityOptions>>().Object, * new Mock<ILogger<SignInManager<User>>>().Object, * new Mock<IAuthenticationSchemeProvider>().Object); */ // ================================================================ }