public void ThrowsExceptionWhenBudgetWithMismatchedUserIds() { // Arrange. this.budget.UserId = 1; this.userId = 2; // Act. Exception caughtException = null; try { using (CheckbookContext context = new CheckbookContext(this.dbContextOptions)) { BudgetsRepository repository = new BudgetsRepository(context); repository.Add(this.budget, this.userId); } } catch (Exception ex) { caughtException = ex; } // Assert. Assert.IsNotNull(caughtException, "An exception should be thrown."); Assert.IsInstanceOfType(caughtException, typeof(ArgumentException), "This should be an argument exception."); string exceptionMessage = "A user ID is expected to match the passed in user ID for a budget.\r\nParameter name: budget.UserId"; Assert.AreEqual(exceptionMessage, caughtException.Message, "The exception message should be correct."); }
public void AddsBudgetToContext() { // Act. using (CheckbookContext context = new CheckbookContext(this.dbContextOptions)) { BudgetsRepository repository = new BudgetsRepository(context); repository.Add(this.budget, this.userId); } // Assert. using (CheckbookContext context = new CheckbookContext(this.dbContextOptions)) { int expectedBudgetCount = 6; Assert.AreEqual(expectedBudgetCount, context.Budgets.Count(), "There should be the correct number of budgets."); long expectedId = 6; Budget actual = ContextDataService.GetBudgetsSet(context) .FirstOrDefault(x => x.Id == expectedId); Assert.IsNotNull(actual, "A budget should be found."); Budget expected = this.budget; Assert.AreEqual(expected.Id, actual.Id, "The ID for the entity should match."); Assert.AreEqual(expected.Name, actual.Name, "The name for the entity should match."); Assert.AreEqual(expected.UserId, actual.UserId, "The user ID for the entity should match."); } }
public void ThrowsExceptionWhenIdPresent() { // Arrange. this.budget.Id = 5; // Act. Exception caughtException = null; try { using (CheckbookContext context = new CheckbookContext(this.dbContextOptions)) { BudgetsRepository repository = new BudgetsRepository(context); repository.Add(this.budget, this.userId); } } catch (Exception ex) { caughtException = ex; } // Assert. Assert.IsNotNull(caughtException, "An exception should be thrown."); Assert.IsInstanceOfType(caughtException, typeof(ArgumentException), "This should be an argument exception."); string exceptionMessage = "A new budget without a specified ID should have been used. To update a budget, use the Save method.\r\nParameter name: budget.Id"; Assert.AreEqual(exceptionMessage, caughtException.Message, "The exception message should be correct."); }
//AddBudget public void AddBudget(BudgetDTO budgetDTO, string userName) { var user = _repo.GetUserByUserName(userName); var budget = new Budget { Id = budgetDTO.Id, Name = budgetDTO.Name, Amount = budgetDTO.Amount, Current = budgetDTO.Current, UserId = user.Id }; _repo.Add(budget); }
public void ReturnsBudgetFromContext() { // Act. Budget actual; using (CheckbookContext context = new CheckbookContext(this.dbContextOptions)) { BudgetsRepository repository = new BudgetsRepository(context); actual = repository.Add(this.budget, this.userId); } // Assert. long expectedId = 6; Assert.AreEqual(expectedId, actual.Id, "The ID for the entity should match."); }