如何在Golang中使用Revel框架从控制器模拟服务?

I am writing unit test for project written in revel framework. Revel itself provides testing framework but its more towards BDD test type. So I started with go test. The MyController.go file looks like below

type MyController struct {
    *revel.Controller
}

func (c MyController) HealthCheckHandler() revel.Result {
   responseBody, status := service.HealthService()
   c.Response.Status = status
   return c.RenderJSON(responseBody)
 }

MyController_test.go file

func TestHealth(t *testing.T ){

myController := &MyController{
    &revel.Controller{
        Response:&revel.Response{
            Status:200,
        },
    }   ,
    }
 res := myController.HealthCheckHandler()

}

How can I mock HealthService() method ? If I use Interface-Composition way for mocking then how routes will inject the interface? As routes.go is self generated file.

douba3975
douba3975 这将能够模拟服务。但是路由将如何初始化服务?由于route.go和main.go文件是自行生成的。
2 年多之前 回复
doujin8673
doujin8673 服务似乎是全局的,请使其成为MyController上的字段,并将其定义为带有一个HealthService方法的接口。然后在您的测试文件中实现一个满足该接口的服务模拟类型,并在您的测试内部初始化该模拟并将其设置为myController实例上的service字段。
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