【发布时间】:2018-01-18 22:02:34
【问题描述】:
Jest 的模拟可以处理我没有编写的模块中的函数吗?
node-yelp-api-v3 有 Yelp.searchBusiness(String) 但我尝试使用 Jest's mocking functionality 没有成功。 Jest 示例似乎假设我在模拟项目中的一个模块。从文档中我也不清楚如何模拟模块中的特定功能。
这些都不起作用:
jest.mock('Yelp.searchBusiness', () => {
return jest.fn(() => [{<stubbed_json>}])
})
或
jest.mock('Yelp', () => {
return jest.fn(() => [{<stubbed_json>}])
})
我目前正在使用sinon,但只想使用 Jest。这种 Sinon 方法有效:
var chai = require('chai')
var should = chai.should()
var agent = require('supertest').agent(require('../../app'))
const Yelp = require('node-yelp-api-v3')
var sinon = require('sinon')
var sandbox
describe('router', function(){
beforeEach(function(){
sandbox = sinon.sandbox.create()
stub = sandbox.stub(Yelp.prototype, 'searchBusiness')
})
afterEach(function(){
sandbox.restore()
})
it ('should render index at /', (done) => {
/* this get invokes Yelp.searchBusiness */
agent
.get('/')
.end(function(err, res) {
res.status.should.equal(200)
res.text.should.contain('open_gyro_outline_500.jpeg')
done()
})
})
})
【问题讨论】:
标签: jestjs