1+ using Moq ;
2+ using Microsoft . AspNetCore . Mvc ;
3+ using tdd_architecture_template_dotnet . Controllers . V1 . Login ;
4+ using tdd_architecture_template_dotnet . Application . Services . Login . Interfaces ;
5+ using tdd_architecture_template_dotnet . Domain . Enums ;
6+ using tdd_architecture_template_dotnet . Application . Models . Http ;
7+
8+ namespace tdd_architecture_template_dotnet . Tests . Controllers . Login
9+ {
10+ public class LoginControllerTests
11+ {
12+ private readonly Mock < ILoginService > _loginServiceMock ;
13+ private readonly LoginController _controller ;
14+
15+ public LoginControllerTests ( )
16+ {
17+ _loginServiceMock = new Mock < ILoginService > ( ) ;
18+ _controller = new LoginController ( _loginServiceMock . Object ) ;
19+ }
20+
21+ [ Fact ]
22+ public async Task GetLogin_ReturnsOk_WhenServiceReturnsSuccess ( )
23+ {
24+ // Arrange
25+ 26+ var result = new Result < string >
27+ {
28+ StatusCode = ( int ) HttpStatus . Ok ,
29+ Data = "token123" ,
30+ Message = "Login successfully"
31+ } ;
32+
33+ _loginServiceMock
34+ . Setup ( s => s . GetLogin ( email ) )
35+ . ReturnsAsync ( result ) ;
36+
37+ // Act
38+ var response = await _controller . GetLogin ( email ) ;
39+
40+ // Assert
41+ var okResult = Assert . IsType < OkObjectResult > ( response ) ;
42+ Assert . Equal ( 200 , okResult . StatusCode ) ;
43+ Assert . Equal ( result , okResult . Value ) ;
44+ }
45+
46+ [ Fact ]
47+ public async Task GetLogin_ReturnsBadRequest_WhenServiceReturnsBadRequest ( )
48+ {
49+ // Arrange
50+ 51+ var result = new Result < string >
52+ {
53+ StatusCode = ( int ) HttpStatus . BadRequest ,
54+ Data = null ,
55+ Message = "Invalid email"
56+ } ;
57+
58+ _loginServiceMock
59+ . Setup ( s => s . GetLogin ( email ) )
60+ . ReturnsAsync ( result ) ;
61+
62+ // Act
63+ var response = await _controller . GetLogin ( email ) ;
64+
65+ // Assert
66+ var badRequestResult = Assert . IsType < BadRequestObjectResult > ( response ) ;
67+ Assert . Equal ( 400 , badRequestResult . StatusCode ) ;
68+ Assert . Equal ( result , badRequestResult . Value ) ;
69+ }
70+ }
71+ }
0 commit comments