unit-testing - 错误 : suite. go:61: test paniced: reflect: Call with too few input arguments

标签 unit-testing go testify

我正在 golang 中设置单元测试。
但是现在我在运行 go test -v 时遇到错误。
我想解决这个错误并使测试成功。

article
  ├ client
  ├ api
  │  ├ main.go
  │  ├ contoroller
  │  │    ├ contoroller.go
  │  │    └ contoroller_test.go
  │  ├ service
  │  │    ├ service.go
  │  │    └ service_test.go
  │  ├ dao
  │  │    ├ dao.go
  │  │    └ dao_test.go
  │  ├ s3
  │  │    ├ s3.go
  │  │    └ s3_test.go
  │  ├ go.mod 
  │  ├ go.sum
  │  └ Dockerfile
  ├ nginx
  └ docker-compose.yml

现在我正在为 service.go 设置 service_test.go

service_test.go

package service

// import

type MockDaoInterface struct {
}

func (_m *MockDaoInterface) GetArticleDao() *sql.Rows {
    db, mock, _ := sqlmock.New()
    mockRows := mock.NewRows([]string{"id", "uuid", "title", "content"}).
        AddRow(1, "bea1b24d-0627-4ea0-aa2b-8af4c6c2a41c", "test", "test").
        AddRow(2, "844bc620-7336-41a3-9cb4-552a0024ff1c", "test2", "test2")
    mock.ExpectQuery("select").WillReturnRows(mockRows)
    rows, _ := db.Query("select")
    return rows
}


type ServiceSuite struct {
    suite.Suite
    service *Service
    dao     dao.DaoInterface
}

func (s *ServiceSuite) SetupTest() {
    s.service = NewService(s.dao)
    s.service.dao = &MockDaoInterface{}
}

func (s *ServiceSuite) TestGetArticleService(t *testing.T) {

    articles := s.service.GetArticleService()

    var expectedArticles []util.Article

    expectedArticle1 := util.Article{
        ID:      1,
        UUID:    "bea1b24d-0627-4ea0-aa2b-8af4c6c2a41c",
        TITLE:   "test",
        CONTENT: "test",
    }
    expectedArticles = append(expectedArticles, expectedArticle1)

    expectedArticle2 := util.Article{
        ID:      2,
        UUID:    "844bc620-7336-41a3-9cb4-552a0024ff1c",
        TITLE:   "test2",
        CONTENT: "test2",
    }
    expectedArticles = append(expectedArticles, expectedArticle2)

    assert.Equal(s.T(), expectedArticles, articles)
}

func TestServiceSuite(t *testing.T) {
    suite.Run(t, new(ServiceSuite))
}

服务.go

package service

// import 

type Service struct {
    dao dao.DaoInterface
}

func NewService(dao dao.DaoInterface) *Service {
    return &Service{dao: dao}
}

func (s Service) GetArticleService() []util.Article {
    var articles []util.Article

    results := s.dao.GetArticleDao()

    article := util.Article{}
    for results.Next() {
        err := results.Scan(&article.ID, &article.UUID, &article.TITLE, &article.CONTENT)
        if err != nil {
            panic(err.Error())
        } else {
            articles = append(articles, article)
        }
    }
    return articles
}

dao.go

package dao

// import

type Dao struct {
    database *sql.DB
    s3       s3.S3Interface
}

func NewDao(database *sql.DB, s3 s3.S3Interface) *Dao {
    objs := &Dao{database: database, s3: s3}
    return objs
}

type DaoInterface interface {
    GetArticleDao() *sql.Rows
}

这里是完整的源代码(分支:go-test-service)
https://github.com/jpskgc/article/tree/go-test-service

我希望 service_test.go 能够成功测试。
但实际是失败并报错。
我想解决这个错误并成功测试。

$ go test -v
=== RUN   TestServiceSuite
=== RUN   TestServiceSuite/TestGetArticleService
--- FAIL: TestServiceSuite (0.00s)
    --- FAIL: TestServiceSuite/TestGetArticleService (0.00s)
        suite.go:61: test panicked: reflect: Call with too few input arguments
FAIL
exit status 1
FAIL    article/api/service     0.045s

最佳答案

通过从参数中删除 t *testing.T 解决了这个问题。

func (s *ServiceSuite) TestGetArticleService() {
// some code
}

关于unit-testing - 错误 : suite. go:61: test paniced: reflect: Call with too few input arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57848372/

相关文章:

typescript - 如何使用 jest 测试对同一函数的特定调用序列

go - "Setting up workspace: Loading packages..."耗时太长

unit-testing - Golang模拟上下文 panic

javascript - 如何编写 QUnit 测试来测试事件?

php - Laravel 依赖注入(inject) : When do you have to? 你什么时候可以模拟门面?两种方法的优点?

function - 如果存在一个没有方法的类型,你能认为它是一个对象吗?

linux - 在子级中安装seccomp过滤器

go - 从包函数返回 Mock

ruby - 在静默模式下测试 Rake 任务