How can I simulate a long response time using Mockjax? Despite setting the responseTime to 20 seconds, my ajax call is still being executed immediately when the page loads.
Any suggestions on how to fix this issue?
To isolate potential sources of error, I've minimized my test page:
<!DOCTYPE HTML>
<html>
<head>
<script src="jquery.js"></script>
<script src="../jquery.mockjax.js"></script>
<title>MockJax Tests</title>
</head>
<body>
<h1>A MockJax test.</h1>
<p>Check the console for results.</p>
<script>
$.mockjax({
url: "foo.html",
responseTime: 20000,
responseText: "Hi! I am mockjax."
});
$.ajax({
async: false,
url: 'foo.html',
success:function(data){
console.log(data);
},
error:function(data){
console.log('It doesn’t work that way :(');
}
});
</script>
</body>
</html>
I have also implemented a test using CasperJS and Mockjax (within casper.evaluate), with similar results.
Below is the CasperJS code snippet:
var casper = require("casper").create({
verbose: true,
logLevel: 'error',
clientScripts: ["node_modules/jquery-mockjax/jquery.mockjax.js"]
});
casper.on('remote.message', function(msg) {
this.echo('remote message caught: ' + msg);
})
casper.start('http://der-zyklop.de/', function() {
this.evaluate(function () {
$.mockjax({
url: "/blog/feed",
responseTime: 20000,
responseText: "Hi! I am mockjax!"
});
$.ajax({
async: false,
url: '/blog/feed',
success:function(data){
console.log(data);
},
error:function(data){
console.log('It doesn’t work that way :(');
}
});
});
});
casper.run();
If you have CasperJS installed, you can run the code by npm install jquery-mockjax
followed by casperjs test.js
. The output should be generated in less than 20 seconds.
For more details, you can refer to my blog article here.