public void AddCategory_ShouldThrowArgumentNullException_WhenCategoryIsNull()
        {
            // Arrange
            // Act
            using (var context = new AppDbContext(_dbContextOptions))
            {
                var         service = new CategoriesDataService(context);
                Func <Task> action  = async() => await service.AddCategory(null);

                // Assert
                action.Should().Throw <ArgumentNullException>();
            }
        }
        public async Task AddCategory_ShouldReturnAddedCategory_WhenCategorySaved()
        {
            // Arrange
            const string expectedCategoryName = "name";

            // Act
            using (var context = new AppDbContext(_dbContextOptions))
            {
                var service = new CategoriesDataService(context);
                var result  = await service.AddCategory(new Category { Name = expectedCategoryName });

                // Assert
                result.Name.Should().Be(expectedCategoryName);
            }
        }
        public async Task AddCategory_ShouldBlankCategoryId_WhenCategoryIdProvided()
        {
            // Arrange
            const int categoryId = 999;

            // Act
            using (var context = new AppDbContext(_dbContextOptions))
            {
                var service = new CategoriesDataService(context);
                var result  = await service.AddCategory(new Category { CategoryId = categoryId });

                // Assert
                result.CategoryId.Should().NotBe(categoryId);
            }
        }
コード例 #4
0
        public void CreateCategory_WithValidCategory_ShouldSaveAndReturnId()
        {
            // Arrange
            var category = new Category
            {
                Name = "Test Category"
            };

            using var dbContext = GetDbContext();

            var service = new CategoriesDataService(dbContext);

            // Act
            var result = service.CreateCategory(category);

            // Assert
            Assert.AreNotEqual(default, result, FailureMessages.ResultNotExpectedValue);
        public async Task GetCategories_ShouldReturnTwoCategories_WhenTwoCategoriesInDatabase()
        {
            // Arrange
            using (var context = new AppDbContext(_dbContextOptions))
            {
                context.Categories.AddRange(new Category(), new Category());
                await context.SaveChangesAsync();
            }

            // Act
            using (var context = new AppDbContext(_dbContextOptions))
            {
                var service = new CategoriesDataService(context);
                var result  = await service.GetCategories();

                // Assert
                result.Should().HaveCount(2);
            }
        }
コード例 #6
0
        public void CreateCategory_WithNullCategory_ShouldThrowException()
        {
            // Arrange
            using var dbContext = GetDbContext();

            var service = new CategoriesDataService(dbContext);

            Exception caughtException = null;

            // Act
            try
            {
                service.CreateCategory(null);
            }
            catch (Exception exception)
            {
                caughtException = exception;
            }

            // Assert
            Assert.IsNotNull(caughtException, FailureMessages.ExceptionNotThrown);
            Assert.IsInstanceOfType(caughtException, typeof(ArgumentNullException), FailureMessages.ExceptionNotExpectedType);
        }