public void UpdateEmployee_ShouldCallRepositoryUpdate() { // Fixture set-up var repoEmployeeSpy = new EmployeeRepositorySpy(new Employee() { Id = 1, Firstname = "Joe", Lastname = "Bloggs", Phone = "22222222", Email = "*****@*****.**", CompanyId = 2 }); var sut = new EmployeeService(repoEmployeeSpy, null); // Expected result var expected = EmployeeServiceTestUtility.CreateValidEmployee(); // Exercise the SUT (system under test), otherwise known as the "Action" phase var actual = sut.updateEmployee( employeeId: expected.Id, companyId: expected.CompanyId, firstName: expected.Firstname, lastName: expected.Lastname, phone: expected.Phone, email: expected.Email); // Behaviour verification (verifying the SUT interacted correctly with its // collaborators/dependencies. In testing we call dependencies depended on components Assert.AreEqual(repoEmployeeSpy.NumTimesGetCalled, 1); Assert.AreEqual(repoEmployeeSpy.NumTimesUpdateCalled, 1); }
public void UpdateEmployee_ShouldReturnNewEmployee() { // Fixture set-up var repoEmployeeStub = new EmployeeRepositoryStub(new Employee() { Id = 1, Firstname = "Joe", Lastname = "Bloggs", Phone = "22222222", Email = "*****@*****.**", CompanyId = 2 }); var sut = new EmployeeService(repoEmployeeStub, null); // Expected result var expected = EmployeeServiceTestUtility.CreateValidEmployee(); // Exercise the SUT (system under test) var actual = sut.updateEmployee( employeeId: expected.Id, companyId: expected.CompanyId, firstName: expected.Firstname, lastName: expected.Lastname, phone: expected.Phone, email: expected.Email); // Assertion or verfication phase AreEmployeeEqual(expected, actual); }
public void UpdateEmployee_ShouldCallRepositoryUpdateMock() { // Fixture set-up var currentEmployee = new Employee() { Id = 1, Firstname = "Joe", Lastname = "Bloggs", Phone = "22222222", Email = "*****@*****.**", CompanyId = 2 }; var repoEmployeeMock = new Mock <IEmployeeRepository>(); // Using the mock to act as a test stub repoEmployeeMock.Setup(r => r.Get(1)) .Returns(currentEmployee); var sut = new EmployeeService(repoEmployeeMock.Object, null); // Expected result var expected = EmployeeServiceTestUtility.CreateValidEmployee(); // Exercise the SUT (system under test), otherwise known as the "Action" phase var actual = sut.updateEmployee( employeeId: expected.Id, companyId: expected.CompanyId, firstName: expected.Firstname, lastName: expected.Lastname, phone: expected.Phone, email: expected.Email); // Behaviour verification (verifying the SUT interacted correctly with its // collaborators/dependencies. In testing we call dependencies depended on components // With Mocks, instead of an Assert we call the verify method on the Mock object // Verify that the Get was called with an int matching the current employee, and // that it was called exactly once repoEmployeeMock.Verify(r => r.Get(expected.Id), Times.Once); // Verify that the Update was called with an employee matching the object reference // of the currentEmployee object, and that it was called exactly once repoEmployeeMock.Verify(r => r.Update(currentEmployee), Times.Once); }