Skip to content

Commit 84d708c

Browse files
committed
Added assertion chain for .ActionResult<TResult>(result => result.BadRequest()) (#359)
1 parent b5af60e commit 84d708c

4 files changed

Lines changed: 189 additions & 7 deletions

File tree

src/MyTested.AspNetCore.Mvc.Controllers/Builders/Actions/ShouldReturn/ShouldReturnActionResultTestBuilder.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using Utilities.Validators;
1010

1111
/// <summary>
12-
/// Used for testing returned <see cref="Microsoft.AspNetCore.Mvc.ActionResult"/>.
12+
/// Used for testing returned <see cref="ActionResult"/>.
1313
/// </summary>
1414
/// <typeparam name="TActionResult">Result from invoked action in ASP.NET Core MVC controller.</typeparam>
1515
public class ShouldReturnActionResultTestBuilder<TActionResult>
@@ -37,7 +37,8 @@ IAndTestBuilder IShouldReturnActionResultTestBuilder<TActionResult>.ActionResult
3737
}
3838

3939
/// <inheritdoc />
40-
IAndTestBuilder IShouldReturnActionResultTestBuilder<TActionResult>.ActionResult(Action<IShouldReturnTestBuilder<TActionResult>> actionResultTestBuilder)
40+
IAndTestBuilder IShouldReturnActionResultTestBuilder<TActionResult>.ActionResult(
41+
Action<IShouldReturnTestBuilder<TActionResult>> actionResultTestBuilder)
4142
{
4243
this.ValidateActionResults();
4344

@@ -48,9 +49,17 @@ IAndTestBuilder IShouldReturnActionResultTestBuilder<TActionResult>.ActionResult
4849

4950
IAndTestBuilder IShouldReturnActionResultTestBuilder<TActionResult>.ActionResult<TResult>()
5051
{
51-
InvocationResultValidator.ValidateInvocationResultType<ActionResult<TResult>>(
52-
this.TestContext,
53-
typeOfActualReturnValue: this.TestContext.Method.ReturnType);
52+
this.ValidateActionResult<TResult>();
53+
54+
return new AndTestBuilder(this.TestContext);
55+
}
56+
57+
IAndTestBuilder IShouldReturnActionResultTestBuilder<TActionResult>.ActionResult<TResult>(
58+
Action<IShouldReturnTestBuilder<TActionResult>> actionResultTestBuilder)
59+
{
60+
this.ValidateActionResult<TResult>();
61+
62+
actionResultTestBuilder?.Invoke(this);
5463

5564
return new AndTestBuilder(this.TestContext);
5665
}
@@ -60,5 +69,10 @@ private void ValidateActionResults()
6069
this.TestContext,
6170
canBeAssignable: true,
6271
typesOfExpectedReturnValue: new[] { ActionResultType, GenericActionResultType });
72+
73+
private void ValidateActionResult<TResult>()
74+
=> InvocationResultValidator.ValidateInvocationResultType<ActionResult<TResult>>(
75+
this.TestContext,
76+
typeOfActualReturnValue: this.TestContext.Method.ReturnType);
6377
}
6478
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ public interface IShouldReturnActionResultTestBuilder<TActionResult>
3535
/// <returns>Test builder of <see cref="IAndTestBuilder"/> type.</returns>
3636
IAndTestBuilder ActionResult<TResult>();
3737

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);
38+
/// <summary>
39+
/// Tests whether the action result is
40+
/// <see cref="Microsoft.AspNetCore.Mvc.ActionResult{TResult}"/>.
41+
/// </summary>
42+
/// <typeparam name="TResult">Type of the expected result.</typeparam>
43+
/// <param name="actionResultTestBuilder">Test builder which asserts the actual action result.</param>
44+
/// <returns>Test builder of <see cref="IAndTestBuilder"/> type.</returns>
45+
IAndTestBuilder ActionResult<TResult>(Action<IShouldReturnTestBuilder<TActionResult>> actionResultTestBuilder);
4046

