私のクライアント側のAPIの使用方法は、karma
のMocha
とSino
です。しかし、私は非同期プロセスを取得するつもりです。何らかの理由で非同期のajax呼び出しによるSinon/Mochaテストで約束が返されない
import api from '../../../src/api';
import stubData from '../data';
import axios from 'axios';
/* eslint-disable prefer-arrow-callback,func-names */
describe('API test', function() {
before(function() {
this.server = sinon.fakeServer.create();
});
after(function() {
this.server.restore();
});
it('Should return cart with provided token', function (done) {
this.server.respondWith("GET", "/cart",
[200, { "Content-Type": "application/json" },
'[{ "id": 12, "comment": "Hey there" }]']);
axios.get('/cart')
.then(function (response) {
console.log(response);
done();
})
.catch(function (error) {
console.log(error);
done();
});
this.server.respond();
});
});
、私はいつもMocha
からError: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
を取得。 axios.get()
が実行されていないのでthen
のように見えるので、done
も呼び出されません。
私は、私はここで何かを
describe('User', function() {
describe('#save()', function() {
it('should save without error', function(done) {
var user = new User('Luna');
user.save(function(err) {
if (err) done(err);
else done();
});
});
});
});
Mocha
文書で推奨されたものをフォローをしていたのですか?ありがとうございました
のGithubのREADMEから
例は、夫婦の時間を過ごします。しかし、少なくとも私は愚かではないことを知っています... hah –
@IanZhao彼らはaxishにはリクエストを模倣する 'moxios'と呼ばれるlibがあるという問題についても言及しています。https://github.com/mzabriskie/moxios – BrunoLM