-
Notifications
You must be signed in to change notification settings - Fork 12
/
validation_controller_test.go
63 lines (53 loc) · 1.93 KB
/
validation_controller_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package controllers
import (
"testing"
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/support/carbon"
testingmock "github.com/goravel/framework/testing/mock"
"github.com/goravel/gin"
"github.com/stretchr/testify/suite"
)
type ValidationControllerTestSuite struct {
suite.Suite
}
func TestValidationControllerTestSuite(t *testing.T) {
suite.Run(t, &ValidationControllerTestSuite{})
}
// SetupTest will run before each test in the suite.
func (s *ValidationControllerTestSuite) SetupTest() {
}
// TearDownTest will run after each test in the suite.
func (s *ValidationControllerTestSuite) TearDownTest() {
}
func (s *ValidationControllerTestSuite) TestJson() {
mockFactory := testingmock.Factory()
mockContext := mockFactory.Context()
mockRequest := mockFactory.ContextRequest()
mockResponse := mockFactory.ContextResponse()
mockValidator := mockFactory.ValidationValidator()
mockContext.EXPECT().Request().Return(mockRequest).Once()
mockRequest.EXPECT().Validate(map[string]string{
"name": "required",
"date": "required|date",
}).Return(mockValidator, nil).Once()
mockValidator.EXPECT().Fails().Return(false).Once()
var user User
mockValidator.EXPECT().Bind(&user).Run(func(user any) {
user.(*User).Name = "Goravel"
user.(*User).Date = carbon.NewDateTime(carbon.Parse("2024-07-08 22:34:31"))
}).Return(nil).Once()
mockContext.EXPECT().Response().Return(mockResponse).Once()
mockResponseStatus := mockFactory.ResponseStatus()
mockResponse.EXPECT().Success().Return(mockResponseStatus).Once()
resp := &gin.JsonResponse{}
mockResponseStatus.EXPECT().Json(http.Json{
"name": "Goravel",
"date": "2024-07-08 22:34:31",
}).Return(resp).Once()
s.Equal(resp, NewValidationController().Json(mockContext))
mockContext.AssertExpectations(s.T())
mockRequest.AssertExpectations(s.T())
mockResponse.AssertExpectations(s.T())
mockValidator.AssertExpectations(s.T())
mockResponseStatus.AssertExpectations(s.T())
}