Skip to content

Commit 5bd7e8c

Browse files
authored
Merge pull request #308 - Added Unit Tests for ActionResults Builders
Added Unit Tests for ActionResults Builders
2 parents d151ad9 + 8ae342a commit 5bd7e8c

File tree

14 files changed

+934
-0
lines changed

14 files changed

+934
-0
lines changed

test/MyTested.AspNetCore.Mvc.Controllers.ActionResults.Test/BuildersTests/ActionResultsTests/BadRequestTests/BadRequestTestBuilderTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Setups;
99
using Setups.Common;
1010
using Setups.Controllers;
11+
using Utilities;
1112
using Xunit;
1213

1314
public class BadRequestTestBuilderTests
@@ -507,5 +508,56 @@ public void AndAlsoShouldWorkCorrectly()
507508
TestObjectFactory.GetOutputFormatter(),
508509
new CustomOutputFormatter()));
509510
}
511+
512+
[Fact]
513+
public void PassingShouldCorrectlyRunItsAssertionFunction()
514+
{
515+
MyController<MvcController>
516+
.Instance()
517+
.Calling(c => c.FullHttpBadRequestAction())
518+
.ShouldReturn()
519+
.BadRequest(badRequest => badRequest
520+
.Passing(br => br.Formatters?.Count == 2));
521+
}
522+
523+
[Fact]
524+
public void PassingShouldThrowAnExceptionOnAnIncorrectAssertion()
525+
{
526+
Test.AssertException<InvocationResultAssertionException>(
527+
() =>
528+
{
529+
MyController<MvcController>
530+
.Instance()
531+
.Calling(c => c.FullHttpBadRequestAction())
532+
.ShouldReturn()
533+
.BadRequest(badRequest => badRequest
534+
.Passing(br => br.Formatters?.Count == 0));
535+
},
536+
$"When calling FullHttpBadRequestAction action in MvcController expected the BadRequestObjectResult to pass the given predicate, but it failed.");
537+
}
538+
539+
[Fact]
540+
public void PassingShouldCorrectlyRunItsAssertionAction()
541+
{
542+
MyController<MvcController>
543+
.Instance()
544+
.Calling(c => c.FullHttpBadRequestAction())
545+
.ShouldReturn()
546+
.BadRequest(badRequest => badRequest
547+
.Passing(br =>
548+
{
549+
const int expectedFormattersCount = 2;
550+
var actualFormattersCount = br.Formatters?.Count;
551+
if (actualFormattersCount != expectedFormattersCount)
552+
{
553+
throw new InvalidAssertionException(
554+
string.Format("Expected {0} to have {1} {2}, but it has {3}.",
555+
br.GetType().ToFriendlyTypeName(),
556+
expectedFormattersCount,
557+
nameof(br.Formatters),
558+
actualFormattersCount));
559+
};
560+
}));
561+
}
510562
}
511563
}

test/MyTested.AspNetCore.Mvc.Controllers.ActionResults.Test/BuildersTests/ActionResultsTests/ChallengeTests/ChallengeTestBuilderTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Exceptions;
55
using Setups;
66
using Setups.Controllers;
7+
using Utilities;
78
using Xunit;
89

910
public class ChallengeTestBuilderTests
@@ -140,5 +141,57 @@ public void AndAlsoShouldWorkCorrectly()
140141
.AndAlso()
141142
.ContainingAuthenticationScheme(AuthenticationScheme.NTLM));
142143
}
144+
145+
146+
[Fact]
147+
public void PassingShouldCorrectlyRunItsAssertionFunction()
148+
{
149+
MyController<MvcController>
150+
.Instance()
151+
.Calling(c => c.ChallengeWithAuthenticationSchemes())
152+
.ShouldReturn()
153+
.Challenge(challenge => challenge
154+
.Passing(c => c.AuthenticationSchemes?.Count == 2));
155+
}
156+
157+
[Fact]
158+
public void PassingShouldThrowAnExceptionOnAnIncorrectAssertion()
159+
{
160+
Test.AssertException<InvocationResultAssertionException>(
161+
() =>
162+
{
163+
MyController<MvcController>
164+
.Instance()
165+
.Calling(c => c.ChallengeWithAuthenticationSchemes())
166+
.ShouldReturn()
167+
.Challenge(challenge => challenge
168+
.Passing(c => c.AuthenticationSchemes?.Count == 0));
169+
},
170+
$"When calling ChallengeWithAuthenticationSchemes action in MvcController expected the ChallengeResult to pass the given predicate, but it failed.");
171+
}
172+
173+
[Fact]
174+
public void PassingShouldCorrectlyRunItsAssertionAction()
175+
{
176+
MyController<MvcController>
177+
.Instance()
178+
.Calling(c => c.ChallengeWithAuthenticationSchemes())
179+
.ShouldReturn()
180+
.Challenge(challenge => challenge
181+
.Passing(c =>
182+
{
183+
const int expectedAuthSchemesCount = 2;
184+
var actualAuthSchemesCount = c.AuthenticationSchemes?.Count;
185+
if (actualAuthSchemesCount != expectedAuthSchemesCount)
186+
{
187+
throw new InvalidAssertionException(
188+
string.Format("Expected {0} to have {1} {2}, but it has {3}.",
189+
c.GetType().ToFriendlyTypeName(),
190+
expectedAuthSchemesCount,
191+
nameof(c.AuthenticationSchemes),
192+
actualAuthSchemesCount));
193+
};
194+
}));
195+
}
143196
}
144197
}

