public void Constructor_ShouldThrowExceptionWhenNullArgument2() { // ARRANGE var dbContext = new Mock <IAppDbContextFactory>(); // ASSERT Assert.Throws <ArgumentNullException>(() => { var _sut = new ControlScheduleService(dbContext.Object, null); }); }
public async Task GetByIdAsync_ShouldReturnErrorWhenExceptionInQuery() { // ARRANGE var mockQuery = new Mock <IControlScheduleQuery>(); mockQuery .Setup(tq => tq.GetById(It.IsAny <int>())) .ThrowsAsync(new Exception("Mock exception")); var mockContextFactory = SetupContextFactory(mockQuery.Object); _sut = new ControlScheduleService(mockContextFactory.Object, _mapper); // ACT var result = await _sut.GetByIdAsync(10); // ASSERT Assert.Equal("Mock exception", result.Error.Message); }
public async Task GetByIdAsync_ShouldReturnErrorWhenNotFound() { // ARRANGE var mockQuery = new Mock <IControlScheduleQuery>(); mockQuery .Setup(tq => tq.GetById(It.IsAny <int>())) .ReturnsAsync((int id) => GetTestControlSchedules().FirstOrDefault(c => c.Id == id)); var mockContextFactory = SetupContextFactory(mockQuery.Object); _sut = new ControlScheduleService(mockContextFactory.Object, _mapper); // ACT var result = await _sut.GetByIdAsync(10); // ASSERT Assert.Equal("Запись не найдена", result.Error.Message); }
public async Task Update_ShouldReturnValue() { // ARRANGE var mockQuery = new Mock <IControlScheduleQuery>(); mockQuery .Setup(tq => tq.Update( It.IsAny <int>(), It.IsAny <short>(), It.IsAny <DateTime>(), It.IsAny <DateTime?>(), It.IsAny <DateTime?>(), It.IsAny <DateTime?>(), It.IsAny <DateTime?>(), It.IsAny <DateTime?>(), It.IsAny <DateTime?>(), It.IsAny <DateTime?>())) .ReturnsAsync((int a, short c, DateTime d, DateTime? e, DateTime? f, DateTime? g, DateTime? h, DateTime? i, DateTime? j, DateTime? k) => new ControlSchedule { Id = a, YearId = 1, SeasonId = 1, UserId = c, DateStart = d, DateEnd = e, Mod1DateStart = f, Mod1DateEnd = g, Mod2DateStart = h, Mod2DateEnd = i, ItogDateStart = j, ItogDateEnd = k } ); var mockContextFactory = SetupContextFactory(mockQuery.Object); _sut = new ControlScheduleService(mockContextFactory.Object, _mapper); var testModel = GetTestControlSchedules().First(); var dto = _mapper.Map <ControlScheduleDto>(testModel); dto.Year = GetTestYears().First(); dto.Season = GetTestSeasons().First(); // ACT var result = await _sut.UpdateAsync(dto); // ASSERT Assert.Equal(ComparableObject.Convert(dto), ComparableObject.Convert(result.Entity)); }
public async Task GetByYearAndSeasonAsync_ShouldReturnErrorWhenExceptionInQuery() { // ARRANGE var mockQuery = new Mock <IControlScheduleQuery>(); mockQuery .Setup(tq => tq.GetByYearAndSeason( It.IsAny <byte>(), It.IsAny <byte>())) .ThrowsAsync(new Exception("Mock exception")); var mockContextFactory = SetupContextFactory(mockQuery.Object); _sut = new ControlScheduleService(mockContextFactory.Object, _mapper); byte yearId = 1, seasonId = 1; var expectedDtoList = _mapper.Map <List <ControlScheduleDto> >(GetTestControlSchedules().GetRange(0, 1)); // ACT var result = await _sut.GetByYearAndSeasonAsync(new GetByYearAndSeasonRequest(yearId, seasonId)); // ASSERT Assert.Equal("Mock exception", result.Error.Message); }
public async Task GetByIdAsync_ShouldReturnValue() { // ARRANGE var mockQuery = new Mock <IControlScheduleQuery>(); mockQuery .Setup(tq => tq.GetById(It.IsAny <int>())) .ReturnsAsync((int id) => GetTestControlSchedules().First(c => c.Id == id)); var mockContextFactory = SetupContextFactory(mockQuery.Object); _sut = new ControlScheduleService(mockContextFactory.Object, _mapper); var expectedDto = _mapper.Map <ControlScheduleDto>(GetTestControlSchedules().First()); expectedDto.Year = GetTestYears().First(); expectedDto.Season = GetTestSeasons().First(); // ACT var result = await _sut.GetByIdAsync(1); // ASSERT Assert.Equal(ComparableObject.Convert(expectedDto), ComparableObject.Convert(result.Entity)); }
public async Task GetByYearAndSeasonAsync_ShouldReturnList() { // ARRANGE var mockQuery = new Mock <IControlScheduleQuery>(); mockQuery .Setup(tq => tq.GetByYearAndSeason( It.IsAny <byte>(), It.IsAny <byte>())) .ReturnsAsync((byte year, byte season) => GetTestControlSchedules().Where(c => c.YearId == year && c.SeasonId == season).ToList()); var mockContextFactory = SetupContextFactory(mockQuery.Object); _sut = new ControlScheduleService(mockContextFactory.Object, _mapper); byte yearId = 1, seasonId = 1; var expectedDtoList = _mapper.Map <List <ControlScheduleListDto> >(GetTestControlSchedules().GetRange(0, 1)); // ACT var result = await _sut.GetByYearAndSeasonAsync(new GetByYearAndSeasonRequest(yearId, seasonId)); // ASSERT Assert.Equal(expectedDtoList.Count, result.Entity.Count); Assert.Equal(ComparableObject.Convert(expectedDtoList.First()), ComparableObject.Convert(result.Entity.First())); }
public async Task Update_ShouldReturnErrorWhenExceptionInQuery() { // ARRANGE var mockQuery = new Mock <IControlScheduleQuery>(); mockQuery .Setup(tq => tq.Update( It.IsAny <int>(), It.IsAny <short>(), It.IsAny <DateTime>(), It.IsAny <DateTime?>(), It.IsAny <DateTime?>(), It.IsAny <DateTime?>(), It.IsAny <DateTime?>(), It.IsAny <DateTime?>(), It.IsAny <DateTime?>(), It.IsAny <DateTime?>())) .ThrowsAsync(new Exception("Mock exception")); var mockContextFactory = SetupContextFactory(mockQuery.Object); _sut = new ControlScheduleService(mockContextFactory.Object, _mapper); var testModel = GetTestControlSchedules().First(); var dto = _mapper.Map <ControlScheduleDto>(testModel); // ACT var result = await _sut.UpdateAsync(dto); // ASSERT Assert.Equal("Mock exception", result.Error.Message); }
public async Task Create_ShouldReturnErrorWhenNull() { // ARRANGE var mockQuery = new Mock <IControlScheduleQuery>(); mockQuery .Setup(tq => tq.Create( It.IsAny <byte>(), It.IsAny <byte>(), It.IsAny <short>(), It.IsAny <DateTime>(), It.IsAny <DateTime?>(), It.IsAny <DateTime?>(), It.IsAny <DateTime?>(), It.IsAny <DateTime?>(), It.IsAny <DateTime?>(), It.IsAny <DateTime?>(), It.IsAny <DateTime?>())) .ReturnsAsync(() => null); var mockContextFactory = SetupContextFactory(mockQuery.Object); _sut = new ControlScheduleService(mockContextFactory.Object, _mapper); var testModel = GetTestControlSchedules().First(); var dto = _mapper.Map <ControlScheduleDto>(testModel); // ACT var result = await _sut.CreateAsync(dto); // ASSERT Assert.Equal("Ошибка при добавлении записи", result.Error.Message); }
public void Constructor_ShouldThrowExceptionWhenNullArgument1() { // ASSERT Assert.Throws <ArgumentNullException>(() => { var _sut = new ControlScheduleService(null, _mapper); }); }