英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论