How to use Sinon with CasperJS?
casperjs, phantomjs, require, sinon
Solution
Well there's multiple problems there. First, you are trying to require `sinon` like the way it works in node, but it doesn't work in casper because casper doesn't care if you have a node_modules directory or not, and it doesn't look into it. I assume you have installed sinon in your node_modules directory so you should do this:
var sinon = require('./node_modules/sinon');
The trick is that you can only use relative paths to get the modules installed in node_modules, because for casper there's no such thing as resolving the node_modules directory.
The next part you are doing wrong, seems like you are confused between the phantomjs side and the client side. The script you have above there is evaluated in phantomjs side, and the scripts that are included in the html are evaluated in client side. These two, don't share any memory with each other, the global objects are different. So you can't do `sinon.fakeServer.create();` in the phantomjs side, because it tries to create a fake `XMLHttpRequest`, but that doesn't exists in phantomjs side, it exists in the client side. So technically you don't need to run it in here.
So what you need to do is that evaluate the sinon module in the client-side and also evaluate the script you have there in the client-side.
Which brings us to the following code:
var url = 'http://localhost:3000/';
// Patch the require as described in
// http://docs.casperjs.org/en/latest/writing_modules.html#writing-casperjs-modules
var require = patchRequire(require);
var casper = require('casper').create({
clientScripts: [
// The paths have to be relative to the directory that you run the
// script from, this might be tricky to get it right, so play with it
// and try different relative paths so you get it right
'node_modules/sinon/pkg/sinon.js',
'node_modules/sinon/pkg/sinon-server-1.7.3.js'
]
});
casper.test.begin('integration',1,function suite(test){
casper.start(url,function start(){
test.assertHttpStatus(200,'http status is 200');
casper.evalute(function () {
var server = sinon.fakeServer.create()
server.respondWith("GET", "/login",
[200, { "Content-Type": "application/json" },'{"id": 12}']);
});
});
casper.run(function run(){
test.done();
});
});
Note that I didn't include the call to `var sinon = require('./node_modules/sinon');`, since it wasn't needed anymore as we are evaluating sinon on client side.
Problem
How can I use Sinon with CasperJS? Here is the basic test file I am using: ``` var url = 'http://localhost:3000/'; var sinon = require('sinon'); var server = sinon.fakeServer.create(); server.respondWith("GET", "/login", [200, { "Content-Type": "application/json" },'{"id": 12}']); casper.test.begin('integration',1,function suite(test){ casper.start(url,function start(){ test.assertHttpStatus(200,'http status is 200'); }); casper.run(function run(){ test.done(); }); }); ``` Then this script is called like this: ``` casperjs test integration.js ``` Here is the version information: ``` CasperJS version 1.1.0-DEV at /usr/local/Cellar/casperjs/1/libexec, using phantomjs version 1.9.1 ``` The next step would be to fill in a login modal and submit, which performs an ajax query. I want to mock out jQuery's `$.ajax` method. The problem is I get this error: "CasperError: Can't find module sinon". But Sinon is installed both globally and locally, and that exact require line works fine in node interactive mode. Can someone please post or point me in the direction of an example where Sinon is being used with CasperJS? It doesn't specifically have to do ajax mocking. Any usage would be fine.