[英]How to test chained Knex methods(/promises) with Jasmine2?
How can I test chained Knex methods with Jasmine2? 如何使用Jasmine2测试链接的Knex方法?
I have the following class and spec: 我有以下课程和规格:
Task class 任务班
class Task {
// ...
findBy(field, value) {
return this.Knex(this.tableName).where(field, value)
}
// ...
}
Task spec 任务规范
// ...
beforeEach(() => {
task = new Task()
spyOn(task, 'Knex').and.returnValue(Q.when({}))
spyOn(task.Knex, 'where').and.returnValue(Q.when({})) // <<< Wrong (where() isn't part of Knex anymore)
})
it('should expose findBy()', () => {
let field = 'id'
, value = 1
task.findBy(field, value)
expect(task.Knex).toHaveBeenCalledWith(task.tableName)
expect(task.Knex.where).toHaveBeenCalledWith(field, value)
})
// ...
The error message I get is: 我收到的错误消息是:
TypeError: this.Knex(...).where is not a function
There error was the return from the first spyOn()
. 从第一个
spyOn()
返回的错误。 The return is a promise without having where()
as part of it. 返回是一个承诺,而
where()
没有where()
。
This solves the problem (but lost the promise return from the first spyOn()
这解决了问题(但是丢失了第一个
spyOn()
的承诺返回
beforeEach(() => {
task = new Task()
knexRes = (spyOn(task, 'Knex').and.returnValue(({
where: function() {}
})))()
spyOn(knexRes, 'where').and.returnValue(Q.when({}))
})
it('should expose findBy()', () => {
let field = 'id'
, value = 1
task.findBy(field, value)
expect(task.Knex).toHaveBeenCalledWith(task.tableName)
expect(knexRes.where).toHaveBeenCalledWith(field, value)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.