Moved all UnitTests to a single project. Implemented parts of AspNetCore UnitTests.
This commit is contained in:
159
UnitTests/AspNetCore/Extensions/SessionExtensionsTests.cs
Normal file
159
UnitTests/AspNetCore/Extensions/SessionExtensionsTests.cs
Normal file
@@ -0,0 +1,159 @@
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace UnitTests.AspNetCore.Extensions
|
||||
{
|
||||
[TestClass]
|
||||
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
||||
public class SessionExtensionsTests
|
||||
{
|
||||
private Mock<ISession> sessionMock;
|
||||
|
||||
private string sessionKey;
|
||||
private byte[] sessionValue;
|
||||
|
||||
private TestModel model;
|
||||
|
||||
internal class TestModel
|
||||
{
|
||||
public string ValueA { get; set; }
|
||||
|
||||
public string ValueB { get; set; }
|
||||
}
|
||||
|
||||
[TestInitialize]
|
||||
public void InitializeTests()
|
||||
{
|
||||
sessionKey = null;
|
||||
sessionValue = null;
|
||||
|
||||
model = new TestModel
|
||||
{
|
||||
ValueA = "A",
|
||||
ValueB = "B"
|
||||
};
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShouldCheckKeyExists()
|
||||
{
|
||||
// arrange
|
||||
sessionKey = "exists";
|
||||
var session = GetSession();
|
||||
|
||||
// act
|
||||
bool result1 = session.HasKey("exists");
|
||||
bool result2 = session.HasKey("somewhere");
|
||||
|
||||
// assert
|
||||
Assert.IsTrue(result1);
|
||||
Assert.IsFalse(result2);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShouldGetValue()
|
||||
{
|
||||
// arrange
|
||||
sessionKey = "test";
|
||||
sessionValue = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(model));
|
||||
var session = GetSession();
|
||||
|
||||
// act
|
||||
var result = session.GetValue<TestModel>(sessionKey);
|
||||
|
||||
// assert
|
||||
Assert.IsNotNull(result);
|
||||
Assert.AreEqual(model.ValueA, result.ValueA);
|
||||
Assert.AreEqual(model.ValueB, result.ValueB);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShouldGetNull()
|
||||
{
|
||||
// arrange
|
||||
sessionKey = "foo";
|
||||
sessionValue = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(model));
|
||||
var session = GetSession();
|
||||
|
||||
// act
|
||||
var result = session.GetValue<TestModel>("bar");
|
||||
|
||||
// assert
|
||||
Assert.IsNull(result);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShouldGetValueWithFallback()
|
||||
{
|
||||
// arrange
|
||||
sessionKey = "test";
|
||||
sessionValue = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(model));
|
||||
var session = GetSession();
|
||||
|
||||
// act
|
||||
var result = session.GetValue(sessionKey, new TestModel());
|
||||
|
||||
// assert
|
||||
Assert.IsNotNull(result);
|
||||
Assert.AreEqual(model.ValueA, result.ValueA);
|
||||
Assert.AreEqual(model.ValueB, result.ValueB);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShouldGetFallback()
|
||||
{
|
||||
// arrange
|
||||
sessionKey = "foo";
|
||||
sessionValue = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(model));
|
||||
var session = GetSession();
|
||||
|
||||
// act
|
||||
var result = session.GetValue("bar", new TestModel());
|
||||
|
||||
// assert
|
||||
Assert.IsNotNull(result);
|
||||
Assert.AreEqual(null, result.ValueA);
|
||||
Assert.AreEqual(null, result.ValueB);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShouldSetValue()
|
||||
{
|
||||
// arrange
|
||||
string key = "foobar";
|
||||
var session = GetSession();
|
||||
|
||||
// act
|
||||
session.SetValue(key, model);
|
||||
|
||||
// arrange
|
||||
Assert.AreEqual(key, sessionKey);
|
||||
Assert.AreEqual(JsonConvert.SerializeObject(model), Encoding.UTF8.GetString(sessionValue));
|
||||
}
|
||||
|
||||
private ISession GetSession()
|
||||
{
|
||||
string[] keys = new[] { sessionKey };
|
||||
|
||||
sessionMock = new Mock<ISession>();
|
||||
sessionMock
|
||||
.Setup(s => s.TryGetValue(It.IsAny<string>(), out sessionValue))
|
||||
.Returns<string, byte[]>((key, value) => sessionKey == key);
|
||||
sessionMock
|
||||
.Setup(s => s.Set(It.IsAny<string>(), It.IsAny<byte[]>()))
|
||||
.Callback<string, byte[]>((key, value) =>
|
||||
{
|
||||
sessionKey = key;
|
||||
sessionValue = value;
|
||||
});
|
||||
sessionMock
|
||||
.Setup(s => s.Keys)
|
||||
.Returns(keys);
|
||||
|
||||
return sessionMock.Object;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user