[これは、Vuex を使用し、vue-cli で作成され、mocha、chai、karma、sinon を使用する Vue アプリです]
vuex 状態のテストを作成しようとしていますが、モックを使用したくありません。これらのテストの大きな目標の 1 つは、データの取得元である API もテストすることです。
chai-as-promised のドキュメントに従おうとしています。
これは、テストしようとしている vuex アクションを簡略化したものです。
const actions = {
login: (context, payload) => {
context.commit('setFlashMessage', "");
axios.get("https://first-api-call")
.then((response) => {
axios.post("https://second-api-call")
.then((response) => {
router.push({ name: "Home"});
context.commit('setFlashMessage', "Logged in successfully");
context.commit('setLogin', response.data);
});
},
login アクションには2 つの promiseがあり、何も返さないことに注意してください。login アクションは 2 つのことを行います。状態を設定することと、ルートを変更することです。
私が見た例では、chai-as-promised を使用すると、promise が返されることが期待されます。あれは:
var result = systemUnderTest();
return expect(result).to.eventually.equal(blah);
しかし、私の場合、login() は何も返さず、返された場合に何を返すかわかりません。
これは私がこれまでに持っているものです:
import store from '@/src/store/store'
describe('login', () => {
it('bad input', () => {
store.login({ username: "abcd", password: ""});
// What is the test I should use?
}
}