Skip to content

Commit

Permalink
Small fix within the UserService and UpdateLastSeen unit tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
DustSwiffer committed Jul 23, 2024
1 parent fb4ad70 commit c371eb9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
3 changes: 1 addition & 2 deletions AdvancedAPI.Business/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ public UserService(
/// <inheritdoc />
public async Task UpdateLastSeen(string userId)
{
IEnumerable<LastSeen> lastSeens = await _lastSeenRepository.FindAsync(e => e.UserId == userId);
LastSeen? lastSeen = lastSeens.FirstOrDefault();
LastSeen? lastSeen = await _lastSeenRepository.GetByUserId(userId);

if (lastSeen != null)
{
Expand Down
56 changes: 56 additions & 0 deletions AdvancedAPI.Tests/Services/UserServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,60 @@ public async Task GetUserProfileReturnsSuccessfullyWithFilledLastSeen()

Assert.Equal(lastSeenResult.DateTime.ToString(CultureInfo.InvariantCulture), response.LastSeen);
}

/// <summary>
/// Tests if UpdateLastSeen calls the AddAsync of the LastSeen Repo just once.
/// </summary>
[Fact]
public async Task UpdateLastSeenCreatesNew()
{
string userId = "valid-user-id";
_lastSeenRepository.Setup(lsr => lsr.GetByUserId(It.IsAny<string>())).ReturnsAsync((LastSeen?)null);

await _UserService.UpdateLastSeen(userId);

_lastSeenRepository.Verify(lsr => lsr.AddAsync(It.IsAny<LastSeen>()), Times.Once);
}

/// <summary>
/// Tests if UpdateLastSeen calls the Update of the LastSeen Repo just once.
/// </summary>
[Fact]
public async Task UpdateLastSeenUpdates()
{
string userId = "valid-user-id";
LastSeen lastSeenResult = new LastSeen
{
Id = 1,
UserId = userId,
DateTime = DateTime.Now,
};

_lastSeenRepository.Setup(lsr => lsr.GetByUserId(It.IsAny<string>())).ReturnsAsync(lastSeenResult);

await _UserService.UpdateLastSeen(userId);

_lastSeenRepository.Verify(lsr => lsr.Update(It.IsAny<LastSeen>()), Times.Once);
}

/// <summary>
/// Tests if UpdateLastSeen calls the Saves the database changes.
/// </summary>
[Fact]
public async Task UpdateLastSeenSaves()
{
string userId = "valid-user-id";
LastSeen lastSeenResult = new LastSeen
{
Id = 1,
UserId = userId,
DateTime = DateTime.Now,
};

_lastSeenRepository.Setup(lsr => lsr.GetByUserId(It.IsAny<string>())).ReturnsAsync(lastSeenResult);

await _UserService.UpdateLastSeen(userId);

_lastSeenRepository.Verify(lsr => lsr.SaveAsync(), Times.Once);
}
}

0 comments on commit c371eb9

Please sign in to comment.