Files
Heimdall/tests/Heimdall.Application.Tests/Services/ServiceResultTests.cs
T

128 lines
3.1 KiB
C#
Raw Normal View History

using Heimdall.Application.Common;
namespace Heimdall.Application.Tests.Services;
public class ServiceResultTests
{
[Fact]
public void Success_should_return_is_success()
{
var result = ServiceResult.Success();
result.IsSuccess.ShouldBeTrue();
result.Error.ShouldBeNull();
result.StatusCode.ShouldBeNull();
}
[Fact]
public void Failure_should_return_is_failure_with_code()
{
var result = ServiceResult.Failure("Something went wrong", 400);
result.IsSuccess.ShouldBeFalse();
result.Error.ShouldBe("Something went wrong");
result.StatusCode.ShouldBe(400);
}
[Fact]
public void Failure_should_default_to_400()
{
var result = ServiceResult.Failure("Bad request");
result.StatusCode.ShouldBe(400);
}
[Fact]
public void NotFound_should_set_404()
{
var result = ServiceResult.NotFound("Site not found");
result.IsSuccess.ShouldBeFalse();
result.Error.ShouldBe("Site not found");
result.StatusCode.ShouldBe(404);
}
[Fact]
public void NotFound_should_have_default_message()
{
var result = ServiceResult.NotFound();
result.Error.ShouldBe("Resource not found");
}
[Fact]
public void Unauthorized_should_set_401()
{
var result = ServiceResult.Unauthorized("Not allowed");
result.IsSuccess.ShouldBeFalse();
result.Error.ShouldBe("Not allowed");
result.StatusCode.ShouldBe(401);
}
[Fact]
public void Unauthorized_should_have_default_message()
{
var result = ServiceResult.Unauthorized();
result.Error.ShouldBe("Unauthorized");
}
[Fact]
public void Conflict_should_set_409()
{
var result = ServiceResult.Conflict("Duplicate entry");
result.IsSuccess.ShouldBeFalse();
result.Error.ShouldBe("Duplicate entry");
result.StatusCode.ShouldBe(409);
}
}
public class ServiceResultGenericTests
{
[Fact]
public void Success_should_return_data()
{
var result = ServiceResult<string>.Success("hello");
result.IsSuccess.ShouldBeTrue();
result.Data.ShouldBe("hello");
}
[Fact]
public void Failure_should_return_is_failure()
{
var result = ServiceResult<int>.Failure("Invalid value", 400);
result.IsSuccess.ShouldBeFalse();
result.Error.ShouldBe("Invalid value");
result.StatusCode.ShouldBe(400);
}
[Fact]
public void NotFound_should_set_404()
{
var result = ServiceResult<string>.NotFound("Item not found");
result.IsSuccess.ShouldBeFalse();
result.Error.ShouldBe("Item not found");
result.StatusCode.ShouldBe(404);
}
[Fact]
public void Unauthorized_should_set_401()
{
var result = ServiceResult<object>.Unauthorized();
result.StatusCode.ShouldBe(401);
}
[Fact]
public void Conflict_should_set_409()
{
var result = ServiceResult<Guid>.Conflict("Already exists");
result.StatusCode.ShouldBe(409);
}
}