forked from Sustainsys/Saml2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExceptionTestHelpers.cs
53 lines (44 loc) · 1.55 KB
/
ExceptionTestHelpers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using FluentAssertions;
namespace Kentor.AuthServices.Tests
{
/// <summary>
/// Helper class for testing standard exception constructors.
/// </summary>
static class ExceptionTestHelpers
{
public static void TestDefaultCtor<TException>()
where TException : Exception, new()
{
new TException();
// Deliberately no asserts - just testing that default ctor can
// be run and exists.
}
public static void TestSerializationCtor<TException>()
where TException : Exception, new()
{
var original = new TException();
var msg = "Some Message";
typeof(Exception).GetField("_message",
BindingFlags.NonPublic | BindingFlags.Instance)
.SetValue(original, msg);
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(ms, original);
ms.Seek(0, SeekOrigin.Begin);
var deserialized = (TException)formatter.Deserialize(ms);
deserialized.Message.Should().Be(msg);
deserialized.ShouldBeEquivalentTo(original);
}
}
}
}