public Task InitializeAsync() { this._mockDbContext = new Mock <DbContext>(); var catalogRepository = new DefaultRepositoryAsync <Catalog>(this._mockDbContext.Object); var productRepository = new DefaultRepositoryAsync <Product>(this._mockDbContext.Object); this.MockRepositoryFactory .Setup(x => x.CreateRepository <Catalog>()).Returns(catalogRepository); this.MockRepositoryFactory .Setup(x => x.CreateRepository <Product>()).Returns(productRepository); this._catalog = Catalog.Create(this.Fixture.Create <string>()); this._category = Category.Create(this.Fixture.Create <string>()); this._product = Product.Create(this.Fixture.Create <string>()); this._catalogCategory = this._catalog.AddCategory(this._category.CategoryId, this._category.DisplayName); this._mockDbContext .Setup(x => x.Set <Catalog>()) .ReturnsDbSet(new List <Catalog> { this._catalog }); this._mockDbContext .Setup(x => x.Set <Product>()) .ReturnsDbSet(new List <Product> { this._product }); this._validator = new CreateCatalogProductCommandValidator(this.MockRepositoryFactory.Object); this._requestHandler = new CommandHandler(this.MockRepositoryFactory.Object, this._validator); return(Task.CompletedTask); }
public Task InitializeAsync() { this._mockDbContext = new Mock <DbContext>(); var catalogRepository = new DefaultRepositoryAsync <Catalog, CatalogId>(this._mockDbContext.Object); var categoryRepository = new DefaultRepositoryAsync <Category, CategoryId>(this._mockDbContext.Object); this.MockRepositoryFactory.Setup(x => x.CreateRepository <Catalog, CatalogId>()) .Returns(catalogRepository); this.MockRepositoryFactory.Setup(x => x.CreateRepository <Category, CategoryId>()) .Returns(categoryRepository); this._catalog = Catalog.Create(this.Fixture.Create <string>()); this._category = Category.Create(this.Fixture.Create <string>()); this._mockDbContext.Setup(x => x.Set <Catalog>()) .ReturnsDbSet(new List <Catalog> { this._catalog }); this._mockDbContext.Setup(x => x.Set <Category>()) .ReturnsDbSet(new List <Category> { this._category }); this._validator = new CreateCatalogCategoryCommandValidator(this.MockRepositoryFactory.Object); this._requestHandler = new CommandHandler(this.MockRepositoryFactory.Object, this._validator); return(Task.CompletedTask); }
public async Task Create_Catalog_With_CatalogCategories_Successfully(string catalogName) { var mockDbContext = new Mock <DbContext>(); var categories = new List <Category>(); Enumerable.Range(0, 4).ToList().ForEach(idx => { categories.Add(Category.Create(this.Fixture.Create <string>())); }); mockDbContext.Setup(x => x.Set <Category>()) .ReturnsDbSet(categories); var categoryRepository = new DefaultRepositoryAsync <Category>(mockDbContext.Object); this.MockRepositoryFactory .Setup(x => x.CreateRepository <Category>()) .Returns(categoryRepository); var command = new CreateCatalogCommand { CatalogName = catalogName }; foreach (var category in categories) { command.AddCategory(category.CategoryId, this.Fixture.Create <string>()); } IRequestHandler <CreateCatalogCommand> handler = new CommandHandler(this.MockRepositoryFactory.Object, this._validator); await handler.Handle(command, this.CancellationToken); this.MockRepository.Verify(x => x.AddAsync(It.IsAny <Catalog>()), Times.Once); }