From ec2ab570ad30efe0e3777a1ad6e24523d96b3605 Mon Sep 17 00:00:00 2001 From: Shakil Ahmed <68474195+urstrulyshakil@users.noreply.github.com> Date: Wed, 1 Apr 2026 02:02:03 +0600 Subject: [PATCH] Add files via upload --- GoalControllerTests.cs | 91 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 GoalControllerTests.cs diff --git a/GoalControllerTests.cs b/GoalControllerTests.cs new file mode 100644 index 0000000..7ff3002 --- /dev/null +++ b/GoalControllerTests.cs @@ -0,0 +1,91 @@ +using CommBank.Controllers; +using CommBank.Services; +using CommBank.Models; +using CommBank.Tests.Fake; +using Microsoft.AspNetCore.Mvc; + +namespace CommBank.Tests; + +public class GoalControllerTests +{ + private readonly FakeCollections collections; + + public GoalControllerTests() + { + collections = new(); + } + + [Fact] + public async void GetAll() + { + // Arrange + var goals = collections.GetGoals(); + var users = collections.GetUsers(); + IGoalsService goalsService = new FakeGoalsService(goals, goals[0]); + IUsersService usersService = new FakeUsersService(users, users[0]); + GoalController controller = new(goalsService, usersService); + + // Act + var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); + controller.ControllerContext.HttpContext = httpContext; + var result = await controller.Get(); + + // Assert + var index = 0; + foreach (Goal goal in result) + { + Assert.IsAssignableFrom(goal); + Assert.Equal(goals[index].Id, goal.Id); + Assert.Equal(goals[index].Name, goal.Name); + index++; + } + } + + [Fact] + public async void Get() + { + // Arrange + var goals = collections.GetGoals(); + var users = collections.GetUsers(); + IGoalsService goalsService = new FakeGoalsService(goals, goals[0]); + IUsersService usersService = new FakeUsersService(users, users[0]); + GoalController controller = new(goalsService, usersService); + + // Act + var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); + controller.ControllerContext.HttpContext = httpContext; + var result = await controller.Get(goals[0].Id!); + + // Assert + Assert.IsAssignableFrom(result.Value); + Assert.Equal(goals[0], result.Value); + Assert.NotEqual(goals[1], result.Value); + } + + [Fact] + public async void GetForUser() + { + // Arrange + var goals = collections.GetGoals(); + var users = collections.GetUsers(); + IGoalsService goalsService = new FakeGoalsService(goals, goals[0]); + IUsersService usersService = new FakeUsersService(users, users[0]); + GoalController controller = new(goalsService, usersService); + + // Act + var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); + controller.ControllerContext.HttpContext = httpContext; + var result = await controller.GetForUser(users[0].Id!); + + // Assert + Assert.NotNull(result); + var index = 0; + foreach (Goal goal in result!) + { + Assert.IsAssignableFrom(goal); + Assert.Equal(goals[index].Id, goal.Id); + Assert.Equal(goals[index].Name, goal.Name); + index++; + } + } +}