C#C
C#3y ago
xdd

✅ Unit testing

Hi chat, should i unit test actions like that? i mean... like... is it any point in that?
[HttpGet]
public IActionResult GetAllChats()
{
    if (_db.Chats.Any())
    {
        return Json(_db.Chats.ToList());
    }
    else
    {
        return NotFound("There is no chats at this moment");
    }
}

[HttpPost]
public async Task<IActionResult> CreateChat([FromBody] CreateChatDto chat)
{
    _db.Chats.Add(new Chat
    {
        AuthorId = (int)chat.AuthorId!,
        Title = chat.Title
    });
    await _db.SaveChangesAsync();

    return Ok("Chat successfully created");
}

i mean, i could test that. but don't see any point do test previous 2
[HttpDelete]
public async Task<IActionResult> DeleteChat([FromBody] DeleteChatDTO helper)
{
    Chat? chat = await _db.Chats.FirstOrDefaultAsync(chat => chat.Id == helper.ChatId);

    if (chat == null)
        return NotFound("Chat with this Id not found");

    if (chat.AuthorId != helper.SenderId)
        return BadRequest("this user doesn't have permissions");

    await DisconnectAllUsersFromChatAsync(helper.ChatId);

    _db.Chats.Remove(chat);
    await _db.SaveChangesAsync();

    return Ok("Chat successfully deleted");
}
Was this page helpful?