/
ThrowAssertionsSpecs.cs
74 lines (64 loc) · 2.92 KB
/
ThrowAssertionsSpecs.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using FakeItEasy;
using Xunit;
using Xunit.Sdk;
namespace FluentAssertions.Specs
{
public class ThrowAssertionsSpecs
{
[Fact]
public void When_subject_throws_expected_exception_it_should_not_do_anything()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
Does testSubject = Does.Throw<InvalidOperationException>();
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
testSubject.Invoking(x => x.Do()).Should().Throw<InvalidOperationException>();
}
[Fact]
public void When_action_throws_expected_exception_it_should_not_do_anything()
{
//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
var act = new Action(() => throw new InvalidOperationException("Some exception"));
//-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
act.Should().Throw<InvalidOperationException>();
}
[Fact]
public void When_subject_does_not_throw_exception_but_one_was_expected_it_should_throw_with_clear_description()
{
try
{
Does testSubject = Does.NotThrow();
testSubject.Invoking(x => x.Do()).Should().Throw<Exception>();
throw new XunitException("Should().Throw() did not throw");
}
catch (XunitException ex)
{
ex.Message.Should().Be(
"Expected a <System.Exception> to be thrown, but no exception was thrown.");
}
}
[Fact]
public void When_action_does_not_throw_exception_but_one_was_expected_it_should_throw_with_clear_description()
{
try
{
var act = new Action(() => { });
act.Should().Throw<Exception>();
throw new XunitException("Should().Throw() did not throw");
}
catch (XunitException ex)
{
ex.Message.Should().Be(
"Expected a <System.Exception> to be thrown, but no exception was thrown.");
}
}
}
}