Ejemplo n.º 1
0
        public async Task CreatePost_SavesData_AndReturnsRedirectToAction_WhenModelIsValid()
        {
            //Arrange
            var options = TestFactory.CreateDbContextOptions("EmpControllerCreateDb");

            using var context = new AppDbContext(options);

            //add department
            var department = new Department {
                DepartmentName = "Emp"
            };
            await context.AddAsync(department);

            await context.SaveChangesAsync();

            var newEmployee = new Employee
            {
                FirstName     = "First",
                LastName      = "Last",
                ContactNumber = "2222333",
                DepartmentId  = department.DepartmentId
            };
            var controller = new EmployeeController(context);

            //Act
            var result = await controller.Create(newEmployee);

            //Assert
            Assert.IsType <RedirectToActionResult>(result);
            Assert.NotEmpty(await context.Employees.ToListAsync());
        }
Ejemplo n.º 2
0
        public async Task DeleteConfirmed_DeletesData_AndReturnsRedirectToAction_WhenIdIsValid()
        {
            //Arrange
            var options = TestFactory.CreateDbContextOptions("EmpControllerDeleteValidDb");

            using var context = new AppDbContext(options);
            var controller = new DepartmentController(context);
            //add department
            var department = new Department {
                DepartmentName = "Department"
            };
            await context.AddAsync(department);

            await context.SaveChangesAsync();

            //add employee
            await context.AddAsync(new Employee
            {
                FirstName     = "First",
                LastName      = "Last",
                ContactNumber = "2222333",
                DepartmentId  = department.DepartmentId
            });

            //Act
            var result = await controller.DeleteConfirmed(department.DepartmentId);

            //Assert
            Assert.IsType <RedirectToActionResult>(result);

            Assert.Empty(await context.Employees.ToListAsync());
        }
Ejemplo n.º 3
0
        public async Task Details_ReturnsViewAndResult_WhenIndexExist()
        {
            //Arrange
            var options = TestFactory.CreateDbContextOptions("DepControllerDetailsIndexExistDb");

            using var context = new AppDbContext(options);

            var department = new Department {
                DepartmentName = "Test"
            };
            await context.AddAsync(department);

            await context.SaveChangesAsync();

            var controller = new DepartmentController(context);

            //Act
            var result = await controller.Details(department.DepartmentId);

            //Assert
            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsAssignableFrom <Department>(viewResult.ViewData.Model);

            Assert.Equal(model.DepartmentId, department.DepartmentId);
        }
Ejemplo n.º 4
0
        public async Task Edit_UpdatesData_ReturnsRedirectActionResult_WhenModelIsValid()
        {
            //Arrange
            var options = TestFactory.CreateDbContextOptions("DepControllerDetailsCreateValidDb");

            using var context = new AppDbContext(options);
            var controller = new DepartmentController(context);
            //init department
            var existingDepartment = new Department {
                DepartmentName = "Department"
            };
            await context.AddAsync(existingDepartment);

            await context.SaveChangesAsync();

            //update name
            existingDepartment.DepartmentName = "New Department";

            //Act
            var result = await controller.Edit(existingDepartment.DepartmentId, existingDepartment);

            //Assert
            Assert.IsType <RedirectToActionResult>(result);

            var updatedDepartment = await context
                                    .Departments
                                    .Where(t => t.DepartmentId == existingDepartment.DepartmentId)
                                    .FirstOrDefaultAsync();

            Assert.Equal(existingDepartment.DepartmentName, updatedDepartment.DepartmentName);
        }
Ejemplo n.º 5
0
        public void Add_Valid_EmployeeModel_Returns_Success()
        {
            //Arrange
            var options = TestFactory.CreateDbContextOptions("EmployeeAddDb");

            using var context = new AppDbContext(options);

            var validator = new EmployeeValidator(context);


            //add a department
            var department = new Department {
                DepartmentName = "Tech"
            };

            context.Departments.Add(department);
            context.SaveChanges();

            //Act
            var employee = new Employee
            {
                FirstName     = "John",
                LastName      = "Doe",
                ContactNumber = "1234567",
                DepartmentId  = department.DepartmentId
            };
            var result = validator.Validate(employee);

            //Assert
            Assert.Empty(result.Errors);
        }
Ejemplo n.º 6
0
        public async Task Details_ReturnsNotFound_WhenIndexDoesNotExist()
        {
            //Arrange
            var options = TestFactory.CreateDbContextOptions("DepControllerDetailsIndexNotExistDb");

            using var context = new AppDbContext(options);

            var controller = new DepartmentController(context);

            //Act
            var result = await controller.Details(0);

            //Assert
            Assert.IsType <NotFoundResult>(result);
        }
Ejemplo n.º 7
0
        public void Empty_DepartmentName_Returns_Error()
        {
            //Arrange
            var options = TestFactory.CreateDbContextOptions("DepartmentEmptyDb");

            using var context = new AppDbContext(options);

            var validator = new DepartmentValidator(context);

            //Act
            var result = validator.Validate(new Department());

            //Assert
            Assert.Contains(result.Errors, e => e.PropertyName == "DepartmentName");
        }
