2017-04-25 60 views
1

我試圖爲我的應用程序在Vue和vue-cli中使用webpack模板創建一個單元測試。我如何在Vue中用sinon窺探或模擬方法?

我閱讀Vue文檔,vue-loader,vue-cli和vue-template/webpack(更多!)。當我嘗試爲我的組件進行單元測試時,我使用它。

const Constructor = Vue.extend(MovieList) 
vm = new Constructor({ 
    propsData: { 
     criteria 
    }, 
    methods: { 
     fetch: sandbox.spy() 
    } 
}).$mount() 

和測試這樣的:

expect(vm.fetch).to.be.called 

但我得到這個錯誤:

✗ should be called fetch

TypeError: { [Function: boundFn] _length: 0 } is not a spy or a call to a spy! at assertCanWorkWith (/home/jmanuelrosa/Developer/personales/vue-course/movues/node_modules/sinon-chai/lib/sinon-chai.js:43:19) at Assertion. (/home/jmanuelrosa/Developer/personales/vue-course/movues/node_modules/sinon-chai/lib/sinon-chai.js:70:13) at Assertion.addProperty (/home/jmanuelrosa/Developer/personales/vue-course/movues/node_modules/chai/chai.js:4240:29) at Context. (webpack:///test/unit/specs/MovieList.spec.js:74:39 <- index.js:14910:39)

回答

1

決不興農但DOC的工作說:

sinon.spy(vm, "fetch"); 
expect(vm.fetch).toHaveBeenCalled(); 

http://sinonjs.org/releases/v2.1.0/spies/

您是否在創建組件時調用獲取?有時你需要手動觸發方法。

相關問題