1
0

Moved all UnitTests to a single project. Implemented parts of AspNetCore UnitTests.

This commit is contained in:
2022-07-17 12:21:05 +02:00
parent 73038bbe5a
commit a26d6a0036
46 changed files with 2411 additions and 105 deletions

View File

@@ -0,0 +1,97 @@
using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTests.AspNetCore.Extensions
{
[TestClass]
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public class ModelStateDictionaryExtensionsTests
{
private TestModel testModel;
[TestInitialize]
public void InitializeTests()
{
testModel = new TestModel
{
ValueA = "A",
ValueB = "B",
SubModel = new TestSubModel
{
SubValueA = "a",
SubValueB = "b"
}
};
}
[TestMethod]
public void ShouldAddNormalModelError()
{
// arrange
var modelState = new ModelStateDictionary();
// act
modelState.AddModelError(testModel, m => m.ValueA, "ShitHappens");
// assert
Assert.AreEqual(1, modelState.Count);
Assert.AreEqual("ValueA", modelState.Keys.First());
Assert.AreEqual("ShitHappens", modelState.Values.First().Errors.First().ErrorMessage);
}
[TestMethod]
public void ShouldAddExtendedModelError()
{
// arrange
var modelState = new ModelStateDictionary();
// act
modelState.AddModelError(testModel, m => m.SubModel.SubValueB, "ShitHappens");
// assert
Assert.AreEqual(1, modelState.Count);
Assert.AreEqual("SubModel.SubValueB", modelState.Keys.First());
Assert.AreEqual("ShitHappens", modelState.Values.First().Errors.First().ErrorMessage);
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ShouldThrowArgumentNull()
{
// arrange
ModelStateDictionary modelState = null;
// act
modelState.AddModelError(testModel, m => m.SubModel.SubValueB, "ShitHappens");
}
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void ShouldThrowInvalidOperation()
{
// arrange
var modelState = new ModelStateDictionary();
// act
modelState.AddModelError(testModel, m => m, "ShitHappens");
}
internal class TestModel
{
public string ValueA { get; set; }
public string ValueB { get; set; }
public TestSubModel SubModel { get; set; }
}
internal class TestSubModel
{
public string SubValueA { get; set; }
public string SubValueB { get; set; }
}
}
}