Exemplo n.º 1
0
 public UserServiceTests()
 {
     _mapper  = new MapperConfiguration(cfg => cfg.AddProfile <TestsMappingConfig>()).CreateMapper();
     _db      = new Mock <IShopUnitOfWork>();
     _service = new UserService(_db.Object, _mapper);
 }
Exemplo n.º 2
0
        public void Upsert()
        {
            var options = new DbContextOptionsBuilder <UsersDbContext>()
                          .UseInMemoryDatabase(databaseName: nameof(Upsert))
                          .Options;

            using (var context = new UsersDbContext(options))
            {
                var usersService = new UserService(context, config);

                var defaultuserRole = new UserRole
                {
                    Name = RoleConstants.REGULAR
                };
                context.UserRoles.Add(defaultuserRole);
                context.Entry(defaultuserRole).State = EntityState.Detached;

                var defaultuserRole2 = new UserRole
                {
                    Name = RoleConstants.ADMIN
                };
                context.UserRoles.Add(defaultuserRole2);
                context.Entry(defaultuserRole2).State = EntityState.Detached;

                UserRole userRoleRegular = new UserRole
                {
                    Name = RoleConstants.REGULAR
                };
                HistoryUserRole historyRegular = new HistoryUserRole
                {
                    UserRole = userRoleRegular,
                };
                List <HistoryUserRole> listRegular = new List <HistoryUserRole>
                {
                    historyRegular
                };
                User radu69 = new User
                {
                    Username        = "******",
                    HistoryUserRole = listRegular,
                    DateRegister    = DateTime.Now,
                    Password        = "******",
                    Email           = "*****@*****.**",
                    FirstName       = "radu",
                    LastName        = "radu"
                };
                context.Users.Add(radu69);
                context.SaveChanges();
            }

            using (var context = new UsersDbContext(options))
            {
                var usersService = new UserService(context, config);

                var defaultuserRole = new UserRole
                {
                    Name = RoleConstants.ADMIN
                };
                context.UserRoles.Add(defaultuserRole);
                context.SaveChanges();
                context.Entry(defaultuserRole).State = EntityState.Detached;

                UserRole userRole = new UserRole
                {
                    Name = RoleConstants.ADMIN
                };
                HistoryUserRole history = new HistoryUserRole
                {
                    UserRole = userRole,
                };
                List <HistoryUserRole> list = new List <HistoryUserRole>
                {
                    history
                };
                User user2 = new User
                {
                    Username        = "******",
                    HistoryUserRole = list,
                    DateRegister    = DateTime.Now,
                    Password        = "******",
                    Email           = "*****@*****.**",
                    FirstName       = "cristina",
                    LastName        = "cristina"
                };

                User firstAdded = new User
                {
                    Username     = "******",
                    DateRegister = DateTime.Now,
                    Password     = "******",
                    Email        = "*****@*****.**",
                    FirstName    = "radu",
                    LastName     = "radu"
                };

                var toUpdateWith = new UserPostModel
                {
                    FirstName = "Gimli",
                    LastName  = "Axeman",
                    UserName  = "******",
                    Email     = "*****@*****.**",
                    Password  = "******",
                    UserRole  = RoleConstants.ADMIN
                };

                var result = context.ChangeTracker.Entries()
                             .Where(t => t.State == EntityState.Unchanged);

                UserGetModel lalala = usersService.Upsert(1, toUpdateWith, user2);

                Assert.AreNotEqual(lalala.Username, firstAdded.Username);
                Assert.AreNotEqual(lalala.Email, firstAdded.Email);
                Assert.AreNotEqual(lalala.UserRole, RoleConstants.ADMIN);
            }
        }
 public UserServiceTests(ITestOutputHelper output)
 {
     _fakeDatabase = new FakeDatabase();
     _userService  = new UserService(l => output.WriteLine(l), _fakeDatabase);
 }