public async Task AddAsyncThrowsWhenTheUserIdIsNull() { // Arrange var mapperMock = new Mock <IMapper>(); var ascentsRepoMock = new Mock <IDeletableEntityRepository <Ascent> >(); var pointsRepoMock = new Mock <IDeletableEntityRepository <Points> >(); var ascentsService = new AscentsService(ascentsRepoMock.Object, pointsRepoMock.Object, mapperMock.Object); // Assert await Assert.ThrowsAsync <ArgumentNullException>(async() => { // Act await ascentsService.AddAsync(new AscentInputModel(), null); }); }
public async Task AddAsyncAddsTheEntityAndSetsItsPoints( string userId, string boulderId, string styleId, string gradeId, int stars, string comment, bool recommend, int daysToSubtract, int weeklyPoints, int monthlyPoints, int yearlyPoints, int allTimePoints) { var timeSpan = TimeSpan.FromDays(daysToSubtract); var ascentDate = DateTime.UtcNow.Subtract(timeSpan); // Arrange AutoMapperConfig.RegisterMappings(typeof(ErrorViewModel).Assembly); var ascentsRepoMock = new Mock <IDeletableEntityRepository <Ascent> >(); var pointsRepoMock = new Mock <IDeletableEntityRepository <Points> >(); var saved = false; // To test if the ascent is actually added var realAscentsList = new List <Ascent>(); // To test if points are calculated correctly var fakeAscentsList = new List <Ascent>() { new Ascent() { ApplicationUserId = userId, Grade = new Grade() { Points = 100, }, Style = new Style() { BonusPoints = 50, }, Date = ascentDate, }, }; var points = new Points() { ApplicationUserId = userId, }; var pointsList = new List <Points>() { points, }; var ascentInput = new AscentInputModel() { BoulderId = boulderId, Stars = stars, StyleId = styleId, Comment = comment, GradeId = gradeId, Recommend = recommend, Date = ascentDate, }; ascentsRepoMock.Setup(x => x.All()) .Returns(realAscentsList.AsQueryable()); ascentsRepoMock.Setup(x => x.AllAsNoTracking()) .Returns(fakeAscentsList.AsQueryable()); ascentsRepoMock.Setup(x => x.SaveChangesAsync()) .Callback(() => { saved = true; }); ascentsRepoMock.Setup(x => x.AddAsync(It.IsAny <Ascent>())) .Callback((Ascent ascent) => { realAscentsList.Add(ascent); }); pointsRepoMock.Setup(x => x.All()) .Returns(pointsList.AsQueryable()); var ascentsService = new AscentsService(ascentsRepoMock.Object, pointsRepoMock.Object, AutoMapperConfig.MapperInstance); // Act await ascentsService.AddAsync(ascentInput, userId); // Assert var ascent = realAscentsList[0]; Assert.True(saved); Assert.Equal(boulderId, ascent.BoulderId); Assert.Equal(stars, ascent.Stars); Assert.Equal(styleId, ascent.StyleId); Assert.Equal(comment, ascent.Comment); Assert.Equal(gradeId, ascent.GradeId); Assert.Equal(recommend, ascent.Recommend); Assert.Equal(ascentDate, ascent.Date); Assert.Equal(weeklyPoints, points.Weekly); Assert.Equal(monthlyPoints, points.Monthly); Assert.Equal(yearlyPoints, points.Yearly); Assert.Equal(allTimePoints, points.AllTime); }