@@ -5,6 +5,7 @@ package test
5
5
6
6
import (
7
7
gocontext "context"
8
+ "io"
8
9
"net/http"
9
10
"net/http/httptest"
10
11
"net/url"
@@ -16,12 +17,27 @@ import (
16
17
user_model "code.gitea.io/gitea/models/user"
17
18
"code.gitea.io/gitea/modules/context"
18
19
"code.gitea.io/gitea/modules/git"
20
+ "code.gitea.io/gitea/modules/templates"
21
+ "code.gitea.io/gitea/modules/translation"
19
22
"code.gitea.io/gitea/modules/web/middleware"
20
23
21
24
chi "github.com/go-chi/chi/v5"
22
25
"github.com/stretchr/testify/assert"
23
26
)
24
27
28
+ type mockRender struct {}
29
+
30
+ func (tr * mockRender ) TemplateLookup (tmpl string ) (templates.TemplateExecutor , error ) {
31
+ return nil , nil
32
+ }
33
+
34
+ func (tr * mockRender ) HTML (w io.Writer , status int , _ string , _ interface {}) error {
35
+ if resp , ok := w .(http.ResponseWriter ); ok {
36
+ resp .WriteHeader (status )
37
+ }
38
+ return nil
39
+ }
40
+
25
41
// MockContext mock context for unit tests
26
42
// TODO: move this function to other packages, because it depends on "models" package
27
43
func MockContext (t * testing.T , path string ) * context.Context {
@@ -34,9 +50,12 @@ func MockContext(t *testing.T, path string) *context.Context {
34
50
}
35
51
36
52
base , baseCleanUp := context .NewBaseContext (resp , req )
53
+ base .Data = middleware.ContextData {}
54
+ base .Locale = & translation.MockLocale {}
37
55
ctx := & context.Context {
38
- Base : base ,
39
- Flash : & middleware.Flash {Values : url.Values {}},
56
+ Base : base ,
57
+ Render : & mockRender {},
58
+ Flash : & middleware.Flash {Values : url.Values {}},
40
59
}
41
60
_ = baseCleanUp // during test, it doesn't need to do clean up. TODO: this can be improved later
42
61
@@ -57,6 +76,8 @@ func MockAPIContext(t *testing.T, path string) *context.APIContext {
57
76
}
58
77
59
78
base , baseCleanUp := context .NewBaseContext (resp , req )
79
+ base .Data = middleware.ContextData {}
80
+ base .Locale = & translation.MockLocale {}
60
81
ctx := & context.APIContext {Base : base }
61
82
_ = baseCleanUp // during test, it doesn't need to do clean up. TODO: this can be improved later
62
83
0 commit comments