4147
//// ActionResult<TResult>, consider OkResult for example to be valid too?!, with additional options to validate the TResult, like EqualTo or Passing (model/result details)
4248
//IAndTestBuilder ActionResult<TResult>(Action<IShouldHaveTestBuilder<TActionResult>> actionResultTestBuilder);

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

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ public void ShouldReturnActionResultWithDetailsShouldNotThrowExceptionWhenResult
8989
.Passing(ok => ok.Value?.GetType() == typeof(ResponseModel))));
9090
}
9191

92+
[Fact]
93+
public void ShouldReturnActionResultWithDetailsAndInnerBuilderShouldThrowExceptionWhenWhenResultIsNotActionResult()
94+
{
95+
Test.AssertException<InvocationResultAssertionException>(
96+
() =>
97+
{
98+
MyController<MvcController>
99+
.Instance()
100+
.Calling(c => c.AnonymousResult())
101+
.ShouldReturn()
102+
.ActionResult(result => result
103+
.Ok());
104+
},
105+
"When calling AnonymousResult action in MvcController expected result to be IActionResult or ActionResult<TValue>, but instead received AnonymousType<Int32, String, AnonymousType<Boolean>>.");
106+
}
107+
92108
[Fact]
93109
public void ShouldReturnActionResultWithDetailsShouldThrowExceptionWhenWhenResultIsNotActionResult()
94110
{
@@ -169,5 +185,76 @@ public void ShouldReturnActionResultOfTShouldThrowExceptionWhenResultIsActionRes
169185
},
170186
"When calling ActionResultOfT action in MvcController expected result to be ActionResult<RequestModel>, but instead received ActionResult<ResponseModel>.");
171187
}
188+
189+
[Fact]
190+
public void ShouldReturnActionResultOfTWithDetailsShouldNotThrowExceptionWhenResultIsActionResultOfTWithActionResult()
191+
{
192+
MyController<MvcController>
193+
.Instance()
194+
.Calling(c => c.ActionResultOfT(0))
195+
.ShouldReturn()
196+
.ActionResult<ResponseModel>(result => result
197+
.BadRequest());
198+
}
199+
200+
[Fact]
201+
public void ShouldReturnActionResultOfTWithDetailsShouldNotThrowExceptionWhenResultIsActionResultOfTWithModel()
202+
{
203+
MyController<MvcController>
204+
.Instance()
205+
.Calling(c => c.ActionResultOfT(int.MaxValue))
206+
.ShouldReturn()
207+
.ActionResult<ResponseModel>(result => result
208+
.Ok(okResult => okResult
209+
.Passing(ok => ok.Value?.GetType() == typeof(ResponseModel))));
210+
}
211+
212+
[Fact]
213+
public void ShouldReturnActionResultOfTWithDetailsShouldThrowExceptionWhenResultIsIActionResultInterface()
214+
{
215+
Test.AssertException<InvocationResultAssertionException>(
216+
() =>
217+
{
218+
MyController<MvcController>
219+
.Instance()
220+
.Calling(c => c.ActionResultInterface())
221+
.ShouldReturn()
222+
.ActionResult<ResponseModel>(result => result
223+
.Ok());
224+
},
225+
"When calling ActionResultInterface action in MvcController expected result to be ActionResult<ResponseModel>, but instead received IActionResult.");
226+
}
227+
228+
[Fact]
229+
public void ShouldReturnActionResultOfTWithDetailsShouldThrowExceptionWhenResultIsActionResultBaseClass()
230+
{
231+
Test.AssertException<InvocationResultAssertionException>(
232+
() =>
233+
{
234+
MyController<MvcController>
235+
.Instance()
236+
.Calling(c => c.ActionResultBaseClass())
237+
.ShouldReturn()
238+
.ActionResult<ResponseModel>(result => result
239+
.Ok());
240+
},
241+
"When calling ActionResultBaseClass action in MvcController expected result to be ActionResult<ResponseModel>, but instead received ActionResult.");
242+
}
243+
244+
[Fact]
245+
public void ShouldReturnActionResultOfTWithDetailsShouldThrowExceptionWhenResultIsActionResultOfWrongModel()
246+
{
247+
Test.AssertException<InvocationResultAssertionException>(
248+
() =>
249+
{
250+
MyController<MvcController>
251+
.Instance()
252+
.Calling(c => c.ActionResultOfT(0))
253+
.ShouldReturn()
254+
.ActionResult<RequestModel>(result => result
255+
.Ok());
256+
},
257+
"When calling ActionResultOfT action in MvcController expected result to be ActionResult<RequestModel>, but instead received ActionResult<ResponseModel>.");
258+
}
172259
}
173260
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
namespace MyTested.AspNetCore.Mvc.Test.BuildersTests.ActionsTests.ShouldReturnTests
2+
{
3+
using Exceptions;
4+
using Setups;
5+
using Setups.Controllers;
6+
using Setups.Models;
7+
using Xunit;
8+
9+
public class ShouldReturnActionResultTests
10+
{
11+
[Fact]
12+
public void ShouldReturnActionResultOfTWithDetailsShouldNotThrowExceptionWhenResultIsActionResultOfTModelResultOfType()
13+
{
14+
MyController<MvcController>
15+
.Instance()
16+
.Calling(c => c.ActionResultOfT(1))
17+
.ShouldReturn()
18+
.ActionResult<ResponseModel>(action => action
19+
.ResultOfType<ResponseModel>(result => result
20+
.Passing(model => model.IntegerValue == 1)));
21+
}
22+
23+
[Fact]
24+
public void ShouldReturnActionResultOfTWithDetailsShouldNotThrowExceptionWhenResultIsActionResultOfTModelResult()
25+
{
26+
MyController<MvcController>
27+
.Instance()
28+
.Calling(c => c.ActionResultOfT(1))
29+
.ShouldReturn()
30+
.ActionResult<ResponseModel>(action => action
31+
.Result(new ResponseModel
32+
{
33+
IntegerValue = 1,
34+
StringValue = "Test"
35+
}));
36+
}
37+
38+
[Fact]
39+
public void ShouldReturnActionResultOfTWithDetailsShouldThrowExceptionWhenResultIsActionResultOfTWithWrongModelResultOfType()
40+
{
41+
Test.AssertException<ResponseModelAssertionException>(
42+
() =>
43+
{
44+
MyController<MvcController>
45+
.Instance()
46+
.Calling(c => c.ActionResultOfT(1))
47+
.ShouldReturn()
48+
.ActionResult<ResponseModel>(action => action
49+
.ResultOfType<ResponseModel>(result => result
50+
.Passing(model => model.IntegerValue == 2)));
51+
},
52+
"When calling ActionResultOfT action in MvcController expected response model ResponseModel to pass the given predicate, but it failed.");
53+
}
54+
55+
[Fact]
56+
public void ShouldReturnActionResultOfTWithDetailsShouldThrowExceptionWhenResultIsActionResultOfTWithWrongModelResult()
57+
{
58+
Test.AssertException<ResponseModelAssertionException>(
59+
() =>
60+
{
61+
MyController<MvcController>
62+
.Instance()
63+
.Calling(c => c.ActionResultOfT(1))
64+
.ShouldReturn()
65+
.ActionResult<ResponseModel>(action => action
66+
.Result(new ResponseModel
67+
{
68+
IntegerValue = 2,
69+
StringValue = "Test"
70+
}));
71+
},
72+
"When calling ActionResultOfT action in MvcController expected the response model to be the given model, but in fact it was a different one. Difference occurs at 'ResponseModel.IntegerValue'. Expected a value of '2', but in fact it was '1'.");
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)