-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathControllerExtensionsTests.cs
More file actions
167 lines (134 loc) · 6.05 KB
/
ControllerExtensionsTests.cs
File metadata and controls
167 lines (134 loc) · 6.05 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System.IO;
using NUnit.Framework;
using TestStack.FluentMVCTesting.Tests.TestControllers;
namespace TestStack.FluentMVCTesting.Tests
{
[TestFixture]
class ControllerExtensionsShould
{
private ControllerExtensionsController _controller;
[SetUp]
public void Setup() => _controller = new ControllerExtensionsController();
[Test]
public void Give_controller_modelstate_errors()
{
_controller.WithModelErrors();
Assert.That(_controller.ModelState.IsValid, Is.False);
}
[Test]
public void Call_action()
{
_controller.WithCallTo(c => c.SomeAction());
Assert.That(_controller.SomeActionCalled);
}
[Test]
public void Call_child_action()
{
_controller.WithCallToChild(c => c.SomeChildAction());
Assert.That(_controller.SomeChildActionCalled);
}
[Test]
public void Throw_exception_for_child_action_call_to_non_child_action()
{
var exception = Assert.Throws<InvalidControllerActionException>(() => _controller.WithCallToChild(c => c.SomeAction()));
Assert.That(exception.Message, Is.EqualTo("Expected action SomeAction of controller ControllerExtensionsController to be a child action, but it didn't have the ChildActionOnly attribute."));
}
[Test]
public void Check_for_existent_temp_data_property()
{
const string key = "";
_controller.TempData[key] = "";
_controller.ShouldHaveTempDataProperty(key);
}
[Test]
public void Check_for_unexpected_non_existent_temp_data_property()
{
const string key = "";
var exception = Assert.Throws<TempDataAssertionException>(() =>
_controller.ShouldHaveTempDataProperty(key));
Assert.That(exception.Message, Is.EqualTo(string.Format(
"Expected TempData to have a non-null value with key '{0}', but none found.", key)));
}
[Test]
public void Check_for_existent_temp_data_property_and_check_value()
{
const string key = "";
const int value = 10;
_controller.TempData[key] = value;
_controller.ShouldHaveTempDataProperty(key, value);
}
[Test]
public void Check_for_existent_temp_data_property_and_check_invalid_value()
{
const string key = "";
const int actualValue = 0;
const int expectedValue = 1;
_controller.TempData[key] = actualValue;
var exception = Assert.Throws<TempDataAssertionException>(() =>
_controller.ShouldHaveTempDataProperty(key, expectedValue));
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected value for key '{0}' to be '{1}', but instead found '{2}'", key, expectedValue, actualValue)));
}
[Test]
public void Check_for_existent_temp_data_property_and_check_invalid_value_of_different_types()
{
const string key = "";
const int actualValue = 0;
const string expectedValue = "one";
_controller.TempData[key] = actualValue;
var exception = Assert.Throws<TempDataAssertionException>(() =>
_controller.ShouldHaveTempDataProperty(key, expectedValue));
Assert.That(exception.Message, Is.EqualTo(string.Format("Expected value to be of type {0}, but instead was {1}.", expectedValue.GetType().FullName, actualValue.GetType().FullName)));
}
[Test]
public void Check_for_existent_temp_data_property_and_check_value_valid_using_referential_equality()
{
const string key = "";
MemoryStream expectedValue = new MemoryStream();
_controller.TempData[key] = expectedValue;
_controller.ShouldHaveTempDataProperty(key, expectedValue);
}
[Test]
public void Check_for_existent_temp_data_property_and_check_value_using_valid_predicate()
{
const string key = "";
const int value = 1;
_controller.TempData[key] = value;
_controller
.ShouldHaveTempDataProperty<int>(key, x => x == value);
}
[Test]
public void Check_for_existent_temp_data_property_and_check_value_using_invalid_predicate()
{
const string key = "";
_controller.TempData[key] = 1;
var exception = Assert.Throws<TempDataAssertionException>(() =>
_controller.ShouldHaveTempDataProperty<int>(key, x => x == 0));
Assert.That(exception.Message, Is.EqualTo("Expected view model to pass the given condition, but it failed."));
}
[Test]
public void Check_for_unexpected_non_existent_temp_data_property_when_supplied_with_predicate()
{
const string key = "";
var exception = Assert.Throws<TempDataAssertionException>(() =>
_controller.ShouldHaveTempDataProperty<int>(key, x => x == 0));
Assert.That(exception.Message, Is.EqualTo(string.Format(
"Expected TempData to have a non-null value with key '{0}', but none found.", key)));
}
[Test]
public void Check_for_non_existent_temp_data_property()
{
_controller
.ShouldNotHaveTempDataProperty("");
}
[Test]
public void Check_for_unexpected_existent_temp_data_property()
{
const string Key = "";
_controller.TempData[Key] = "";
var exception = Assert.Throws<TempDataAssertionException>(() =>
_controller.ShouldNotHaveTempDataProperty(Key));
Assert.That(exception.Message, Is.EqualTo(string.Format(
"Expected TempData to have no value with key '{0}', but found one.", Key)));
}
}
}