我是新来的。我正在尝试测试Go例程中的函数调用,但是错误消息失败了。
预期的呼叫数(8)与实际呼叫数(0)不匹配。
我的测试代码如下:
package executor
import (
"testing"
"sync"
"github.com/stretchr/testify/mock"
)
type MockExecutor struct {
mock.Mock
wg sync.WaitGroup
}
func (m *MockExecutor) Execute() {
defer m.wg.Done()
}
func TestScheduleWorksAsExpected(t *testing.T) {
scheduler := GetScheduler()
executor := &MockExecutor{}
scheduler.AddExecutor(executor)
// Mock exptectations
executor.On("Execute").Return()
// Function Call
executor.wg.Add(8)
scheduler.Schedule(2, 1, 4)
executor.wg.Wait()
executor.AssertNumberOfCalls(t, "Execute", 8)
}我的应用程序代码是:
package executor
import (
"sync"
"time"
)
type Scheduler interface {
Schedule(repeatRuns uint16, coolDown uint8, parallelRuns uint64)
AddExecutor(executor Executor)
}
type RepeatScheduler struct {
executor Executor
waitGroup sync.WaitGroup
}
func GetScheduler() Scheduler {
return &RepeatScheduler{}
}
func (r *RepeatScheduler) singleRun() {
defer r.waitGroup.Done()
r.executor.Execute()
}
func (r *RepeatScheduler) AddExecutor(executor Executor) {
r.executor = executor
}
func (r *RepeatScheduler) repeatRuns(parallelRuns uint64) {
for count := 0; count < int(parallelRuns); count += 1 {
r.waitGroup.Add(1)
go r.singleRun()
}
r.waitGroup.Wait()
}
func (r *RepeatScheduler) Schedule(repeatRuns uint16, coolDown uint8, parallelRuns uint64) {
for repeats := 0; repeats < int(repeatRuns); repeats += 1 {
r.repeatRuns(parallelRuns)
time.Sleep(time.Duration(coolDown))
}
}你能告诉我我在这里可能做错了什么吗?我用的是Go 1.16.3。调试代码时,可以看到调用了Execute()函数,但不能注册函数调用
发布于 2021-05-06 02:06:30
您需要调用Called(),以便mock.Mock记录Execute()已被调用的事实。由于您不担心参数或返回值,以下内容应该解决您的问题:
func (m *MockExecutor) Execute() {
defer m.wg.Done()
m.Called()
}但是,我注意到,当前编写测试的方式--这个测试可能无法完成您想要的任务。这是因为:
在调用forever).
executor.wg.Wait() (这将等待函数被调用的预期次数),因此,如果没有至少调用Execute()的预期次数(wg.Wait()将阻塞executor.AssertNumberOfCalls的预期次数),那么测试将永远不会完成(wg.Wait()将阻塞executor.AssertNumberOfCalls的预期次数,即存在竞争条件的预期次数(如果executor仍在运行,则在executor.AssertNumberOfCalls和下一个m.Called()之间进行竞争)。如果wg.Done()确实被调用了额外的时间,您就会感到恐慌(我想您可能会认为失败了!)但我可能会简化一些测试:scheduler.Schedule(2, 1, 4)
time.Sleep(time.Millisecond) // Wait long enough that all executions are guaranteed to have completed (should be quick as Schedule waits for go routines to end)
executor.AssertNumberOfCalls(t, "Execute", 8)https://stackoverflow.com/questions/67410367
复制相似问题