© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
1 reply
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");
    }
}
[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");
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

unit testing
C#CC# / help
2y ago
Unit testing
C#CC# / help
2y ago
Unit testing FakeItEasy
C#CC# / help
2y ago
✅ Unit Testing HttpClient
C#CC# / help
3y ago