C
C#9mo ago
wwww

✅ 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");
}
}
[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");
}
[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");
}
[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");
}
1 Reply
Mayor McCheese
Mayor McCheese9mo ago
I only unit test code that represents a provable "competitive advantage", the things in code that I can prove work and continue to work, that also keep the lights on. Past that I just call my entry points pumping in scenarios that I expect to work a certain way.