test/MyTested.AspNetCore.Mvc.Controllers.ActionResults.Test/BuildersTests/ActionResultsTests/ContentTests/ContentTestBuilderTests.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.Net.Http.Headers;
66
using Setups;
77
using Setups.Controllers;
8+
using Utilities;
89
using Xunit;
910

1011
public class ContentTestBuilderTests
@@ -156,5 +157,58 @@ public void AndAlsoShouldWorkCorrectly()
156157
.AndAlso()
157158
.WithContentType(ContentType.ApplicationJson));
158159
}
160+
161+
162+
[Fact]
163+
public void PassingShouldCorrectlyRunItsAssertionFunction()
164+
{
165+
MyController<MvcController>
166+
.Instance()
167+
.Calling(c => c.ContentAction())
168+
.ShouldReturn()
169+
.Content(content => content
170+
.Passing(c => c.Content == "content"));
171+
}
172+
173+
[Fact]
174+
public void PassingShouldThrowAnExceptionOnAnIncorrectAssertion()
175+
{
176+
Test.AssertException<InvocationResultAssertionException>(
177+
() =>
178+
{
179+
MyController<MvcController>
180+
.Instance()
181+
.Calling(c => c.ContentAction())
182+
.ShouldReturn()
183+
.Content(content => content
184+
.Passing(c => c.Content == string.Empty));
185+
},
186+
$"When calling ContentAction action in MvcController expected the ContentResult to pass the given predicate, but it failed.");
187+
}
188+
189+
[Fact]
190+
public void PassingShouldCorrectlyRunItsAssertionAction()
191+
{
192+
MyController<MvcController>
193+
.Instance()
194+
.Calling(c => c.ContentAction())
195+
.ShouldReturn()
196+
.Content(content => content
197+
.Passing(c =>
198+
{
199+
const string expectedContentBody = "content";
200+
var actualContentBody = c.Content;
201+
if (actualContentBody != expectedContentBody)
202+
{
203+
throw new InvalidAssertionException(
204+
string.Format("Expected {0} to have {1} equal to content, but it was {2}.",
205+
c.GetType().ToFriendlyTypeName(),
206+
nameof(c.Content),
207+
actualContentBody));
208+
};
209+
}));
210+
}
211+
212+
159213
}
160214
}

test/MyTested.AspNetCore.Mvc.Controllers.ActionResults.Test/BuildersTests/ActionResultsTests/FileTests/FileTestBuilderTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.Net.Http.Headers;
66
using Setups;
77
using Setups.Controllers;
8+
using Utilities;
89
using Xunit;
910

1011
public class FileTestBuilderTests
@@ -199,5 +200,57 @@ public void AndAlsoShouldWorkCorrectly()
199200
.AndAlso()
200201
.WithContentType(ContentType.ApplicationJson));
201202
}
203+
204+
205+
[Fact]
206+
public void PassingShouldCorrectlyRunItsAssertionFunction()
207+
{
208+
MyController<MvcController>
209+
.Instance()
210+
.Calling(c => c.FileWithVirtualPath())
211+
.ShouldReturn()
212+
.File(file => file
213+
.Passing(f => f.FileName == "/Test"));
214+
}
215+
216+
[Fact]
217+
public void PassingShouldThrowAnExceptionOnAnIncorrectAssertion()
218+
{
219+
Test.AssertException<InvocationResultAssertionException>(
220+
() =>
221+
{
222+
MyController<MvcController>
223+
.Instance()
224+
.Calling(c => c.FileWithVirtualPath())
225+
.ShouldReturn()
226+
.File(file => file
227+
.Passing(f => f.FileName == string.Empty));
228+
},
229+
$"When calling FileWithVirtualPath action in MvcController expected the VirtualFileResult to pass the given predicate, but it failed.");
230+
}
231+
232+
[Fact]
233+
public void PassingShouldCorrectlyRunItsAssertionAction()
234+
{
235+
MyController<MvcController>
236+
.Instance()
237+
.Calling(c => c.FileWithVirtualPath())
238+
.ShouldReturn()
239+
.File(file => file
240+
.Passing(f =>
241+
{
242+
const string expectedFileName = "/Test";
243+
var actualFileName = f.FileName;
244+
if (actualFileName != expectedFileName)
245+
{
246+
throw new InvalidAssertionException(
247+
string.Format("Expected {0} to have {1} equal to {2}, but it was {3}.",
248+
f.GetType().ToFriendlyTypeName(),
249+
nameof(f.FileName),
250+
expectedFileName,
251+
actualFileName));
252+
};
253+
}));
254+
}
202255
}
203256
}

