Jasmine failing with Uncaught Reference Error - javascript

I am trying to get a project to work properly with Jasmine. I am using the project that I downloaded from here. I added another spec file, PatientSpec.js:
describe('Patient :: Create', function() {
it("Must note be null", function() {
require(['models/Patient'], function(Patient) {
var patient1 = new Patient();
expect(patient).toBeDefined();
});
});
});
You see that my var is named patient1 and I am running the expectation on the variable name patient. When I look at my index.html, all of my tests are passing, and this is obviously not defined. I pull up my console and I here is my error:
What would cause this error? Why does it fail silently?

It fails silently cause the error happens in the callback of your require call not in your test. So when the error is thrown after your test has finished. You have to run your test inside of the callback:
require(['models/Patient'], function(Patient) {
describe('Patient :: Create', function() {
it("Must note be null", function() {
var patient1 = new Patient();
expect(patient).toBeDefined();
});
});
});
Take a look at this SO do understand how to test requireJs modules

Related

Dynamically build tests in mocha - TypeError: test.retries is not a function

I'm trying to loop through some options and build tests based on those options in mocha. I've set up a simple proof of concept for making dynamic tests based roughly on this gist: https://gist.github.com/cybertk/fff8992e12a7655157ed
I keep getting the error: "TypeError: test.retries is not a function" when I run dynamicSuite.addTest(). I cannot figure out what is causing the error. There doesn't seem to be much documentation for this method of building tests in mocha.
Here's the code:
var dynamicSuite = describe('dynamic suite', function() {
this.timeout(10000);
before( function (done) {
var a = ['a', 'b', 'c'];
for(let item of a){
dynamicSuite.addTest(new common.Mocha.Test('test' + item, function(done){
done();
}));
}
done();
});
it('this is needed to make sure tests run', function (done) {
done();
});
after(function(done) {
done();
});
});//end describe test block
Test readability is important. Consider whether programatically generating tests compromises the readability of the tests and/or increases the chances that the tests contain bugs.
That said, this should work:
describe('dynamic suite', function(){
['a','b','c'].forEach(function(letter, index){
it('should make a test for letter' + letter, function(done){
// do something with letter
done();
});
});
});
You are currently adding tests in the beforeEach block, which will execute once per test in the file. So if you added another test in the file all of tests would get duplicated.
The code above works because declaring a test is just executing a function (it(name, test)), so we can just iterate over each input and execute the it function.

When unit-testing my angularJS application with Jasmine, $interval is undefined

When writing unit tests for my application using Jasmine I have a $interval function which i need to test. To that extend, I am trying to inject $interval and call $interval.flush();
I have the following code (this is a stripped version)
describe("whatever", function () {
var $interval;
beforeEach(function () {
angular.mock.module('whatever');
angular.mock.inject(function (_$interval_) {
$interval = _$interval_;
});
});
describe("requestIntervalFunction", function () {
$interval.flush(21000); //this gives me error "Cannot read property "flush" of undefined
});
My question is, how come that it says $interval is undefined?
You are missing your specificaton part.Thats why you getting such an error.
Add by define it function
describe("requestIntervalFunction", function() {
it("test",function(){
$interval.flush(21000); //this gives me error "Cannot read property "flush" of undefined
})
});

emberjs - Testing - click on a link with a link-to helper

I try to implement some tests on my app.
When i do a click(".on-action-element")it works very well, but when i try to do same on an element built with the {{#link-to}}helper i have this error
Uncaught Error: Assertion Failed: You have turned on testing mode, which disabled the
run-loop's autorun.
You will need to wrap any code with asynchronous side-effects in an
Ember.run
I 've tried to do :
it('can click on link', function (done) {
Ember.run(function() {
click('a:contains("demo")');
});
});
but with the same result. What i miss ?
All code in setup should be wrapped into Em.run
setup: function () {
Em.run(function () {
//code to prepare environment
});
}

Jasmine jQuery: loadFixtures() "undefined"?

My Set-up
Win7/64bit
Wamp Server
SublimeText 2
What I did...
Loaded jasmine via ruby (jasmine init)
Ran rake (rake jasmine)
deleted all the default public javascripts, spec files and helper files.
added jquery and jasmine-jquery to the "helpers" directory
Opened a browser, fired up liveReload and ran the following tests...
... of which, only "readFixtures()" passed. All others fail. wtf? Please advise!
"readFixtures()" test works perfectly...
describe("test read fixtures", function() {
it("should be able to read fixtures", function() {
// expect(readFixtures()).toBeDefined();
expect(readFixtures()).toBeDefined();
});
});
"loadFixtures()" test returns "Expected undefined to be defined."
describe("test load fixtures", function() {
it("should be able to load fixtures", function() {
// expect(loadFixtures()).toBeDefined();
expect(loadFixtures()).toBeDefined();
});
});
"setFixtures()" test returns "Expected undefined to be defined."
describe("test set fixtures", function() {
it("should be able to set fixtures", function() {
// expect(setFixtures()).toBeDefined();
expect(setFixtures()).toBeDefined();
});
});
I think you are not testing what you want to here. The functions setFixtures and loadFixtures do not have a return value. What that means is that when you call setFixtures() it will always return undefined. You want to test that the functions are defined, not the functions' return values. Your tests should look like this instead:
it("should be able to set fixtures", function() {
expect(setFixtures).toBeDefined(); // Notice I took out the ()
});

Expected a spy, but got Function

I am trying to implement a test (1) for this module (2).
My purpose is to check if the collection is fetched when a particular event is triggered.
As you can see from my comment in (2) I get the message Error: Expected a spy, but got Function.
The module works but the test fails. any ideas?
(1)
// jasmine test module
describe('When onGivePoints is fired', function () {
beforeEach(function () {
spyOn(this.view.collection, 'restartPolling').andCallThrough();
app.vent.trigger('onGivePoints');
});
it('the board collection should be fetched', function () {
expect(this.view.collection.restartPolling).toHaveBeenCalled();
// Error: Expected a spy, but got Function.
});
});
(2)
// model view module
return Marionette.CompositeView.extend({
initialize: function () {
this.collection = new UserBoardCollection();
this.collection.startPolling();
app.vent.on('onGivePoints', this.collection.restartPolling);
},
// other code
});
You need to get into the actual method, which in this case is on the prototype.
describe('When onGivePoints is fired', function () {
beforeEach(function () {
spyOn(UsersBoardCollection.prototype, 'restartPolling').andCallThrough();
app.vent.trigger('onGivePoints');
});
it('the board collection should be fetched', function () {
expect(UsersBoardCollection.prototype.restartPolling).toHaveBeenCalled();
});
});
Spying on the prototype is a nice trick you can use when you can't get to the actual instance you want to spy on.
I was also getting the same issue but I resolved it by passing an argument in the function call. Then you have to write your test case like this in the it
var data = {name:"test"}
spyOn(UsersBoardCollection.prototype, "restartPolling").and.callThrough();
UsersBoardCollection.prototype.restartPolling(data);
expect(UsersBoardCollection.prototype.restartPolling).toHaveBeenCalled();
I had this bug because I had two versions of sinon loaded, or possibly i wasn't initialising sinon-jasmine correctly. When I explicitly loaded sinon and then sinon jasmine in my spec setup, it started running correctly.

Categories