Repository navigation
Expand file tree
/
Copy pathExpectationsTest.cs
More file actions
135 lines (113 loc) · 6.47 KB
/
Copy pathExpectationsTest.cs
File metadata and controls
135 lines (113 loc) · 6.47 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using NUnit.Framework;
using NUnit.Framework.Internal;
using System;
using System.Threading.Tasks;
using UnitTestEx.Expectations;
namespace UnitTestEx.NUnit.Test.Other
{
[TestFixture]
public class ExpectationsTest
{
[Test]
public void ExceptionSuccess_ExpectException_Any()
{
var gt = GenericTester.Create().ExpectException().Any();
var ex = Assert.Throws<Exception>(() => ArrangerAssert(async () => await gt.ExpectationsArranger.AssertAsync(null, null)));
Assert.That(ex.Message, Is.EqualTo("Expected an exception; however, the execution was successful."));
gt.ExpectException().Any();
ArrangerAssert(async () => await gt.ExpectationsArranger.AssertAsync(null, new DivideByZeroException()));
}
[Test]
public void ExceptionSuccess_ExpectException_Message()
{
var gt = GenericTester.Create().ExpectException("error");
ArrangerAssert(async () => await gt.ExpectationsArranger.AssertAsync(null, new DivideByZeroException("error")));
gt.ExpectException("error");
ArrangerAssert(async () => await gt.ExpectationsArranger.AssertAsync(null, new DivideByZeroException("Error")));
gt.ExpectException("error");
var ex = Assert.Throws<Exception>(() => ArrangerAssert(async () => await gt.ExpectationsArranger.AssertAsync(null, new DivideByZeroException("not ok"))));
Assert.That(ex.Message, Is.EqualTo("Expected Exception message 'error' is not contained within 'not ok'."));
}
[Test]
public void ExceptionSuccess_ExpectException_Type()
{
var gt = GenericTester.Create().ExpectException().Type<DivideByZeroException>();
var ex = Assert.Throws<Exception>(() => ArrangerAssert(async () => await gt.ExpectationsArranger.AssertAsync(null, null)));
Assert.That(ex.Message, Is.EqualTo("Expected an exception; however, the execution was successful."));
gt.ExpectException().Type<DivideByZeroException>();
ex = Assert.Throws<Exception>(() => ArrangerAssert(async () => await gt.ExpectationsArranger.AssertAsync(null, new NotSupportedException())));
Assert.That(ex.Message, Is.EqualTo("Expected Exception type 'DivideByZeroException' not equal to actual 'NotSupportedException'."));
gt.ExpectException().Type<DivideByZeroException>();
ArrangerAssert(async () => await gt.ExpectationsArranger.AssertAsync(null, new DivideByZeroException()));
}
[Test]
public void ExpectError_None()
{
var gt = GenericTester.Create().ExpectError("No error will be raised.");
var ex = Assert.Throws<Exception>(() => ArrangerAssert(async () => await gt.ExpectationsArranger.AssertAsync(null, null)));
Assert.That(ex.Message, Is.EqualTo("Expected one or more errors; however, none were returned."));
}
[Test]
public void ExpectValue_Simple()
{
var gt = GenericTester.CreateFor<string>().ExpectValue("bob");
ArrangerAssert(async () => await gt.ExpectationsArranger.AssertValueAsync(null, "bob"));
gt.ExpectValue("bob");
var ex = Assert.Throws<Exception>(() => ArrangerAssert(async () => await gt.ExpectationsArranger.AssertValueAsync(null, "jenny")));
Assert.That(ex.Message, Does.Contain("Value is not equal: \"bob\" != \"jenny\"."));
gt.ExpectValue("bob");
ex = Assert.Throws<Exception>(() => ArrangerAssert(async () => await gt.ExpectationsArranger.AssertValueAsync(null, null)));
Assert.That(ex.Message, Does.Contain("Kind is not equal: String != Null."));
}
[Test]
public void ExpectValue_WithFunc()
{
var gt = GenericTester.CreateFor<string>().ExpectValue(_ => "bob");
ArrangerAssert(async () => await gt.ExpectationsArranger.AssertValueAsync(null, "bob"));
gt.ExpectValue(_ => "bob");
var ex = Assert.Throws<Exception>(() => ArrangerAssert(async () => await gt.ExpectationsArranger.AssertValueAsync(null, "jenny")));
Assert.That(ex.Message, Does.Contain("Value is not equal: \"bob\" != \"jenny\"."));
gt.ExpectValue(_ => "bob");
ex = Assert.Throws<Exception>(() => ArrangerAssert(async () => await gt.ExpectationsArranger.AssertValueAsync(null, null)));
Assert.That(ex.Message, Does.Contain("Kind is not equal: String != Null."));
}
[Test]
public void ExpectValueComplex()
{
var gt = GenericTester.CreateFor<Entity<int>>().ExpectValue(new Entity<int> { Id = 88, Name = "bob" });
ArrangerAssert(async () => await gt.ExpectationsArranger.AssertValueAsync(null, new Entity<int> { Id = 88, Name = "bob" }));
gt.ExpectValue(new Entity<int> { Id = 88, Name = "bob" });
ArrangerAssert(async () => await gt.ExpectationsArranger.AssertValueAsync(null, new { Id = 88, Name = "bob" }));
gt.ExpectValue(new Entity<int> { Id = 88, Name = "bob" });
var ex = Assert.Throws<Exception>(() => ArrangerAssert(async () => await gt.ExpectationsArranger.AssertValueAsync(null, new { Id = 99, Name = "bob" })));
Assert.That(ex.Message, Does.Contain("Path '$.id': Value is not equal: 88 != 99."));
gt = GenericTester.CreateFor<Entity<int>>().ExpectValue(new Entity<int> { Id = 88, Name = "bob" }, "id");
ArrangerAssert(async () => await gt.ExpectationsArranger.AssertValueAsync(null, new { Id = 99, Name = "bob" }));
}
public class Entity<TId>
{
public TId Id { get; set; }
public string Name { get; set; }
}
/// <summary>
/// Runs an asynchronous expectations assertion synchronously and unwraps assertion failures from task exceptions.
/// </summary>
/// <param name="func">The asynchronous assertion to execute.</param>
private static void ArrangerAssert(Func<Task> func)
{
try
{
var t = Task.Run(() => func());
t.Wait();
}
catch (AggregateException agex) when (agex.InnerException is not null && agex.InnerException is AssertionException aex)
{
throw new Exception(aex.Message, aex);
}
catch (AssertionException aex)
{
throw new Exception(aex.Message, aex);
}
}
}
}