Skip to content

Commit a04a9fb

Browse files
committed
Adjusted the strategy for coping with FA5's Subject Identification. This time to use compiler directives to set the expected result
1 parent d91bcd3 commit a04a9fb

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

FluentAssertionsMvc.sln

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,15 @@ Global
7272
{53589F79-0908-409A-8366-3E18DC637600}.Release|Mixed Platforms.Build.0 = Release|Any CPU
7373
{53589F79-0908-409A-8366-3E18DC637600}.Release|x86.ActiveCfg = Release|Any CPU
7474
{3CB00FF9-3DC2-460F-82E8-EBFB6339247D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
75+
{3CB00FF9-3DC2-460F-82E8-EBFB6339247D}.Debug|Any CPU.Build.0 = Debug|Any CPU
7576
{3CB00FF9-3DC2-460F-82E8-EBFB6339247D}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
7677
{3CB00FF9-3DC2-460F-82E8-EBFB6339247D}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
7778
{3CB00FF9-3DC2-460F-82E8-EBFB6339247D}.Debug|x86.ActiveCfg = Debug|Any CPU
78-
{3CB00FF9-3DC2-460F-82E8-EBFB6339247D}.Release|Any CPU.ActiveCfg = Debug|Any CPU
79-
{3CB00FF9-3DC2-460F-82E8-EBFB6339247D}.Release|Any CPU.Build.0 = Debug|Any CPU
80-
{3CB00FF9-3DC2-460F-82E8-EBFB6339247D}.Release|Mixed Platforms.ActiveCfg = Debug|Any CPU
81-
{3CB00FF9-3DC2-460F-82E8-EBFB6339247D}.Release|Mixed Platforms.Build.0 = Debug|Any CPU
82-
{3CB00FF9-3DC2-460F-82E8-EBFB6339247D}.Release|x86.ActiveCfg = Debug|Any CPU
79+
{3CB00FF9-3DC2-460F-82E8-EBFB6339247D}.Release|Any CPU.ActiveCfg = Release|Any CPU
80+
{3CB00FF9-3DC2-460F-82E8-EBFB6339247D}.Release|Any CPU.Build.0 = Release|Any CPU
81+
{3CB00FF9-3DC2-460F-82E8-EBFB6339247D}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
82+
{3CB00FF9-3DC2-460F-82E8-EBFB6339247D}.Release|Mixed Platforms.Build.0 = Release|Any CPU
83+
{3CB00FF9-3DC2-460F-82E8-EBFB6339247D}.Release|x86.ActiveCfg = Release|Any CPU
8384
{95B0CEBD-D9B8-4C41-A918-B13317A9AC16}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
8485
{95B0CEBD-D9B8-4C41-A918-B13317A9AC16}.Debug|Any CPU.Build.0 = Debug|Any CPU
8586
{95B0CEBD-D9B8-4C41-A918-B13317A9AC16}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
@@ -105,11 +106,11 @@ Global
105106
{B9F72C07-90A8-4A15-815B-7618530CA889}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
106107
{B9F72C07-90A8-4A15-815B-7618530CA889}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
107108
{B9F72C07-90A8-4A15-815B-7618530CA889}.Debug|x86.ActiveCfg = Debug|Any CPU
108-
{B9F72C07-90A8-4A15-815B-7618530CA889}.Release|Any CPU.ActiveCfg = Debug|Any CPU
109-
{B9F72C07-90A8-4A15-815B-7618530CA889}.Release|Any CPU.Build.0 = Debug|Any CPU
110-
{B9F72C07-90A8-4A15-815B-7618530CA889}.Release|Mixed Platforms.ActiveCfg = Debug|Any CPU
111-
{B9F72C07-90A8-4A15-815B-7618530CA889}.Release|Mixed Platforms.Build.0 = Debug|Any CPU
112-
{B9F72C07-90A8-4A15-815B-7618530CA889}.Release|x86.ActiveCfg = Debug|Any CPU
109+
{B9F72C07-90A8-4A15-815B-7618530CA889}.Release|Any CPU.ActiveCfg = Release|Any CPU
110+
{B9F72C07-90A8-4A15-815B-7618530CA889}.Release|Any CPU.Build.0 = Release|Any CPU
111+
{B9F72C07-90A8-4A15-815B-7618530CA889}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
112+
{B9F72C07-90A8-4A15-815B-7618530CA889}.Release|Mixed Platforms.Build.0 = Release|Any CPU
113+
{B9F72C07-90A8-4A15-815B-7618530CA889}.Release|x86.ActiveCfg = Release|Any CPU
113114
{E5BFF617-46A3-48F7-83AA-BD34376F1AB2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
114115
{E5BFF617-46A3-48F7-83AA-BD34376F1AB2}.Debug|Any CPU.Build.0 = Debug|Any CPU
115116
{E5BFF617-46A3-48F7-83AA-BD34376F1AB2}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
@@ -125,11 +126,11 @@ Global
125126
{650A92FF-8874-41CD-BCDA-BE583F3F2638}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
126127
{650A92FF-8874-41CD-BCDA-BE583F3F2638}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
127128
{650A92FF-8874-41CD-BCDA-BE583F3F2638}.Debug|x86.ActiveCfg = Debug|Any CPU
128-
{650A92FF-8874-41CD-BCDA-BE583F3F2638}.Release|Any CPU.ActiveCfg = Debug|Any CPU
129-
{650A92FF-8874-41CD-BCDA-BE583F3F2638}.Release|Any CPU.Build.0 = Debug|Any CPU
130-
{650A92FF-8874-41CD-BCDA-BE583F3F2638}.Release|Mixed Platforms.ActiveCfg = Debug|Any CPU
131-
{650A92FF-8874-41CD-BCDA-BE583F3F2638}.Release|Mixed Platforms.Build.0 = Debug|Any CPU
132-
{650A92FF-8874-41CD-BCDA-BE583F3F2638}.Release|x86.ActiveCfg = Debug|Any CPU
129+
{650A92FF-8874-41CD-BCDA-BE583F3F2638}.Release|Any CPU.ActiveCfg = Release|Any CPU
130+
{650A92FF-8874-41CD-BCDA-BE583F3F2638}.Release|Any CPU.Build.0 = Release|Any CPU
131+
{650A92FF-8874-41CD-BCDA-BE583F3F2638}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
132+
{650A92FF-8874-41CD-BCDA-BE583F3F2638}.Release|Mixed Platforms.Build.0 = Release|Any CPU
133+
{650A92FF-8874-41CD-BCDA-BE583F3F2638}.Release|x86.ActiveCfg = Release|Any CPU
133134
EndGlobalSection
134135
GlobalSection(SolutionProperties) = preSolution
135136
HideSolutionNode = FALSE

tests/FluentAssertions.Mvc.Tests.Shared/RedirectToRoute_Tests.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ public void WithRouteValue_GivenExpected_ShouldPass()
7070
[Test]
7171
public void WithRouteValue_GivenUnexpected_ShouldFail()
7272
{
73+
var subjectIdentifier = GetSubjectIdentifier();
74+
7375
ActionResult result = new RedirectToRouteResult("", new RouteValueDictionary(
7476
new
7577
{
@@ -80,7 +82,7 @@ public void WithRouteValue_GivenUnexpected_ShouldFail()
8082
.BeRedirectToRouteResult()
8183
.WithRouteValue("Id", "11");
8284
a.Should().Throw<Exception>()
83-
.WithMessage("Expected Subject.RouteValues to contain value \"11\" at key \"Id\", but found \"22\".");
85+
.WithMessage($"Expected {subjectIdentifier} to contain value \"11\" at key \"Id\", but found \"22\".");
8486
}
8587

8688
[Test]
@@ -100,6 +102,8 @@ public void WithController_GivenExpected_ShouldPass()
100102
[Test]
101103
public void WithController_GivenUnexpected_ShouldFail()
102104
{
105+
var subjectIdentifier = GetSubjectIdentifier();
106+
103107
ActionResult result = new RedirectToRouteResult("", new RouteValueDictionary(
104108
new
105109
{
@@ -110,7 +114,7 @@ public void WithController_GivenUnexpected_ShouldFail()
110114
.BeRedirectToRouteResult()
111115
.WithController("xyz");
112116
a.Should().Throw<Exception>()
113-
.WithMessage("Expected Subject.RouteValues to contain value \"xyz\" at key \"Controller\", but found \"home\".");
117+
.WithMessage($"Expected {subjectIdentifier} to contain value \"xyz\" at key \"Controller\", but found \"home\".");
114118
}
115119

116120
[Test]
@@ -130,6 +134,8 @@ public void WithAction_GivenExpected_ShouldPass()
130134
[Test]
131135
public void WithAction_GivenUnexpected_ShouldFail()
132136
{
137+
var subjectIdentifier = GetSubjectIdentifier();
138+
133139
ActionResult result = new RedirectToRouteResult("", new RouteValueDictionary(
134140
new
135141
{
@@ -140,7 +146,7 @@ public void WithAction_GivenUnexpected_ShouldFail()
140146
.BeRedirectToRouteResult()
141147
.WithAction("xyz");
142148
a.Should().Throw<Exception>()
143-
.WithMessage("Expected Subject.RouteValues to contain value \"xyz\" at key \"Action\", but found \"index\".");
149+
.WithMessage($"Expected {subjectIdentifier} to contain value \"xyz\" at key \"Action\", but found \"index\".");
144150
}
145151

146152
[Test]
@@ -160,6 +166,8 @@ public void WithArea_GivenExpected_ShouldPass()
160166
[Test]
161167
public void WithArea_GivenUnexpected_ShouldFail()
162168
{
169+
var subjectIdentifier = GetSubjectIdentifier();
170+
163171
ActionResult result = new RedirectToRouteResult("", new RouteValueDictionary(
164172
new
165173
{
@@ -170,7 +178,29 @@ public void WithArea_GivenUnexpected_ShouldFail()
170178
.BeRedirectToRouteResult()
171179
.WithArea("xyz");
172180
a.Should().Throw<Exception>()
173-
.WithMessage("Expected Subject.RouteValues to contain value \"xyz\" at key \"Area\", but found \"accounts\".");
181+
.WithMessage($"Expected {subjectIdentifier} to contain value \"xyz\" at key \"Area\", but found \"accounts\".");
182+
}
183+
184+
/// <summary>
185+
/// Gets the expected subject identifier for the failure message
186+
/// </summary>
187+
/// <remarks>
188+
/// The Fluent Assertions library will attempt to determine the name of the subject from the stack trace.
189+
/// This requires the Unit Tests to be compiled in DEBUG mode in order for it to work successfully.
190+
/// If it cannot determne the Subject's Identity, it will fall back to a generic value.
191+
/// This method is an attempt to cope with the different build configurations
192+
/// ref: http://fluentassertions.com/documentation.html#subject-identification
193+
/// </remarks>
194+
/// <returns></returns>
195+
private static string GetSubjectIdentifier()
196+
{
197+
var subjectIdentifier = "dictionary";
198+
#if DEBUG
199+
subjectIdentifier = "Subject.RouteValues";
200+
#endif
201+
return subjectIdentifier;
174202
}
203+
204+
175205
}
176206
}

0 commit comments

Comments
 (0)