File tree 2 files changed +34
-2
lines changed
QbQuestionsAPI/Controllers
QbQuestionsAPI.UnitTests/ControllerTests
2 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -66,6 +66,21 @@ public async Task GetRandomAsyncSuccessTest()
66
66
result . Should ( ) . BeOfType < OkObjectResult > ( ) ;
67
67
}
68
68
69
+ [ Fact ]
70
+ public async Task GetRandomAsyncBadRequestFailureTest ( )
71
+ {
72
+ // Arrange
73
+ const string invalidLevel = "invalid" ;
74
+ IQbQuestionService qbQuestionService = Substitute . For < IQbQuestionService > ( ) ;
75
+ QbQuestionsController controller = new QbQuestionsController ( qbQuestionService , mapper ) ;
76
+
77
+ // Act
78
+ IActionResult result = await controller . GetRandomAsync ( invalidLevel ) ;
79
+
80
+ // Assert
81
+ result . Should ( ) . BeOfType < BadRequestResult > ( ) ;
82
+ }
83
+
69
84
[ Fact ]
70
85
public async Task GetRandomAsyncFailureTest ( )
71
86
{
Original file line number Diff line number Diff line change 1
1
using AutoMapper ;
2
2
using Microsoft . AspNetCore . Authorization ;
3
3
using Microsoft . AspNetCore . Mvc ;
4
+ using System ;
4
5
using System . Collections . Generic ;
5
6
using System . Threading . Tasks ;
6
7
@@ -41,9 +42,25 @@ public async Task<IActionResult> GetAsync(int id)
41
42
}
42
43
43
44
[ HttpGet ( "random" ) ]
44
- public async Task < IActionResult > GetRandomAsync ( int ? level = null )
45
+ public async Task < IActionResult > GetRandomAsync ( string level = null )
45
46
{
46
- QbQuestion question = await _qbQuestionService . GetRandomAsync ( level ) ;
47
+ int ? levelInt = null ;
48
+
49
+ if ( ! String . IsNullOrEmpty ( level ) )
50
+ {
51
+ Enum . TryParse ( level , true , out ETournamentLevel levelEnum ) ;
52
+ // 0 is default enum value, which is returned if TryParse fails
53
+ if ( levelEnum != 0 )
54
+ {
55
+ levelInt = ( int ? ) levelEnum ;
56
+ }
57
+ else
58
+ {
59
+ return BadRequest ( ) ;
60
+ }
61
+ }
62
+
63
+ QbQuestion question = await _qbQuestionService . GetRandomAsync ( levelInt ) ;
47
64
48
65
if ( question == null )
49
66
{
You can’t perform that action at this time.
0 commit comments