Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CommBank-Server/Models/Goal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ public class Goal

[BsonRepresentation(BsonType.ObjectId)]
public string? UserId { get; set; }
}

public string? Icon { get; set; }
}
90 changes: 43 additions & 47 deletions CommBank.Tests/GoalControllerTests.cs
Original file line number Diff line number Diff line change
@@ -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<IGoalsService> _mockGoalsService;
private readonly Mock<IUsersService> _mockUsersService;
private readonly GoalController _controller;

public GoalControllerTests()
{
collections = new();
_mockGoalsService = new Mock<IGoalsService>();
_mockUsersService = new Mock<IUsersService>();

_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<Goal>
{
Assert.IsAssignableFrom<Goal>(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<Goal>(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<Goal>();

_mockGoalsService
.Setup(s => s.GetForUserAsync(userId))
.ReturnsAsync(emptyGoals);

// Act

var result = await _controller.GetForUser(userId);

// Assert
Assert.NotNull(result);
Assert.Empty(result);
}
}
}