My Jasmine and RequireJS test setup was going smoothly until I encountered an issue with the context of the functions I was working on.
I am conducting Ajax tests, so my first step is to set up a listener for success and then make the service request. Within each of my it()
declarations, I perform tests based on the response from the service.
You can find my spec modules here:
// auth.js
define(['service/auth'], function(auth) {
describe('Tests "auth" service', function() {
var data;
var OPTIONS = {
CN: '147144147',
GV: '147153162'
};
auth.on.success.addOnce(function(response) {
data = response;
});
auth.request(OPTIONS);
it('"status" should exist and be "true"', function() {
waitsFor(function() {
return data !== undefined;
});
runs(function() {
expect(data['status']).toBeDefined();
expect(data['status']).toBeTruthy();
});
});
});
});
// login.js
define(['service/login'], function(login) {
describe('Tests "login" service', function() {
var data;
var OPTIONS = {
oper: 1,
codigoouemail: '101',
senha: '10151015'
};
login.on.success.addOnce(function( response ) {
data = response;
});
login.request(OPTIONS);
it('Should get the service response for user "' + OPTIONS.codigoouemail + '"', function() {
waitsFor(function() {
return data !== undefined;
});
runs(function() {
expect(data).toBeDefined();
});
});
});
});
Both modules work well individually, but I noticed that they share the same value for data
. The first module to run sets its value, and subsequent specs will have the same value. I need each module to have its own unique value so I can properly test each service response.
In theory, each module should have its own scope, but it seems like this isn't happening in practice.
Does anyone have any suggestions on how to resolve this issue?