我试图将模块中调用的函数操作为true,但是如果模块没有将函数封装到对象上,我该如何模拟函数的值?
// app.js
function start(foo) {
if (isGood(foo)) initialize(foo)
}
function isGood = function(name) {
return dosomething_really_complicated_here(name) // i dont care about this, and actuallyi want to mock its return value
}
function initialize(foo) {
//... interesting stuff
}
// export {
// start as default,
// initialize
// }
// IN ANOTHER FILE
// app.test.js
// import app from './app'
// const { start, initialize } = app
describe('app', () => {
beforeEach(() => {
start('abc')
// TODO: here I want to stub isGood to return true or false, note i could
// TODO: i also want to spy on initialize
})
it('should call initialize', () => {
expect(initializeSpy).toHaveBeenCalled()
})
})
<link href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.8.0/jasmine.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.8.0/jasmine.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.8.0/jasmine-html.js"></script>
答案 0 :(得分:0)
取决于您自己的想法和偏好