jasmine を使用して単体テストを行い、sinon を使用して ajax 呼び出しをモックしています。ただし、これにより、テスト ケースが繰り返し呼び出される無限ループが発生します。ajax 呼び出しは同期的に行われます。ajax 呼び出しを非同期に変更すると、無限ループは発生しません。どんな助けや指針も大歓迎です!
ajax 呼び出しを使用した関数は次のとおりです。
define([
// Utils
'util/logging',
// Project Dependencies
'views/core/baseView',
'i18n!i18n_DNA/nls/basePage',
'i18n!i18n_DNA/nls/consentSettingsView',
'duster!templates/widgets/testSettings/consentSettings.dust.html'
], function (Log, BaseView, i18n_basePage, i18n_consentSettingsView, template) {
'use strict';
// ConsentSettingsView
// -----------------------------------------------------------------------------------------
return BaseView.extend({
events: {
'click #deleteTestButton': 'deleteTestModal'
},
className: 'connectDelete',
// initialize()
// ---------------------------------------------------------------------------------------
initialize: function () {
this.hasConsented = this.options.hasConsented;
this.testId = this.options.testId;
this.appRouter = this.options.appRouter;
// Call `super()`.
BaseView.prototype.initialize.call(this);
},
// render()
// ---------------------------------------------------------------------------------------
render: function () {
var self = this;
var context = {
i18n_basePage: i18n_basePage,
i18n_consentSettingsView: i18n_consentSettingsView,
hasConsented: this.hasConsented,
testId: this.testId
};
dust.render('templates/widgets/testSettings/consentSettings.dust.html', context, function (err, out) {
if (err) {
console.error(err);
} else {
self.$el.html(out);
}
});
return self;
},
// deleteTestModal()
// ---------------------------------------------------------------------------------------
deleteTestModal: function () {
AcomModal.open('deleteTestModal');
$('#pDelId').click($.proxy( this.deleteTest, this));
},
// deleteTest()
// ---------------------------------------------------------------------------------------
deleteTest: function () {
var self = this;
$.ajax({
url: 'testSettings/' + encodeURIComponent(this.testId),
dataType: 'json',
data: {
password: $('#pUserPwd').val(),
testId: self.testId
},
async: false,
type: 'DELETE',
success: function (data, textStatus, jqXHR) {
Log.info("Successfully deleted test #", self.testId, data, textStatus, jqXHR);
},
error: function (jqXHR, textStatus, errorThrow) {
Log.error(jqXHR, textStatus, errorThrow);
switch (jqXHR.status) {
case 401:
self.appRouter.navigate("#login", {trigger: true});
break;
case 404:
self.appRouter.navigate("#404", {trigger: true});
break;
default:
self.appRouter.navigate("#serviceUnavailable", {trigger: true});
}
},
complete: function (jqXHR, textStatus) {
$('#pDelId').unbind('click');
window.location = window.location.origin + window.location.pathname + window.location.search;
}
});
}
});
});
これが私のテストケースです:
define([
'backbone',
'views/widgets/testSettings/consentSettingsView'
], function (Backbone, ConsentSettingsView) {
// deleteTestModal()
// ---------------------------------------------------------------------------------------
describe('ConsentSettingsView.deleteTest()', function () {
var server;
var appRouter;
var consentSettingsView;
// Setup
// -----------------------------------------------------------------------------------------
beforeEach(function(){
appRouter = {
navigate: function () {
console.log('navigate called', arguments);
}
};
consentSettingsView = new ConsentSettingsView({
hasConsented: false,
testId: 1,
appRouter: appRouter
});
server = sinon.fakeServer.create();
server.respondWith(/testSettings\/(\d+)/, function(xhr, id) {
switch (id) {
case '1':
xhr.respond(200, {'Content-Type': 'application/json'}, JSON.stringify({id: id, success: true}));
break;
case '2':
xhr.respond(401, {'Content-Type': 'application/json'}, JSON.stringify({id: id, success: false}));
break;
case '3':
xhr.respond(404, {'Content-Type': 'application/json'}, JSON.stringify({id: id, success: false}));
break;
default:
xhr.respond(500, {'Content-Type': 'application/json'}, JSON.stringify({id: id, success: false}));
break;
}
});
});
afterEach(function(){
server.restore();
});
it('makes a successful ajax call', function () {
consentSettingsView.testId = 1;
consentSettingsView.deleteTest();
});
});
});