Untitled
user_8078570
plain_text
a year ago
4.2 kB
5
Indexable
using System;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
using Moq;
using Xunit;
public class SaveMethodTests
{
private readonly Fixture fixture;
public SaveMethodTests()
{
fixture = new Fixture(); // Using the existing fixture setup
}
[Fact]
public async Task Save_ShouldReturn_Status403Forbidden_WhenUserIsNotOwner()
{
// Arrange
var setupId = 1;
var user = "testuser";
var request = new NodeValuesRequest();
fixture.setupServiceMoq.Setup(s => s.Save(It.IsAny<int>(), It.IsAny<NodeValuesRequest>(), It.IsAny<string>()))
.Throws(new InvalidSetupOwnership("You are not the owner of this setup."));
var controller = new SetupController(fixture.loggerFactoryMoq.Object, fixture.setupServiceMoq.Object).WithMockedIdentity();
// Act
var act = async () => await controller.Save(setupId, request);
// Assert
await act.Should().ThrowAsync<InvalidSetupOwnership>()
.WithMessage("You are not the owner of this setup.");
}
[Fact]
public async Task Save_ShouldReturn_Status404NotFound_OnInexistentSetup()
{
// Arrange
var setupId = 1;
var user = "testuser";
var request = new NodeValuesRequest();
fixture.setupServiceMoq.Setup(s => s.Save(It.IsAny<int>(), It.IsAny<NodeValuesRequest>(), It.IsAny<string>()))
.Throws(new EntityNotFoundException("Setup", null));
var controller = new SetupController(fixture.loggerFactoryMoq.Object, fixture.setupServiceMoq.Object).WithMockedIdentity();
// Act
var act = async () => await controller.Save(setupId, request);
// Assert
await act.Should().ThrowAsync<EntityNotFoundException>();
}
[Fact]
public async Task Save_ShouldReturn_Status500InternalServerError_OnDbException()
{
// Arrange
var setupId = 1;
var user = "testuser";
var request = new NodeValuesRequest();
fixture.setupServiceMoq.Setup(s => s.Save(It.IsAny<int>(), It.IsAny<NodeValuesRequest>(), It.IsAny<string>()))
.Throws(new DbException("Database error"));
var controller = new SetupController(fixture.loggerFactoryMoq.Object, fixture.setupServiceMoq.Object).WithMockedIdentity();
// Act
var act = async () => await controller.Save(setupId, request);
// Assert
await act.Should().ThrowAsync<DbException>()
.WithMessage("Database error");
}
[Fact]
public async Task Save_ShouldReturn_Status200OK_WhenSaveIsSuccessful()
{
// Arrange
var setupId = 1;
var user = "testuser";
var request = new NodeValuesRequest();
var result = new SetupValuesResult();
fixture.setupServiceMoq.Setup(s => s.Save(It.IsAny<int>(), It.IsAny<NodeValuesRequest>(), It.IsAny<string>()))
.ReturnsAsync(result);
var controller = new SetupController(fixture.loggerFactoryMoq.Object, fixture.setupServiceMoq.Object).WithMockedIdentity();
// Act
var response = await controller.Save(setupId, request);
// Assert
response.Should().NotBeNull();
response.Should().Be(result);
}
[Fact]
public async Task Save_ShouldReturn_Status500InternalServerError_OnUnhandledException()
{
// Arrange
var setupId = 1;
var user = "testuser";
var request = new NodeValuesRequest();
fixture.setupServiceMoq.Setup(s => s.Save(It.IsAny<int>(), It.IsAny<NodeValuesRequest>(), It.IsAny<string>()))
.Throws(new Exception("Unexpected error"));
var controller = new SetupController(fixture.loggerFactoryMoq.Object, fixture.setupServiceMoq.Object).WithMockedIdentity();
// Act
var act = async () => await controller.Save(setupId, request);
// Assert
await act.Should().ThrowAsync<DbException>()
.WithMessage("An unhandled error occured when trying to add/update SetupValues");
}
}
Editor is loading...
Leave a Comment