Skip to content

Commit

Permalink
Adding AccountController tests, tidying up some formatting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dancita committed Mar 17, 2023
1 parent ff15b29 commit 54c5af6
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 16,6 @@ public AccountController(IManagementService managementService)
_managementService = managementService;
}


[HttpGet]
public async Task<ActionResult<IEnumerable<Account>>> GetAccounts()
{
Expand Down
1 change: 0 additions & 1 deletion src/Services/IManagementService.cs
Original file line number Diff line number Diff line change
@@ -1,5 1,4 @@
using AccountManagementAPI.Models;
using Microsoft.AspNetCore.Mvc;

namespace AccountManagementAPI.Services
{
Expand Down
1 change: 0 additions & 1 deletion src/Services/ManagementService.cs
Original file line number Diff line number Diff line change
@@ -1,5 1,4 @@
using AccountManagementAPI.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace AccountManagementAPI.Services
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 146,64 @@ public async Task GetAccount_InvalidAccountId_ThrowsAccountManagementAPIExceptio
// Assert
ex.Message.ShouldBe("An error occured while trying to get an account by id : 5");
}

[Fact]
public async Task ToggleAccountState_Successful_MustReturnNoContent()
{
// Arrange
var testAccount = new Account()
{
Id = 5,
AccountHolderName = "Test Account Holder",
IsActive = true
};
_managementService.Setup(x => x.GetAccount(5)).ReturnsAsync(testAccount);
_managementService.Setup(x => x.ToggleAccountStatus(testAccount)).ReturnsAsync(new Account()
{
Id = 5,
AccountHolderName = "Test Account Holder",
IsActive = false
});
var accountController = new AccountController(_managementService.Object);

// Act
var result = await accountController.ToggleAccountState(5);

// Assert
Assert.IsAssignableFrom<NoContentResult>(result);
}

[Fact]
public async Task ToggleAccountState_InvalidId_ThrowsNotFoundException()
{
// Arrange
var accountController = new AccountController(_managementService.Object);

// Act
var ex = await Assert.ThrowsAsync<NotFoundException>(() => accountController.ToggleAccountState(6));

// Assert
ex.Message.ShouldBe("Account wasn't found with id : 6");
}

[Fact]
public async Task ToggleAccountState_ValidIdUnsuccessfulUpdate_ThrowsAccountManagementAPIException()
{
// Arrange
var testAccount = new Account()
{
Id = 5,
AccountHolderName = "Test Account Holder",
IsActive = true
};
_managementService.Setup(x => x.GetAccount(5)).ReturnsAsync(testAccount);
var accountController = new AccountController(_managementService.Object);

// Act
var ex = await Assert.ThrowsAsync<AccountManagementAPIException>(() => accountController.ToggleAccountState(5));

// Assert
ex.Message.ShouldBe("An error occured while toggling the account status");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 60,13 @@ public async Task GetPhoneNumbersByAccountId_NoAccountIdAssignedToPhoneNumber_Th
[Fact]
public async Task DeletePhoneNumber_ValidId_MustReturnNoContent()
{
// Arrange
var phoneNumber = new PhoneNumber()
{
AccountId = 1,
Id = 2,
Number = "555-4321"
};
// Arrange
_managementService.Setup(x => x.GetPhoneNumber(2)).ReturnsAsync(phoneNumber);

_managementService.Setup(x => x.DeletePhoneNumber(phoneNumber)).ReturnsAsync(true);
Expand All @@ -83,6 83,7 @@ public async Task DeletePhoneNumber_ValidId_MustReturnNoContent()
[Fact]
public async Task DeletePhoneNumber_InvalidId_ThrowsNotFoundException()
{
// Arrange
var phoneNumberController = new PhoneNumberController(_managementService.Object);

// Act
Expand All @@ -95,13 96,13 @@ public async Task DeletePhoneNumber_InvalidId_ThrowsNotFoundException()
[Fact]
public async Task DeletePhoneNumber_UnsuccessfulDelete_ThrowsAccountManagementAPIException()
{
// Arrange
var phoneNumber = new PhoneNumber()
{
AccountId = 1,
Id = 2,
Number = "555-4321"
};
// Arrange
_managementService.Setup(x => x.GetPhoneNumber(2)).ReturnsAsync(phoneNumber);

_managementService.Setup(x => x.DeletePhoneNumber(phoneNumber)).ReturnsAsync(false);
Expand Down

0 comments on commit 54c5af6

Please sign in to comment.