Skip to content

Commit 208e0f0

Browse files
committed
Added tests for the .ShouldReturn().ActionResult() assertion chain (#359)
1 parent f62c150 commit 208e0f0

3 files changed

Lines changed: 69 additions & 4 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.ActionsTests.ShouldReturnTests
2+
{
3+
using Exceptions;
4+
using Setups;
5+
using Setups.Controllers;
6+
using Xunit;
7+
8+
public class ShouldReturnActionResultTests
9+
{
10+
[Fact]
11+
public void ShouldReturnActionResultShouldNotThrowExceptionWhenResultIsIActionResultInterface()
12+
{
13+
MyController<MvcController>
14+
.Instance()
15+
.Calling(c => c.ActionResultInterface())
16+
.ShouldReturn()
17+
.ActionResult();
18+
}
19+
20+
[Fact]
21+
public void ShouldReturnActionResultShouldNotThrowExceptionWhenResultIsIActionResultBaseClass()
22+
{
23+
MyController<MvcController>
24+
.Instance()
25+
.Calling(c => c.ActionResultBaseClass())
26+
.ShouldReturn()
27+
.ActionResult();
28+
}
29+
30+
[Fact]
31+
public void ShouldReturnActionResultShouldNotThrowExceptionWhenResultIsActionResultOfT()
32+
{
33+
MyController<MvcController>
34+
.Instance()
35+
.Calling(c => c.ActionResultOfAnonymousType())
36+
.ShouldReturn()
37+
.ActionResult();
38+
}
39+
40+
[Fact]
41+
public void ShouldReturnActionResultShouldThrowExceptionWhenWhenResultIsNotActionResult()
42+
{
43+
Test.AssertException<InvocationResultAssertionException>(
44+
() =>
45+
{
46+
MyController<MvcController>
47+
.Instance()
48+
.Calling(c => c.AnonymousResult())
49+
.ShouldReturn()
50+
.ActionResult();
51+
},
52+
"When calling AnonymousResult action in MvcController expected result to be IActionResult or ActionResult<TValue>, but instead received AnonymousType<Int32, String, AnonymousType<Boolean>>.");
53+
}
54+
}
55+
}

test/MyTested.AspNetCore.Mvc.Controllers.Test/BuildersTests/ActionsTests/ShouldReturnTests/ShouldReturnBadRequestTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public class ShouldReturnBadRequestTests
99
{
1010
[Fact]
11-
public void ShouldReturnBadRequestShouldNotThrowExceptionWhenResultIsHttpBadRequest()
11+
public void ShouldReturnBadRequestShouldNotThrowExceptionWhenResultIsBadRequest()
1212
{
1313
MyController<MvcController>
1414
.Instance()
@@ -28,7 +28,7 @@ public void ShouldReturnBadRequestShouldNotThrowExceptionWhenResultIsBadRequestE
2828
}
2929

3030
[Fact]
31-
public void ShouldReturnNotFoundShouldThrowExceptionWhenActionDoesNotReturnNotFound()
31+
public void ShouldReturnBadRequestShouldThrowExceptionWhenActionDoesNotReturnBadRequest()
3232
{
3333
Test.AssertException<InvocationResultAssertionException>(
3434
() =>

test/MyTested.AspNetCore.Mvc.Test.Setups/Controllers/MvcController.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ public IActionResult SignOutWithAuthenticationProperties()
505505
return this.SignOut(TestObjectFactory.GetAuthenticationProperties());
506506
}
507507

508-
public FileResult FileWithVirtualPath()
508+
public IActionResult FileWithVirtualPath()
509509
{
510510
return this.File("/Test", ContentType.ApplicationJson, "FileDownloadName");
511511
}
@@ -702,7 +702,7 @@ public IActionResult ActionWithAggregateException()
702702

703703
public async Task EmptyActionWithExceptionAsync()
704704
{
705-
await Task.Run(() => this.ThrowNewNullReferenceException());
705+
await Task.Run(this.ThrowNewNullReferenceException);
706706
}
707707

708708
public async Task<IActionResult> ActionWithExceptionAsync()
@@ -1247,6 +1247,16 @@ public IActionResult WithService(IHttpContextAccessor httpContextAccessor)
12471247
return this.Ok();
12481248
}
12491249

1250+
public IActionResult ActionResultInterface()
1251+
{
1252+
return this.Ok();
1253+
}
1254+
1255+
public ActionResult ActionResultBaseClass()
1256+
{
1257+
return this.Ok();
1258+
}
1259+
12501260
public ActionResult<ResponseModel> ActionResultOfT(int id)
12511261
{
12521262
if (id == 0)

0 commit comments

Comments
 (0)