如何正确定义gorm数据库接口?

huangapple go评论105阅读模式
英文:

How to properly define a gorm db interface?

问题

我正在尝试对使用类型*gorm.DB的方法进行单元测试,并且想知道如何正确定义一个接口,以便能够在不连接到数据库的情况下进行测试。

这是一个我想要做的简单示例:

type DBProxy interface {
    Create(values interface{}) *DBProxy
    Update(values interface{}) *DBProxy
}

type TestDB struct{}

func (t *TestDB) Create(values interface{}) *TestDB {
    return t
}

func (t *TestDB) Update(values interface{}) *TestDB {
    return t
}

func Connect() DBProxy {
    return &TestDB{}
}

这导致以下错误:

cannot use TestDB literal (type *TestDB) as type DBProxy in return argument:
    *TestDB does not implement DBProxy (wrong type for Create method)
        have Create(interface {}) *TestDB
        want Create(interface {}) *DBProxy

任何帮助将不胜感激!


更新

这是我的实际应用程序代码:

package database

import (
    "log"
    "os"

    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/postgres"
)

type DBProxy interface {
    Create(values interface{}) DBProxy
    Update(values interface{}) DBProxy
}

func Connect() DBProxy {
    databaseUrl := os.Getenv("DATABASE_URL")

    db, err := gorm.Open("postgres", databaseUrl)

    if err != nil {
        log.Fatal(err)
    }

    return db
}

这导致以下错误:

cannot use db (type *gorm.DB) as type DBProxy in return argument:
    *gorm.DB does not implement DBProxy (wrong type for Create method)
        have Create(interface {}) *gorm.DB
        want Create(interface {}) DBProxy
英文:

I am trying to unit test methods that use type *gorm.DB and am curious how to properly define an interface that will enable me to test without hitting a db.

Here is a small example of what I want to do:

type DBProxy interface {
    Create(values interface{}) *DBProxy
    Update(values interface{}) *DBProxy
}

type TestDB struct{}

func (t *TestDB) Create(values interface{}) *TestDB {
    return t
}

func (t *TestDB) Update(values interface{}) *TestDB {
    return t
}

func Connect() DBProxy {
    return &TestDB{}
}

Which results in:

cannot use TestDB literal (type *TestDB) as type DBProxy in return argument:
    *TestDB does not implement DBProxy (wrong type for Create method)
        have Create(interface {}) *TestDB
        want Create(interface {}) *DBProxy

Any help would be appreciated!


UPDATE

Here is my actual application code:

package database

import (
    "log"
    "os"

    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/postgres"
)

type DBProxy interface {
    Create(values interface{}) DBProxy
	Update(values interface{}) DBProxy
}

func Connect() DBProxy {
    databaseUrl := os.Getenv("DATABASE_URL")

	db, err := gorm.Open("postgres", databaseUrl)

    if err != nil {
	    log.Fatal(err)
	}

	return db
}

Which results in:

cannot use db (type *gorm.DB) as type DBProxy in return argument:
    *gorm.DB does not implement DBProxy (wrong type for Create method)
        have Create(interface {}) *gorm.DB
        want Create(interface {}) DBProxy

答案1

得分: 1

TestDB方法必须返回*DBProxy以实现DBProxy接口。

func (t *TestDB) Create(values interface{}) *DBProxy {
    return t
}
func (t *TestDB) Update(values interface{}) *DBProxy {
    return t
}

稍后,您需要断言CreatedDBProxy.(TestDB)

英文:

TestDB methods must return *DBProxy to implement DBProxy

func (t *TestDB) Create(values interface{}) *DBProxy {
    return t
}
func (t *TestDB) Update(values interface{}) *DBProxy {
    return t
}

later you will need to assert CreatedDBProxy.(TestDB)

huangapple
  • 本文由 发表于 2016年4月24日 05:18:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/36816553.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定