How to Use Ext Ajax in Promise Style and Test It
After translated a blog about how Promise works in a more functional programming way,I tried to build something to make Ext Ajax call to work in Promise style as a practice. ExtPromise is a simple wrapper to Ext.Ajax and Ext.data.Connection to help you do an Ajax call in Promise style instead of passing success/failure callback to it. The Promise library I used is the bluebird. I chose it not only because its speed is faster than most of the Promise library,but also its error handling philosophy looks cleaner and more attractive. It didn't took long to implement the ExtPromise wrapper but it took me some time to test it. Originally,I thought I could use the jasmine-ajax I enhanced and shared before about how to test Ajax call in ExtJs. However,it doesn't work as expected. Testing Async method in Jasmine seems very awkward because the API in version 1.4 and 2.0 are dramaticlly different. Even worst,many strange issues messed around all the way. I finally gave up and search other alternative approaches. Sinon.js and Mocha come to rescure. It is pretty easy to test the Ajax call using the describe("Ajax should be now working in promise style",function() { var xhr,ajax; before(function() { xhr = sinon.useFakeXMLHttpRequest(); xhr.onCreate = function(xhr) { ajax = xhr; } }) after(function() { xhr.restore(); }); describe('ExtPromise.Ajax',function() { it("#success case",function(done) { ExtPromise.Ajax().request({url: 'foo'}) .then(function(result) { expect(result.responseText).to.equal('Bar'); done(); }) .catch(done); ajax.respond(200,{ 'Content-Type': 'application/json' },'Bar'); }); }); }); It's quite straightforward. Before test spec runs,it's required to stub the XMLHttpRequest using Sinon's Passing a .catch(done); If you don't do this,and the assertion in the test spec failed,the error it shows will be a timeout error instead of telling the true assertion error. When testing failure case,the style written like below doesn't look good and error-prone because ExtPromise.Ajax().request({url: 'foo',scope: scopeObj}) .then(scopeObj.getName) .then(function(result) { expect(result).to.equal('Bar In scope'); done(); },function(result) { expect(result.status).to.equal(500); done(); }) .catch(done); ajax.respond(500,'Error'); You may rewrite the call to ExtPromise.Ajax().request({url: 'foo',scope: scopeObj}) .then(scopeObj.getName) .then(function(result) { expect(result).to.equal('Bar In scope'); },function(result) { expect(result.status).to.equal(500); }) .then(done) .catch(done); ajax.respond(500,'Error'); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |