// verileri kayit ederken db ayarlarinin ayni dbname icerisinde olduguna dikkat edin, eger db nameler farkliysa test verileri farkli veri tabanlarina kayit olur. tum test metotlarinda(Fact'lerde) ayri dbName'ler verilmesinin sebebi budur. public async Task <List <ApplicationResult <PostDto> > > CreatePost(List <CreatePostInput> fakePostList, string postDbName) { var options = new DbContextOptionsBuilder <ApplicationUserDbContext>().UseInMemoryDatabase(databaseName: postDbName).Options; MapperConfiguration mappingConfig = new MapperConfiguration(mc => { mc.AddProfile(new AutoMapperProfile()); }); IMapper mapper = mappingConfig.CreateMapper(); ApplicationResult <CategoryDto> result = new ApplicationResult <CategoryDto>(); using (var inMemoryContext = new ApplicationUserDbContext(options)) { CategoryServiceShould categoryShould = new CategoryServiceShould(); result = await categoryShould.CreateCategory(inMemoryContext, mapper); await categoryShould.AssertCreatedCategory(inMemoryContext, result); } List <ApplicationResult <PostDto> > createdPostResulList = new List <ApplicationResult <PostDto> >(); using (var inMemoryContext = new ApplicationUserDbContext(options)) { var service = new PostService(inMemoryContext, mapper); foreach (var item in fakePostList) { item.CategoryId = result.Result.Id; createdPostResulList.Add(await service.Create(item)); } } return(createdPostResulList); }
public async Task CreatePost() { var options = new DbContextOptionsBuilder <ApplicationUserDbContext>().UseInMemoryDatabase(databaseName: "Test_PostCreate").Options; MapperConfiguration mappingConfig = new MapperConfiguration(mc => { mc.AddProfile(new AutoMapperProfile()); }); IMapper mapper = mappingConfig.CreateMapper(); ApplicationResult <CategoryDto> resultCreateCategory = new ApplicationResult <CategoryDto>(); // create category using (var inMemoryContext = new ApplicationUserDbContext(options)) { CategoryServiceShould categoryShould = new CategoryServiceShould(); resultCreateCategory = await categoryShould.CreateCategory(inMemoryContext, mapper); } // check create category ApplicationResult <PostDto> resultPost = new ApplicationResult <PostDto>(); using (var inMemoryContext = new ApplicationUserDbContext(options)) { CategoryServiceShould categoryShould = new CategoryServiceShould(); await categoryShould.AssertCreatedCategory(inMemoryContext, resultCreateCategory); // create post var service = new PostService(inMemoryContext, mapper); CreatePostInput fakePost = new CreatePostInput { CategoryId = resultCreateCategory.Result.Id, Content = "Lorem Ipsum Dolor Sit Amet", Title = "Lorem Ipsum Dolor", UrlName = "lorem-ipsum-dolor", CreatedBy = "Tester1", CreatedById = Guid.NewGuid().ToString() }; resultPost = await service.Create(fakePost); } // check post create service using (var inMemoryContext = new ApplicationUserDbContext(options)) { Assert.True(resultPost.Succeeded); Assert.NotNull(resultPost.Result); Assert.Equal(1, await inMemoryContext.Posts.CountAsync()); var item = await inMemoryContext.Posts.FirstAsync(); Assert.Equal("Tester1", item.CreatedBy); Assert.Equal("Lorem Ipsum Dolor", item.Title); Assert.Equal("lorem-ipsum-dolor", item.UrlName); Assert.Equal("Lorem Ipsum Dolor Sit Amet", item.Content); Assert.Equal(resultCreateCategory.Result.Id, item.CategoryId); } }