コード例 #1
0
        public void CalculateBonus(int employeeSalary, int allEmployeesSalary, int bonusPool, int expectedBonus)
        {
            // Arrange
            var sut = new BonusCalculator();

            // Act
            var actualBonus = sut.Calculate(employeeSalary, allEmployeesSalary, bonusPool);

            // Assert
            Assert.That(actualBonus, Is.EqualTo(expectedBonus));
        }
コード例 #2
0
        public void When_EmployeeNotFound_Then_Exception()
        {
            var mockEmployeeService = new Mock <IEmployeeService>();

            mockEmployeeService.Setup(x => x.GetAsync(It.IsAny <int>())).ReturnsAsync((EmployeeDto)null);

            var testEmployeeId = 5;

            var bonusCalculator = new BonusCalculator(mockEmployeeService.Object);

            Func <Task> action = async() => { await bonusCalculator.Calculate(testEmployeeId, 1000); };

            action.Should().Throw <EmployeeNotFoundException>().WithMessage($"Employee not found for id - {testEmployeeId }");
        }
コード例 #3
0
        public async Task When_EmployeeFound_Then_CalculateBonus(int employeeId, int totalBonusPool, decimal expectedBonus)
        {
            var dbContextOptionBuilder = new DbContextOptionsBuilder <AppDbContext>();

            dbContextOptionBuilder.UseInMemoryDatabase(databaseName: "HrDb");

            var context = new AppDbContext(dbContextOptionBuilder.Options);

            //test can break if seed data changes.
            // we should have our own seed/mock data
            if (!context.Employees.Any())
            {
                DbContextGenerator.SeedData(context);
            }

            var employeeService = new EmployeeService(context);

            var bonusCalculator = new BonusCalculator(employeeService);

            var bonus = await bonusCalculator.Calculate(employeeId, totalBonusPool);

            bonus.Should().NotBeNull().And.BeOfType <BonusPoolCalculatorResultDto>();
            bonus.Amount.Should().Be(expectedBonus);
        }