का उपयोग करके किए गए और असफल डिफरर्ड ऑब्जेक्ट का परीक्षण कैसे करें जावास्क्रिप्ट सबमिट अनुरोध (1) के बारे में कोड यहां दिया गया है।
यहां जैस्मीन (2) का उपयोग करके AJAX अनुरोध को मजाक करने के बारे में परीक्षण है।जैस्मीन
मैं सर्वर व्यवहार का नकल करना चाहता हूं। कोई विचार?
अधिक जानकारी के लिए (1) और (2) में टिप्पणी देखें।
पीएस .:
असल में दोनों मामलों में नकली फंक्शन के विफल और असफल डिफरर्ड ऑब्जेक्ट को बुलाया जाता है।
(1)
submitForm: function() {
// the server execute fail only if message.val() is empty
// and I would like to mock this behaviour in (2)
backendController.submitForm(message.val()).done(this.onSuccess).fail(this.onError);
},
backendController.submitForm = function (message) {
return $.ajax({
url: 'some url',
type: 'POST',
dataType: 'json',
data: {
message: message
}
}).done(function() {
//some code;
});
};
(2)
describe('When Submit button handler fired', function() {
var submitFormSpy,
fakeFunction = function() {
this.done = function() {
return this;
};
this.fail = function() {
return this;
};
return this;
};
beforeEach(function() {
submitFormSpy = spyOn(backendController, 'submitForm').andCallFake(fakeFunction);
});
describe('if the message is empty', function() {
beforeEach(function() {
this.view.$el.find('#message').text('');
this.view.$el.find('form').submit();
});
it('backendController.submitForm and fail Deferred Object should be called', function() {
expect(submitFormSpy).toHaveBeenCalled();
// how should I test that fail Deferred Object is called?
});
});
describe('if the message is not empty', function() {
beforeEach(function() {
this.view.$el.find('#message').text('some text');
this.view.$el.find('form').submit();
});
it('backendController.submitForm should be called and the fail Deferred Object should be not called', function() {
expect(submitFormSpy).toHaveBeenCalled();
// how should I test that fail Deferred Object is not called?
});
});
});
में किया() कोड अपने अजाक्स के चारों ओर एक आवरण का उपयोग करके कॉल उन्हें बहुत आसान परीक्षण करना होगा। Https://github.com/webadvanced/takeCommand – Paul
@Paul अपने सुझाव के लिए धन्यवाद देखें। असल में मुझे समझ में नहीं आ रहा है कि मुझे '/ webadvanced/takeCommand' का उपयोग क्यों करना चाहिए क्योंकि $ .ajax' के पास पहले से ही मुझे चाहिए। क्या आप मुझे कुछ उदाहरण कोड लिखने का उत्तर दे सकते हैं? –
var spy = spyOn (कक्षा, "विधि"); actionThatCallsClassMethod(); उम्मीद (जासूसी) .toHaveBeenCalled(); ने मुझे आपके उदाहरण से बाहर करने में मदद की। – Aligned