172 lines
5.0 KiB
C#
172 lines
5.0 KiB
C#
namespace LaaProductionWeb.Tests.Models
|
|
{
|
|
using LaaProductionWeb.API.Models;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
public class ModelResultTests
|
|
{
|
|
[Test]
|
|
public void ModelResult_ShouldBeImplicitlyCreated_FromModelStateDictionary()
|
|
{
|
|
// Act
|
|
ModelResult<string> modelResult = default(ModelStateDictionary);
|
|
|
|
// Assert
|
|
Assert.IsNotNull(modelResult);
|
|
}
|
|
|
|
[Test]
|
|
public void ModelResult_ShouldInitializeModelErrors_WhenCreatedFrom_ModelStateDictionary()
|
|
{
|
|
// Act
|
|
ModelResult<string> modelResult = default(ModelStateDictionary);
|
|
|
|
// Assert
|
|
Assert.IsNotNull(modelResult.ModelErrors);
|
|
}
|
|
|
|
[Test]
|
|
public void ModelResult_ShouldNotSucceeded_WhenCreatedFrom_ModelStateDictionary()
|
|
{
|
|
// Act
|
|
ModelResult<string> modelResult = default(ModelStateDictionary);
|
|
|
|
// Assert
|
|
Assert.IsFalse(modelResult.Succeeded);
|
|
}
|
|
|
|
[Test]
|
|
public void ModelResult_ShouldBeImplicitlyCreated_FromOtherObjects()
|
|
{
|
|
// Act
|
|
ModelResult<dynamic> modelResult = default(object);
|
|
|
|
// Assert
|
|
Assert.IsNotNull(modelResult);
|
|
}
|
|
|
|
[Test]
|
|
public void ModelResult_ShouldSucceeded_WhenCreated_FromOtherObjects()
|
|
{
|
|
// Act
|
|
ModelResult<dynamic> modelResult = default(object);
|
|
|
|
// Assert
|
|
Assert.IsTrue(modelResult.Succeeded);
|
|
}
|
|
|
|
[Test]
|
|
public void ModelResult_ShouldHaveSameValue_WhenCreated_FromOtherObjects()
|
|
{
|
|
// Arrange
|
|
var value = nameof(ModelResult<dynamic>);
|
|
ModelResult<string> modelResult = value;
|
|
|
|
// Act
|
|
var fieldValue = typeof(ModelResult<string>)
|
|
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
|
|
.FirstOrDefault(x => x.Name == nameof(value))
|
|
?.GetValue(modelResult);
|
|
|
|
// Assert
|
|
Assert.AreEqual(value, fieldValue);
|
|
}
|
|
|
|
[Test]
|
|
public void ModelResult_AddModelError_ShuldNotAddError_WhenKeyIsNull()
|
|
{
|
|
// Arrange
|
|
ModelResult<dynamic> modelResult = default(ModelStateDictionary);
|
|
var modelErrors = modelResult.ModelErrors;
|
|
|
|
// Act
|
|
var errorsCountBefore = modelErrors.Count;
|
|
modelResult.AddModelError(null, null);
|
|
var errorsCountAfter = modelErrors.Count;
|
|
|
|
// Assert
|
|
Assert.AreEqual(errorsCountBefore, errorsCountAfter);
|
|
}
|
|
|
|
[Test]
|
|
public void ModelResult_AddModelError_ShuldAddErrors_WhenKeyIsNotNull()
|
|
{
|
|
// Arrange
|
|
ModelResult<dynamic> modelResult = default(ModelStateDictionary);
|
|
var modelErrors = modelResult.ModelErrors;
|
|
|
|
// Act
|
|
var errorsCountBefore = modelErrors.Count;
|
|
modelResult.AddModelError(string.Empty, null);
|
|
var errorsCountAfter = modelErrors.Count;
|
|
|
|
// Assert
|
|
Assert.Greater(errorsCountAfter, errorsCountBefore);
|
|
}
|
|
|
|
[Test]
|
|
public void ModelResult_AddModelError_ShuldOverrideError_WhenKeyExists()
|
|
{
|
|
// Arrange
|
|
ModelResult<dynamic> modelResult = default(ModelStateDictionary);
|
|
var modelErrors = modelResult.ModelErrors;
|
|
|
|
// Act
|
|
modelResult.AddModelError(string.Empty, "1");
|
|
var valueBefore = modelErrors[string.Empty];
|
|
|
|
modelResult.AddModelError(string.Empty, "2");
|
|
var valueAfter = modelErrors[string.Empty];
|
|
|
|
// Assert
|
|
Assert.AreNotEqual(valueBefore, valueAfter);
|
|
}
|
|
|
|
[Test]
|
|
public void ModelResult_ReturnsNotFoundResult_WhenModelResult_IsNull()
|
|
{
|
|
// Arrange
|
|
ModelResult<dynamic> modelResult = null;
|
|
|
|
// Act
|
|
ActionResult actionResult = modelResult;
|
|
|
|
// Assert
|
|
Assert.AreEqual(actionResult.GetType(), typeof(NotFoundResult));
|
|
}
|
|
|
|
[Test]
|
|
public void ModelResult_ReturnsBadRequestObjectResult_WhenCreatedFrom_ModelStateDictionary()
|
|
{
|
|
// Arrange
|
|
ModelResult<dynamic> modelResult = default(ModelStateDictionary);
|
|
|
|
// Act
|
|
ActionResult actionResult = modelResult;
|
|
|
|
// Assert
|
|
Assert.AreEqual(actionResult.GetType(), typeof(BadRequestObjectResult));
|
|
}
|
|
|
|
[Test]
|
|
public void ModelResult_ReturnsOkObjectResult_WhenCreatedWhitValue()
|
|
{
|
|
// Arrange
|
|
ModelResult<dynamic> modelResult = default(string);
|
|
|
|
// Act
|
|
ActionResult actionResult = modelResult;
|
|
|
|
// Assert
|
|
Assert.AreEqual(actionResult.GetType(), typeof(OkObjectResult));
|
|
}
|
|
}
|
|
}
|