我有一个方法可以根据接收到的参数返回不同的值
const myAPI = {
getRepository: async data => await connectDB(data)
}
userService = {
create: async data => {
const user = await getRepository('User').save(data.user)
const image = await getRepository('Image').save(data.image)
// this code shows that returned values matter
if (user & image) {
return { user, image }
} else if (user) {
return { user }
} else if (image) {
return { image }
} else {
return {}
}
}
}
这是我的模拟物
import { createSandbox } from 'sinon'
const mock = createSandbox().mock(myAPI)
mock
.expects('getRepository')
.withArgs('User')
.returns(fakeUser)
.withArgs('Image')
.returns(fakeImage)
(async () => await userService.create({ user, image }))()
我有一个以下错误
ExpectationError: Unexpected call: getRepository(function User() {})
Expected getRepository([function Image() {}, function User() {}][, ...]) once (never called)
我的代码应如何显示以便模拟能够正常工作?