test/MyTested.AspNetCore.Mvc.Controllers.ActionResults.Test/BuildersTests/ActionResultsTests/ForbidTests/ForbidTestBuilderTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Exceptions;
55
using Setups;
66
using Setups.Controllers;
7+
using Utilities;
78
using Xunit;
89

910
public class ForbidTestBuilderTests
@@ -136,5 +137,56 @@ public void AndAlsoShouldWorkCorrectly()
136137
.AndAlso()
137138
.ContainingAuthenticationScheme(AuthenticationScheme.NTLM));
138139
}
140+
141+
[Fact]
142+
public void PassingShouldCorrectlyRunItsAssertionFunction()
143+
{
144+
MyController<MvcController>
145+
.Instance()
146+
.Calling(c => c.ForbidWithAuthenticationSchemes())
147+
.ShouldReturn()
148+
.Forbid(forbid => forbid
149+
.Passing(f => f.AuthenticationSchemes?.Count == 2));
150+
}
151+
152+
[Fact]
153+
public void PassingShouldThrowAnExceptionOnAnIncorrectAssertion()
154+
{
155+
Test.AssertException<InvocationResultAssertionException>(
156+
() =>
157+
{
158+
MyController<MvcController>
159+
.Instance()
160+
.Calling(c => c.ForbidWithAuthenticationSchemes())
161+
.ShouldReturn()
162+
.Forbid(forbid => forbid
163+
.Passing(f => f.AuthenticationSchemes?.Count == 0));
164+
},
165+
$"When calling ForbidWithAuthenticationSchemes action in MvcController expected the ForbidResult to pass the given predicate, but it failed.");
166+
}
167+
168+
[Fact]
169+
public void PassingShouldCorrectlyRunItsAssertionAction()
170+
{
171+
MyController<MvcController>
172+
.Instance()
173+
.Calling(c => c.ForbidWithAuthenticationSchemes())
174+
.ShouldReturn()
175+
.Forbid(forbid => forbid
176+
.Passing(f =>
177+
{
178+
const int expectedAuthSchemesCount = 2;
179+
var actualAuthSchemesCount = f.AuthenticationSchemes?.Count;
180+
if (actualAuthSchemesCount != expectedAuthSchemesCount)
181+
{
182+
throw new InvalidAssertionException(
183+
string.Format("Expected {0} to have {1} {2}, but it was {3}.",
184+
f.GetType().ToFriendlyTypeName(),
185+
expectedAuthSchemesCount,
186+
nameof(f.AuthenticationSchemes),
187+
actualAuthSchemesCount));
188+
};
189+
}));
190+
}
139191
}
140192
}

test/MyTested.AspNetCore.Mvc.Controllers.ActionResults.Test/BuildersTests/ActionResultsTests/LocalRedirectTests/LocalRedirectTestBuilderTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Setups;
77
using Setups.Common;
88
using Setups.Controllers;
9+
using Utilities;
910
using Xunit;
1011

1112
public class LocalRedirectTestBuilderTests
@@ -251,5 +252,57 @@ public void AndAlsoShouldWorkCorrectly()
251252
.AndAlso()
252253
.ToUrl("/local/test"));
253254
}
255+
256+
257+
[Fact]
258+
public void PassingShouldCorrectlyRunItsAssertionFunction()
259+
{
260+
MyController<MvcController>
261+
.Instance()
262+
.Calling(c => c.LocalRedirectAction())
263+
.ShouldReturn()
264+
.LocalRedirect(localRedirect => localRedirect
265+
.Passing(lr => lr.Url == "/local/test"));
266+
}
267+
268+
[Fact]
269+
public void PassingShouldThrowAnExceptionOnAnIncorrectAssertion()
270+
{
271+
Test.AssertException<InvocationResultAssertionException>(
272+
() =>
273+
{
274+
MyController<MvcController>
275+
.Instance()
276+
.Calling(c => c.LocalRedirectAction())
277+
.ShouldReturn()
278+
.LocalRedirect(localRedirect => localRedirect
279+
.Passing(lr => lr.Url == string.Empty));
280+
},
281+
$"When calling LocalRedirectAction action in MvcController expected the LocalRedirectResult to pass the given predicate, but it failed.");
282+
}
283+
284+
[Fact]
285+
public void PassingShouldCorrectlyRunItsAssertionAction()
286+
{
287+
MyController<MvcController>
288+
.Instance()
289+
.Calling(c => c.LocalRedirectAction())
290+
.ShouldReturn()
291+
.LocalRedirect(localRedirect => localRedirect
292+
.Passing(lr =>
293+
{
294+
const string expectedRedirect = "/local/test";
295+
var actualRedirect = lr.Url;
296+
if (expectedRedirect != actualRedirect)
297+
{
298+
throw new InvalidAssertionException(
299+
string.Format("Expected {0} to have {1} equal to {2}, but it was {3}.",
300+
lr.GetType().ToFriendlyTypeName(),
301+
nameof(lr.Url),
302+
expectedRedirect,
303+
actualRedirect));
304+
};
305+
}));
306+
}
254307
}
255308
}

0 commit comments

Comments
 (0)