public void Add_WhenNewShift_ThenShiftShouldBeAdded() { // Arrange var mockedPublisher = new Mock <IMessagePublisher>(); var mockedRepository = new Mock <IRepository>(); mockedRepository.Setup(m => m.AddShift(It.IsAny <AddShift>())).Returns(Task.FromResult(1)); mockedRepository.Setup(m => m.GetWorker(It.IsAny <int>())) .Returns(Task.FromResult(new Worker() { Id = 1, FirstName = "Jan", LastName = "Hello" })); mockedRepository.Setup(m => m.GetShiftsPerWorker(It.IsAny <int>())) .Returns(Task.FromResult((IList <Shift>) new List <Shift>())); var controller = new ShiftController(mockedRepository.Object, mockedPublisher.Object); var newShift = new AddShift() { Day = new DateTime(2020, 1, 1), Type = ShiftType.ShiftFrom0To8, WorkerId = 1 }; // Act var result = controller.AddAsync(newShift); var okResult = result.Result as OkObjectResult; // Assert Assert.IsNotNull(okResult); Assert.AreEqual(200, okResult.StatusCode); Assert.AreEqual(1, okResult.Value); mockedPublisher.Verify(m => m.SendMessageShiftCreated(It.IsAny <Shift>()), Times.Once); }
public void Add_WhenNewShiftWithSameOrCloseTimes_ThenShiftShouldNotBeAdded(int year, int month, int day, ShiftType type, int workerId) { // Arrange var mockedPublisher = new Mock <IMessagePublisher>(); var mockedRepository = new Mock <IRepository>(); var shift = new Shift() { Day = new DateTime(year, month, day), Type = type, WorkerId = workerId }; IList <Shift> list = new List <Shift>(); list.Add(shift); mockedRepository.Setup(m => m.AddShift(It.IsAny <AddShift>())).Returns(Task.FromResult(1)); mockedRepository.Setup(m => m.GetWorker(It.IsAny <int>())) .Returns(Task.FromResult(new Worker() { Id = 1, FirstName = "Jan", LastName = "Hello" })); mockedRepository.Setup(m => m.GetShiftsPerWorker(It.IsAny <int>())).Returns(Task.FromResult(list)); var controller = new ShiftController(mockedRepository.Object, mockedPublisher.Object); var newShift = new AddShift() { Day = new DateTime(2020, 2, 2), Type = ShiftType.ShiftFrom0To8, WorkerId = 1 }; // Act var result = controller.AddAsync(newShift); // Assert var exception = (AggregateException)result.Exception; Assert.IsNotNull(exception); }