public async Task TestGetByIdMethod() { MapperInitializer.InitializeMapper(); var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var articleRepository = new EfDeletableEntityRepository <Article>(context); var articlesService = new ArticlesService(articleRepository); var articlesId = new List <int>(); var inputModel = new CreateArticleViewModel { ImgUrl = $"TT{2}{2 * 2}asd", Content = $"Ten{2}{2 * 2}", Title = $"Article{2}", }; var id = await articlesService.CreateAsync(inputModel, "2"); var articleId = articleRepository.All().First(x => x.Id == id); var article = articlesService.GetById <IndexArticleViewModel>(id); Assert.True(article != null); Assert.True(article.Id == id); }
public async Task TestGetArticlesByPageMethod() { var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var articleRepository = new EfDeletableEntityRepository <Article>(context); var articlesService = new ArticlesService(articleRepository); for (int i = 0; i < 10; i++) { var inputModel = new CreateArticleViewModel { ImgUrl = $"TT{i}{i * 2}asd", Content = $"Ten{i}{i * 2}", Title = $"Article{i}", }; await articlesService.CreateAsync(inputModel, i.ToString()); } var result1 = articlesService.GetArticlesByPage <IndexArticleViewModel>(5, 5); var result2 = articlesService.GetArticlesByPage <IndexArticleViewModel>(5, 0); Assert.Equal(5, result2.Count()); Assert.Equal(5, result1.Count()); }
private async Task <string> GetArticleAsync(ArticlesService service) { var picture = this.mockPicture.Object; var articleId = await service.CreateAsync("title", "content test article", this.category.Id, picture, this.stylist.Id); return(articleId); }
public async Task GetByIdShouldReturnTheCorrectArticle() { this.Setup(); var repo = new Mock <IDeletableEntityRepository <BlogArticle> >(); repo.Setup(x => x.AllAsNoTracking()).Returns(this.articleList.AsQueryable()); repo.Setup(x => x.AddAsync(It.IsAny <BlogArticle>())).Callback( (BlogArticle article) => this.articleList.Add(article)); var categoryRepo = new Mock <IDeletableEntityRepository <BlogCategory> >(); var articlesService = new ArticlesService(repo.Object, categoryRepo.Object); await articlesService.CreateAsync(this.testArticle, "testUser"); await articlesService.CreateAsync(this.testArticle, "testUser"); var returnedArticle = articlesService.GetById <ArticleViewModel>(1); Assert.Equal("test", returnedArticle.Title); }
public async Task ArticlesShouldBeAddedSuccessfully() { this.Setup(); var repo = new Mock <IDeletableEntityRepository <BlogArticle> >(); repo.Setup(x => x.AddAsync(It.IsAny <BlogArticle>())).Callback( (BlogArticle article) => this.articleList.Add(article)); var categoryRepo = new Mock <IDeletableEntityRepository <BlogCategory> >(); var articlesService = new ArticlesService(repo.Object, categoryRepo.Object); await articlesService.CreateAsync(this.testArticle, "testUser"); Assert.Single(this.articleList); Assert.Equal(this.testArticle.Title, this.articleList.First().Title); await articlesService.CreateAsync(this.testArticle, "testUser"); Assert.Equal(2, this.articleList.Count); }
public async Task GetCountShouldReturnTheCorrectCount() { this.Setup(); var repo = new Mock <IDeletableEntityRepository <BlogArticle> >(); repo.Setup(x => x.All()).Returns(this.articleList.AsQueryable()); repo.Setup(x => x.AddAsync(It.IsAny <BlogArticle>())).Callback( (BlogArticle article) => this.articleList.Add(article)); var categoryRepo = new Mock <IDeletableEntityRepository <BlogCategory> >(); var articlesService = new ArticlesService(repo.Object, categoryRepo.Object); await articlesService.CreateAsync(this.testArticle, "testUser"); var count = articlesService.GetCount(); Assert.Equal(1, count); await articlesService.CreateAsync(this.testArticle, "testUser"); count = articlesService.GetCount(); Assert.Equal(2, count); }
public async Task CheckIfCreateAsyncReturnsFalseIfCannotCreateArticle() { var options = new DbContextOptionsBuilder <ElectricTravelDbContext>() .UseInMemoryDatabase(databaseName: "ArticlesTestDb").Options; using var dbContext = new ElectricTravelDbContext(options); using var repo = new EfDeletableEntityRepository <Article>(dbContext); var service = new ArticlesService(repo); var article = new ArticleInputViewModel(); article = null; var isCreated = await service.CreateAsync(article); Assert.True(!isCreated); }
public async Task TestCreateASyncMethodReturnId() { var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var articleRepository = new EfDeletableEntityRepository <Article>(context); var articlesService = new ArticlesService(articleRepository); var articlesId = new List <int>(); for (int i = 0; i < 10; i++) { var inputModel = new CreateArticleViewModel { ImgUrl = $"TT{i}{i * 2}asd", Content = $"Ten{i}{i * 2}", Title = $"Article{i}", }; articlesId.Add(await articlesService.CreateAsync(inputModel, i.ToString())); } Assert.True(articlesId[1] is int); }
public async Task CheckIfCreateAsyncCreatesArticle() { var options = new DbContextOptionsBuilder <ElectricTravelDbContext>() .UseInMemoryDatabase(databaseName: "ArticlesTestDb").Options; using var dbContext = new ElectricTravelDbContext(options); using var repo = new EfDeletableEntityRepository <Article>(dbContext); var service = new ArticlesService(repo); var article = new ArticleInputViewModel() { ShortDescription = "DDDDDSASDSASD", Content = "DDDDDSASDSASD", UserId = "goshoasd", Title = "DDDDDASDASDS", }; var isCreated = await service.CreateAsync(article); Assert.True(isCreated); }
public async Task TestCreateASyncMethodWithWrongData() { // Arrange var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var articleRepository = new EfDeletableEntityRepository <Article>(context); var articlesService = new ArticlesService(articleRepository); var inputModel = new CreateArticleViewModel { ImgUrl = string.Empty, Content = string.Empty, Title = string.Empty, }; // Act // Assert await Assert.ThrowsAsync <ArgumentNullException>(async() => { await articlesService.CreateAsync(inputModel, "1"); }); }