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
})
});
Related
I have a problem when I'm trying to test some javascript, I'm not sure that the code is actually testable, but then I would be pleased to know how to make it testable.
$("#Button").on('touchstart', () => testFunc());
function testFunc() {
button(true, userInfo, gameCode);
}
I want to test when the button is touched that the testFunc is called. I'm using Jasmine as my test framework.
But for whatever reason when I try to call the testFunc from Jasmine just to test that something is working I get the error
"ReferenceError: Can't find variable: testClass in file:///E:/Dokumenter/SemesterProjekt%20fun%20stuff/PRJ4Web/TankWebApplication/TankWebApplication/Test/Test.js (line 8)s"
I have made the reference to the file. So I'm not sure what is going on.
The test code that gives me the error
describe("Joystick test for functionallity",
function() {
beforeEach(function() {
});
it("All default values",
function() {
testFunc();
});
});
How do I test this code? Is it possible to do?
Your goal is to see if the function will be called upon button click, therefore, you must check the actual function call with
toHaveBeenCalled jasmine method.
Invoking the functions by itself doesn't make any sense (like in your example)
You should do something like this:
it('All default values', => {
spyOn(class.testFunc);
document.getElementById('Button').click();
expect(class.testFunc).toHaveBeenCalled();
}));
})
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
});
}
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
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 ()
});
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.