Skip to content

Commit a403a7f

Browse files
committed
Introduced IShouldReturnActionResultTestBuilder (#359)
1 parent bf3e1bb commit a403a7f

4 files changed

Lines changed: 70 additions & 1 deletion

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Actions.ShouldReturn
2+
{
3+
class ShouldReturnActionResult
4+
{
5+
}
6+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Actions
2+
{
3+
using System;
4+
using And;
5+
6+
public interface IShouldReturnActionResultTestBuilder<TActionResult>
7+
: IShouldReturnTestBuilder<TActionResult>
8+
{
9+
// IActionResult, ActionResult or ActionResult<T>
10+
IAndTestBuilder ActionResult();
11+
12+
//// IActionResult, ActionResult or ActionResult<T> with options to specify the result - Ok() for example
13+
//IAndTestBuilder ActionResult(Action<IShouldReturnTestBuilder<TActionResult>> actionResultTestBuilder);
14+
15+
//// ActionResult<TResult>, consider OkResult for example to be valid too?
16+
//IAndTestBuilder ActionResult<TResult>();
17+
18+
//// ActionResult<TResult>, consider OkResult for example to be valid too? with additional options to specify the result - Ok() for example
19+
//IAndTestBuilder ActionResult<TResult>(Action<IShouldReturnTestBuilder<TActionResult>> actionResultTestBuilder);
20+
21+
//// ActionResult<TResult>, consider OkResult for example to be valid too?!, with additional options to validate the TResult, like EqualTo or Passing (model/result details)
22+
//IAndTestBuilder ActionResult<TResult>(Action<IShouldHaveTestBuilder<TActionResult>> actionResultTestBuilder);
23+
}
24+
}

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Contracts/Actions/IShouldReturnTestBuilder.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@
2626
public interface IShouldReturnTestBuilder<TActionResult>
2727
: IBaseShouldReturnTestBuilder<TActionResult, IAndTestBuilder>
2828
{
29+
// IActionResult, ActionResult or ActionResult<T>
30+
IAndTestBuilder ActionResult();
31+
32+
// IActionResult, ActionResult or ActionResult<T> with options to specify the result - Ok() for example
33+
IAndTestBuilder ActionResult(Action<IShouldReturnTestBuilder<TActionResult>> actionResultTestBuilder);
34+
35+
//// ActionResult<TResult>, consider OkResult for example to be valid too?
36+
//IAndTestBuilder ActionResult<TResult>();
37+
38+
//// ActionResult<TResult>, consider OkResult for example to be valid too? with additional options to specify the result - Ok() for example
39+
//IAndTestBuilder ActionResult<TResult>(Action<IShouldReturnTestBuilder<TActionResult>> actionResultTestBuilder);
40+
41+
//// ActionResult<TResult>, consider OkResult for example to be valid too?!, with additional options to validate the TResult, like EqualTo or Passing (model/result details)
42+
//IAndTestBuilder ActionResult<TResult>(Action<IShouldHaveTestBuilder<TActionResult>> actionResultTestBuilder);
43+
2944
/// <summary>
3045
/// Tests whether the action result is <see cref="Microsoft.AspNetCore.Mvc.AcceptedResult"/>,
3146
/// <see cref="Microsoft.AspNetCore.Mvc.AcceptedAtActionResult"/> or

test/MyTested.AspNetCore.Mvc.Controllers.ActionResults.Test/BuildersTests/ActionResultsTests/AcceptedTests/AcceptedTestBuilderTests.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,34 @@
44
using Setups;
55
using Setups.Controllers;
66
using Exceptions;
7-
using Utilities;
87

98
public class AcceptedTestBuilderTests
109
{
10+
[Fact]
11+
public void AcceptedShouldNotThrowExceptionWithCorrectActionResult()
12+
{
13+
MyController<MvcController>
14+
.Instance()
15+
.Calling(c => c.AcceptedAction())
16+
.ShouldReturn()
17+
.Accepted();
18+
}
19+
20+
[Fact]
21+
public void AcceptedShouldThrowExceptionWithIncorrectActionResult()
22+
{
23+
Test.AssertException<InvocationResultAssertionException>(
24+
() =>
25+
{
26+
MyController<MvcController>
27+
.Instance()
28+
.Calling(c => c.OkResultAction())
29+
.ShouldReturn()
30+
.Accepted();
31+
},
32+
"When calling OkResultAction action in MvcController expected result to be AcceptedResult, but instead received OkResult.");
33+
}
34+
1135
[Fact]
1236
public void WithStatusCodeShouldNotThrowExceptionWithCorrectStatusCode()
1337
{

0 commit comments

Comments
 (0)