From 053c2302e800654b2d77a6dd411ee73ebf97a784 Mon Sep 17 00:00:00 2001 From: amoghrraikar Date: Tue, 20 Jan 2026 19:16:26 +0530 Subject: [PATCH 1/2] Add optional Icon field to Goal model --- CommBank-Server/Models/Goal.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CommBank-Server/Models/Goal.cs b/CommBank-Server/Models/Goal.cs index 77ff1ad..0099f51 100644 --- a/CommBank-Server/Models/Goal.cs +++ b/CommBank-Server/Models/Goal.cs @@ -27,4 +27,6 @@ public class Goal [BsonRepresentation(BsonType.ObjectId)] public string? UserId { get; set; } -} \ No newline at end of file + + public string? Icon { get; set; } +} From cb5fb1aa9d22bed69702f588f440fa4eab52e5e4 Mon Sep 17 00:00:00 2001 From: amoghrraikar Date: Sun, 1 Feb 2026 16:51:14 +0530 Subject: [PATCH 2/2] Add tests for GetGoalsForUser route --- CommBank.Tests/GoalControllerTests.cs | 90 +++++++++++++-------------- 1 file changed, 43 insertions(+), 47 deletions(-) diff --git a/CommBank.Tests/GoalControllerTests.cs b/CommBank.Tests/GoalControllerTests.cs index 8380181..d9c6691 100644 --- a/CommBank.Tests/GoalControllerTests.cs +++ b/CommBank.Tests/GoalControllerTests.cs @@ -1,74 +1,70 @@ -using CommBank.Controllers; -using CommBank.Services; -using CommBank.Models; -using CommBank.Tests.Fake; -using Microsoft.AspNetCore.Mvc; +using Xunit; +using Moq; +using System.Collections.Generic; +using System.Threading.Tasks; -namespace CommBank.Tests; +using CommBank.Controllers; +using CommBank.Models; +using CommBank.Services; public class GoalControllerTests { - private readonly FakeCollections collections; + private readonly Mock _mockGoalsService; + private readonly Mock _mockUsersService; + private readonly GoalController _controller; public GoalControllerTests() { - collections = new(); + _mockGoalsService = new Mock(); + _mockUsersService = new Mock(); + + _controller = new GoalController( + _mockGoalsService.Object, + _mockUsersService.Object + ); } [Fact] - public async void GetAll() + public async Task GetForUser_ReturnsGoals_WhenUserHasGoals() { // 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(); + string userId = "507f1f77bcf86cd799439011"; - // Assert - var index = 0; - foreach (Goal goal in result) + var goals = new List { - Assert.IsAssignableFrom(goal); - Assert.Equal(goals[index].Id, goal.Id); - Assert.Equal(goals[index].Name, goal.Name); - index++; - } - } + new Goal { Id = "1", UserId = userId }, + new Goal { Id = "2", UserId = userId } + }; - [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); + _mockGoalsService + .Setup(s => s.GetForUserAsync(userId)) + .ReturnsAsync(goals); // Act - var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); - controller.ControllerContext.HttpContext = httpContext; - var result = await controller.Get(goals[0].Id!); + var result = await _controller.GetForUser(userId); // Assert - Assert.IsAssignableFrom(result.Value); - Assert.Equal(goals[0], result.Value); - Assert.NotEqual(goals[1], result.Value); + Assert.NotNull(result); + Assert.Equal(2, result.Count); } [Fact] - public async void GetForUser() + public async Task GetForUser_ReturnsEmptyList_WhenUserHasNoGoals() { // Arrange - + string userId = "507f1f77bcf86cd799439012"; + + var emptyGoals = new List(); + + _mockGoalsService + .Setup(s => s.GetForUserAsync(userId)) + .ReturnsAsync(emptyGoals); + // Act - + var result = await _controller.GetForUser(userId); + // Assert + Assert.NotNull(result); + Assert.Empty(result); } -} \ No newline at end of file +}