Ejemplo n.º 8
0
        public void Empty_EmployeeModel_Returns_Error()
        {
            //Arrange
            var options = TestFactory.CreateDbContextOptions("EmployeeEmptyDb");

            using var context = new AppDbContext(options);

            var validator = new EmployeeValidator(context);
            //Act
            var result = validator.Validate(new Employee());

            //Assert
            Assert.Contains(result.Errors, e => e.PropertyName == "FirstName");
            Assert.Contains(result.Errors, e => e.PropertyName == "LastName");
            Assert.Contains(result.Errors, e => e.PropertyName == "ContactNumber");
        }
Ejemplo n.º 9
0
        public void NotEmpty_DepartmentName_Returns_Success()
        {
            //Arrange
            var options = TestFactory.CreateDbContextOptions("DepartmentNotEmptyDb");

            using var context = new AppDbContext(options);

            var validator = new DepartmentValidator(context);

            //Act
            var result = validator.Validate(new Department {
                DepartmentName = "IT"
            });

            //Assert
            Assert.Empty(result.Errors);
        }
Ejemplo n.º 10
0
        public async Task CreatePost_SavesData_ReturnsRedirectActionResult_WhenModelIsValid()
        {
            //Arrange
            var options = TestFactory.CreateDbContextOptions("DepControllerDetailsCreateValidDb");

            using var context = new AppDbContext(options);
            var controller = new DepartmentController(context);

            //Act
            var result = await controller.Create(new Department { DepartmentName = "New Department" });

            //Assert
            Assert.IsType <RedirectToActionResult>(result);

            var departments = await context.Departments.ToListAsync();

            Assert.NotEmpty(departments);
        }
Ejemplo n.º 11
0
        public async Task Index_ReturnsViewAndListofEmployees()
        {
            //Arrange
            var options = TestFactory.CreateDbContextOptions("EmpControllerIndexDb");

            using var context = new AppDbContext(options);

            //add department
            var department = new Department {
                DepartmentName = "Emp"
            };
            await context.AddAsync(department);

            await context.SaveChangesAsync();

            //add some data
            await context.Employees.AddRangeAsync(new Employee
            {
                FirstName     = "First",
                LastName      = "Last",
                ContactNumber = "2222333",
                DepartmentId  = department.DepartmentId
            }, new Employee
            {
                FirstName     = "First2",
                LastName      = "Last2",
                ContactNumber = "22223332",
                DepartmentId  = department.DepartmentId
            }
                                                  );

            await context.SaveChangesAsync();

            var controller = new EmployeeController(context);

            //Act
            var result = await controller.Index();

            //Assert
            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsAssignableFrom <List <Employee> >(viewResult.ViewData.Model);

            Assert.Equal(2, model.Count);
        }
Ejemplo n.º 12
0
        public async Task DeleteConfirmed_DeletesData_AndReturnsRedirectToAction_WhenIdIsValid()
        {
            //Arrange
            var options = TestFactory.CreateDbContextOptions("DepControllerDeleteConfirmedValidDb");

            using var context = new AppDbContext(options);
            var controller = new DepartmentController(context);
            //init department
            var existingDepartment = new Department {
                DepartmentName = "Department"
            };
            await context.AddAsync(existingDepartment);

            await context.SaveChangesAsync();

            //Act
            var result = await controller.DeleteConfirmed(existingDepartment.DepartmentId);

            //Assert
            Assert.IsType <RedirectToActionResult>(result);

            Assert.Empty(await context.Departments.ToListAsync());
        }
Ejemplo n.º 13
0
        public void Duplicate_Name_Returns_Error()
        {
            //Arrange
            var options = TestFactory.CreateDbContextOptions("DepartmentDuplicateDb");

            using var context = new AppDbContext(options);

            //add a department
            context.Departments.Add(new Department {
                DepartmentName = "HR"
            });
            context.SaveChanges();

            var validator = new DepartmentValidator(context);

            //Act
            var result = validator.Validate(new Department {
                DepartmentName = "HR"
            });

            //Assert
            Assert.NotEmpty(result.Errors);
        }
Ejemplo n.º 14
0
        public async Task Index_Returns_ViewAndListOfDeparment()
        {
            //Arrange
            var options = TestFactory.CreateDbContextOptions("DepControllerIndexDb");

            using var context = new AppDbContext(options);

            //add some data
            await context.Departments.AddRangeAsync(new Department { DepartmentName = "IT" },
                                                    new Department { DepartmentName = "IT" });

            await context.SaveChangesAsync();

            var controller = new DepartmentController(context);

            //Act
            var result = await controller.Index();

            //Assert
            var viewResult = Assert.IsType <ViewResult>(result);
            var model      = Assert.IsAssignableFrom <List <Department> >(viewResult.ViewData.Model);

            Assert.Equal(2, model.Count);
        }
Ejemplo n.º 15
0
        public void Duplicate_Employee_Returns_Error()
        {
            //Arrange
            var options = TestFactory.CreateDbContextOptions("EmployeeDuplicateDb");

            using var context = new AppDbContext(options);

            var validator = new EmployeeValidator(context);

            //add a department
            var department = new Department {
                DepartmentName = "Tech"
            };

            //add employee
            context.Employees.Add(new Employee
            {
                FirstName     = "John",
                LastName      = "Doe",
                ContactNumber = "1234567",
                Department    = department
            });
            context.SaveChanges();

            //Act
            var result = validator.Validate(new Employee
            {
                FirstName     = "John",
                LastName      = "Doe",
                ContactNumber = "1234567",
                Department    = department
            });

            //Assert
            Assert.NotEmpty(result.Errors);
        }