File tree Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Original file line number Diff line number Diff line change @@ -536,9 +536,11 @@ func init() {
536536
537537## ⏳ Mock 与单元测试
538538
539- Go-Spring 提供了和 go test 无缝集成的单元测试框架。
540- 你可以使用 ` gstest.MockFor[T]().With(obj) ` 在运行时替换掉一个 bean。
541- 你可以使用 ` gstest.Wire(t, ...) ` 或者 ` gstest.Get[T](t) ` 获取对象实例。
539+ Go-Spring 提供了与标准 ` go test ` 无缝集成的单元测试框架,让依赖注入和模拟测试变得简单高效。
540+
541+ ### 1. 模拟对象注入
542+
543+ 使用 ` gstest.MockFor[T]().With(obj) ` 可以在运行时轻松替换任何 bean:
542544
543545``` go
544546gstest.MockFor [*book_dao.BookDao ]().With (&book_dao.BookDao {
@@ -553,11 +555,19 @@ gstest.MockFor[*book_dao.BookDao]().With(&book_dao.BookDao{
553555})
554556```
555557
558+ ### 2. 获取测试对象
559+
560+ 有两种方式获取被测试对象:
561+
562+ ** 直接获取实例** :
563+
556564``` go
557565o := gstest.Get [*BookDao](t)
558566assert.NotNil (t, o)
559567```
560568
569+ ** 结构化注入** :
570+
561571``` go
562572s := gstest.Wire (t, new (struct {
563573 SvrAddr string ` value:"${server.addr}"`
You can’t perform that action at this time.
0 commit comments