busterjs / buster

Abandoned - A powerful suite of automated test tools for JavaScript.
http://docs.busterjs.org
Other
448 stars 37 forks source link

useFakeServer() returns null object? #433

Closed akaias closed 9 years ago

akaias commented 9 years ago

hi there,

according to the documentation i wanted to create a testcase which mocks my jQuery.ajax call, but it fails without being mocked.

my testcase: (function(){ buster.testCase('DateInformationController', { testIndexAction: { setUp: function() { this.server = this.useFakeServer(); this.controller = new DateInformationController(); }, testIndex: function() { this.controller.indexAction(); assert.equals(this.server.requests.length, 1); } } } }()); Error: DateInformationController testIndexAction testIndex TypeError: Cannot read property 'requests' of null TypeError: Cannot read property 'requests' of null at Object.buster.testCase.testIndexAction.testIndex

dwittner commented 9 years ago

@akaias, could you please also provide the content of your buster.js configuration file?

akaias commented 9 years ago

@dwittner here is the buster.js

the contents of the requiredLibs.json is a custom build list of our internal js libaries, since they are not 100% node style, but all other unit tests work perfectly, including coverage

requiredLibs.json contents look like: { "libs": [ /* long list of js files_/ ], "sources":[ "dashboard/public/dashboard/js/dashboard.js.d/_/.js" ], "tests":[ "dashboard/test/public/dashboard/*_/_Test.js" ] }

one part of the jslibs prepares some basic dom setup

var jsdom = require('jsdom').jsdom, markup = [ '<!DOCTYPE html>', '', '', '', '', 'Test page markup', '', '', '

', '', '' ];

var doc = jsdom(markup.join('')); global.window = doc.parentWindow; global.navigator = global.window.navigator; if (typeof global.navigator === 'undefined') { global.navigator = { userAgent: "node-js" }; } document = global.document; window = global.window; navigator = global.navigator;

window.googletag = {}; window.location = { protocol: 'http:' };

global.jQuery = mzNodeRequire('jQuery'); // load jquery 1.11 MZ = mzNodeRequire('MZ'); // our own library basically an "autoloader" MZ.devMode = false; MZ.testMode = true; ko = mzNodeRequire('ko'); // knockout js

buster = require('buster');

assert = buster.referee.assert; refute = buster.referee.refute;

buster.js content:

PROJECTS_PATH = "../../";

var config = module.exports;

var libs = require('../tmp/requiredLibs.json');

config["dashboard test cases"] = { rootPath: PROJECTS_PATH, environment: "node", libs: libs.libs, sources: libs.sources, tests: libs.tests, 'buster-istanbul': { outputDirectory: "../dashboard/docs/logs/cobertura", format: "cobertura", silent: true }, extensions: [ require('buster-sinon'), require('buster-istanbul') ] };

dwittner commented 9 years ago

Why have you specified "node" for environment? You want to mock ajax calls, thus it should be "browser".

akaias commented 9 years ago

The tests run via commandline on our CI server, without attached browsers, that's why i chose "node" as environment.. But i'll try "browser" to run my ajax mocks and see if i can get them running. Thank you for pointing me to this

dwittner commented 9 years ago

If you are looking for an easy way to test browsers in an CI environment with Buster.JS, have a look at the new module buster-ci. Even though it is very new, it might already be useful for your needs.

akaias commented 9 years ago

nice one, i'll give it a try during next week thanks

dwittner commented 9 years ago

@akaias, can this issue be closed?

kkirby commented 9 years ago

Is there anyway to have this work on the CL? I'm figured one benefit of mocking XMLHttpRequest would be that it could work on the command line. Is this not the case?

kkirby commented 9 years ago

Never mind. It's a limitation of Sinon. https://github.com/cjohansen/Sinon.JS/issues/125