英文:
AssertCalled always fails with testify library
问题
我正在使用testify来测试我的代码,并且我想检查一个函数是否被调用。
我正在进行以下操作:
type Foo struct {
mock.Mock
}
func (m Foo) Bar() {
}
func TestFoo(t *testing.T) {
m := Foo{}
m.Bar()
m.AssertCalled(t, "Bar")
}
我得到的错误信息是:
Error: Should be true
Messages: The "Bar" method should have been called with 0 argument(s), but was not.
mock.go:419: []
我调用了函数"Bar",然后立即询问它是否被调用,但它返回false。
我做错了什么?
使用testify正确测试函数是否被调用的方法是什么?
英文:
I am using testify to test my code and I want to check if a function was called.
I am doing the following:
type Foo struct {
mock.Mock
}
func (m Foo) Bar() {
}
func TestFoo(t *testing.T) {
m := Foo{}
m.Bar()
m.AssertCalled(t, "Bar")
}
The error I am getting:
Error: Should be true
Messages: The "Bar" method should have been called with 0 argument(s), but was not.
mock.go:419: []
I call function "Bar" and immediately ask if it was called but it returns false.
What am I doing wrong?
What is the proper way to test if a function was called with testify?
答案1
得分: 13
我试过这个,它可以工作:
type Foo struct {
mock.Mock
}
func (m *Foo) Bar() {
m.Called()
}
func TestFoo(t *testing.T) {
m := &Foo{}
m.On("Bar").Return(nil)
m.Bar()
m.AssertCalled(t, "Bar")
}
正如Chris Drew所说,你需要在Bar方法的声明中使用一个接收器指针。
此外,你需要实例化一个新的指针结构,并模拟该方法返回一个值。
英文:
I tried with this and works:
type Foo struct {
mock.Mock
}
func (m *Foo) Bar() {
m.Called()
}
func TestFoo(t *testing.T) {
m := &Foo{}
m.On("Bar").Return(nil)
m.Bar()
m.AssertCalled(t, "Bar")
}
As stated by Chris Drew, you've to use a receiver pointer on Bar method's declaration.
Additionally, you've to istantiate a new structure as pointer and mock the method to return a value.
答案2
得分: 3
根据 testify 的文档,我认为你需要显式调用 func (*Mock) Called
来告诉模拟对象一个方法已被调用。
func (m *Foo) Bar() {
m.Called()
}
在 testify 的测试中有一些示例,你可以在 这里 找到。
英文:
Looking at the documentation of testify I think you have to explicitly call func (*Mock) Called
to tell the mock object that a method has been called.
func (m *Foo) Bar() {
m.Called()
}
There are some examples in the testify tests.
答案3
得分: 1
作为当您想/需要使用值接收器时的额外解决方案,尽管不够简洁,但将Mock指定为指针字段对我来说有效。
type Foo struct {
m *mock.Mock
}
func (f Foo) Bar() {
f.m.Called()
}
func TestFoo(t *testing.T) {
f := Foo{m: &mock.Mock{}}
f.Bar()
f.m.AssertCalled(t, "Bar")
}
请注意,以上是代码的翻译部分。
英文:
As an additional solution for when you want/need to use a value receiver, though not as clean, specifying Mock as a pointer field worked for me.
type Foo struct {
m *mock.Mock
}
func (f Foo) Bar() {
f.m.Called()
}
func TestFoo(t *testing.T) {
f := Foo{m: &mock.Mock{}}
f.Bar()
f.m.AssertCalled(t, "Bar")
}
答案4
得分: 0
确保它是指针接收器而不是值接收器。
这将始终具有nil Calls。
type Foo struct {
mock.Mock
}
func (m Foo) Bar() {
m.Called()
}
这将具有N个调用。
type Foo struct {
mock.Mock
}
func (m *Foo) Bar() {
m.Called()
}
英文:
Make sure it is pointer receiver NOT value receiver.
This will always have nil Calls
type Foo struct {
mock.Mock
}
func (m Foo) Bar()
m.Called()
}
This will have N number of Calls
type Foo struct {
mock.Mock
}
func (m *Foo) Bar()
m.Called()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论