daily pastebin goal
86%
SHARE
TWEET

Untitled

a guest Jan 11th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private readonly IEmployeeService _employeeService;
  2.  
  3.     public EmployeeServiceTests()
  4.     {
  5.         var mockData = new List<Employee>
  6.         {
  7.             new Employee { Id = 0, FirstName = "Homer", LastName = "Simpson" },
  8.             new Employee { Id = 1, FirstName = "Carl", LastName = "Carlson" },
  9.             new Employee { Id = 2, FirstName = "Lenny", LastName = "Leonard" },
  10.         };
  11.  
  12.         _employeeService = MockSetup.SetupEmployeeService(mockData);
  13.     }
  14.  
  15.     [Fact]
  16.     public void UpdateEmployee_EmployeeExists_EmployeeGetsUpdated()
  17.     {
  18.         var homer = _employeeService.GetEmployee(0);
  19.         homer.FirstName = "Homer Jay";
  20.  
  21.         _employeeService.UpdateEmployee(homer);
  22.  
  23.         var actual = homer.FirstName;
  24.         var expected = _employeeService.GetEmployee(0).FirstName;
  25.  
  26.         Assert.Equal(expected, actual);
  27.     }
  28.    
  29. public static Mock<MockDbSet<Employee>> CreateEmployeeMockSet(List<Employee> data)
  30.     {
  31.         var mockSet = new Mock<MockDbSet<Employee>>();
  32.         mockSet.As<IQueryable<Employee>>().Setup(m => m.Provider).Returns(data.AsQueryable().Provider);
  33.         mockSet.As<IQueryable<Employee>>().Setup(m => m.Expression).Returns(data.AsQueryable().Expression);
  34.         mockSet.As<IQueryable<Employee>>().Setup(m => m.ElementType).Returns(data.AsQueryable().ElementType);
  35.         mockSet.As<IQueryable<Employee>>().Setup(m => m.GetEnumerator()).Returns(data.AsQueryable().GetEnumerator());
  36.         mockSet.Setup(d => d.Add(It.IsAny<Employee>())).Callback<Employee>(e => data.Add(e));
  37.         mockSet.Setup(d => d.AddOrUpdate(It.IsAny<Employee>())).Callback<Employee>(e => UpdateList(e, data));
  38.         mockSet.Setup(d => d.Remove(It.IsAny<Employee>())).Callback<Employee>(e => data.Remove(e));
  39.  
  40.         return mockSet;
  41.     }
  42.  
  43.     private static void UpdateList(Employee employee, List<Employee> data)
  44.     {
  45.         var index = data.FindIndex(e => e.Id == employee.Id);
  46.  
  47.         data[index] = employee;
  48.     }
  49.    
  50. Message: Assert.Equal() Failure
  51.                ↓ (pos 5)
  52. Expected: Homer
  53. Actual:   Homer Jay
  54.                ↑ (pos 5)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top