QunitとMockJaxを使用して、理解しやすいようにここで簡略化した2つのテストを作成しようとしています。次の2つのテストのいずれかが失敗します。これは、おそらく2つのテストが並行して実行されるため、それぞれが独自の迂回を取得しないためです$.ajax()
。(唯一の違いは、それぞれのresponseTextです。)次の両方のテストに合格するように調整するための良い方法についてのアイデアはありますか?
function testAjax() {
return $.ajax({
type: 'POST',
dataType: 'json',
url: '/fakeservice/1',
data: {'a':'b'}
});
}
asyncTest("testAjax 1", function () {
$.mockjax({
url: '/fakeservice/1',
type: 'POST',
dataType: 'json',
responseText: { 'name1': 'foo' }
});
testAjax().then(
function (response) {
deepEqual(response.name1, 'foo', "no name1");
start();
},
function (error) {
ok(false, "got AJAX error");
start();
}
);
});
asyncTest("testAjax 2", function () {
$.mockjax({
url: '/fakeservice/1',
type: 'POST',
dataType: 'json',
responseText: { 'name1': 'bar' }
});
testAjax().then(
function (response) {
deepEqual(response.name1, "bar", "no name1");
start();
},
function (error) {
ok(false, "got AJAX error");
start();
}
);
});