0
我該如何監視一個函數,該函數是從一個對象內的引用調用的? 我使用Jasmine 2.5.2作爲我的測試框架,並使用babel-plugin-rewire來重新連接依賴關係。茉莉花和巴貝爾重新引用間諜參考的方法
請看:
a.js
const map = {
a,
b,
c
};
function run(options) {
map[options.val]();
}
function a() {...}
function b() {...}
function c() {...}
a.spec.js
import { a, __RewireAPI__ as ARewireAPI } from './a';
describe('a',() => {
describe('run',() => {
const spy= jasmine.createSpy('spy').and.callFake(() => {
...
});
beforeEach(() => {
ARewireAPI.__Rewire__('b', spy);
});
afterEach(() => {
ARewireAPI.__ResetDependency__('b');
});
it('calls b()',() => {
a.run({val: 'b'}); // doesn't call the spy because what actually being called is the reference from the map object
});
});
});