import store from <path to store>
import MyComponent from <path to my component>
// load the component with a vue instance
var vm = new Vue({
template: '<div><test :items="items" :id="id" ref=testcomponent></test></div>',
components: {
'test': MyComponent
},
data() {
return {
items: [],
id: 'myId'
}
},
store
}).$mount();
var myComponent = vm.$refs.testcomponent;
it('should add item to the shopping list array and increase its length', () => {
//call the add_shopping_list mutations
mutations[ADD_SHOPPING_LIST](state, {id: '1'})
//check that the array now equals array with new object
expect(state.shoppinglists).to.eql([{id: '1'}])
//check that array's length had increased
expect(state.shoppinglists).to.have.length(1)
})
创建完这个函数后, 整个 spec 代码看起来是这样的:
// mutations.spec.js
import mutations from 'src/vuex/mutations'
import { ADD_SHOPPING_LIST, DELETE_SHOPPING_LIST, POPULATE_SHOPPING_LISTS, CHANGE_TITLE } from 'src/vuex/mutation_types'
describe('mutations.js', () => {
var state
beforeEach(() => {
state = {shoppinglists: []
}
})
describe('ADD_SHOPPING_LIST', () => {
it('should add item to the shopping list array and increase its length', () => {
mutations[ADD_SHOPPING_LIST](state, {id: '1'})
expect(state.shoppinglists).to.eql([{id: '1'}])
expect(state.shoppinglists).to.have.length(1)
})
})
})
it('should test that commit is called with correct parameters', () => {
actions.populateShoppingLists({ commit }).then(() => {
expect(commit).to.have.been.calledWith(<...>)
})
})
it('should test that commit is called with correct parameters',
(done) => {
actions.populateShoppingLists({ commit }).then(() => {
expect(commit).to.have.been.calledWith(<...>)
done()
})
})
现在就编码! 创建一个测试说明叫 actions.spec.js , 写入:
// actions.spec.js
import actions from 'src/vuex/actions'
import { CHANGE_TITLE, POPULATE_SHOPPING_LISTS } from 'src/vuex/mutation_types'
describe('actions.js', () => {
describe('populateShoppingLists', () => {
//here we will add our test case
})
})
//AddItemComponent.spec.js
it('should call $emit method', () => {
let newItem = 'Learning Vue JS'
// stub $emit method
sinon.stub(component, '$emit')
// stub store's dispatch method
sinon.stub(store, 'dispatch')
// set a new item
component.newItem = newItem
component.addItem()
// newItem should be reset
expect(component.newItem).to.eql('')
// $emit should be called with custom event 'add' and a newItem value
expect(component.$emit).to.have.been.calledWith('add', newItem)
// dispatch should be called with updateList and the id of the list
expect(store.dispatch).to.have.been.calledWith('updateList', 'niceId')
store.dispatch.restore()
component.$emit.restore()
})
试着为购物清单方程式剩下的组件写单元测试。
为我们的番茄钟写单元测试
好的! 我们看看我们的番茄钟!
// mutations.spec.js
import Vue from 'vue'
import mutations from 'src/vuex/mutations'
import * as types from 'src/vuex/mutation_types'
describe('mutations', () => {
var state
beforeEach(() => {
state = {}
// let's mock Vue noise plugin
//to be able to listen on its methods
Vue.noise = {
start: () => {},
stop: () => {},
pause: () => {}
}
sinon.spy(Vue.noise, 'start')
sinon.spy(Vue.noise, 'pause')
sinon.spy(Vue.noise, 'stop')
})
afterEach(() => {
Vue.noise.start.restore()
Vue.noise.pause.restore()
Vue.noise.stop.restore()
})
describe('START', () => {
})
})
it('should set all the state properties correctly after start', () => {
// ensure that all the properties are undefined
// before calling the start method
expect(state.started).to.be.undefined
expect(state.stopped).to.be.undefined
expect(state.paused).to.be.undefined
expect(state.interval).to.be.undefined
// call the start method
mutations[types.START](state)
// check that all the properties were correctly set
expect(state.started).to.be.true
expect(state.paused).to.be.false
expect(state.stopped).to.be.false
expect(state.interval).not.to.be.undefined
})
module.exports = {
'default e2e tests': (browser) => {
// open the browser and check that #app is on the page
browser.url('http://localhost:8080')
.waitForElementVisible('#app', 5000);
// check that toggle-volume icon is not visible
browser.expect.element('.toggle-volume')
.to.not.be.visible
// check that pause button is disabled
browser.expect.element('[title=pause]')
.to.have.attribute('disabled')
// check that stop button is disabled
browser.expect.element('[title=stop]')
.to.have.attribute('disabled')
// check that start button is not disabled
browser.expect.element('[title=start]')
.to.not.have.attribute('disabled')
// click on start button, check that toggle volume
// button is visible
browser.click('[title=start]')
.waitForElementVisible('.toggle-volume', 5000)
// check that pause button is not disabled
browser.expect.element('[title=pause]')
.to.not.have.attribute('disabled')
// check that stop button is not disabled
browser.expect.element('[title=stop]')
.to.not.have.attribute('disabled')
// check that stop button is disabled
browser.expect.element('[title=start]')
.to.have.attribute('disabled')
browser.end()
}
}
'wait for kitten test': (browser) => {
browser.url('http://localhost:8080')
.waitForElementVisible('#app', 5000)
// initially the kitten element is not visible
browser.expect.element('.well.kittens')
.to.not.be.visible
// click on the start button and wait for 7s for
//kitten element to appear
browser.click('[title=start]')
.waitForElementVisible('.well.kittens', 7000)
// check that the image contains the src element
//that matches thecatapi string
browser.expect.element('.well.kittens img')
.to.have.attribute('src')
.which.matches(/thecatapi/);
browser.end()
}