Question

I'm completely new to jasmine / karma and having a few problems. Test run fine when i run a simple

describe('JavaScript addition operator', function () {
    it('adds two numbers together', function () {
        expect(1 + 2).toEqual(3);
    });
});

test, it passes and is ok but I want to now start testing functions in my othe files, Naturally I started with the most difficult one and fell flat on my face. I then worked my way down the list / errors until I got to the most basic of functions, one that rounds a number to a decimal place by taking in the params. It gave me an undefined error, so I then thought I'd move the addition test that worked into that file just to see if I was being special and it doesn't work either so can someone please tell me what I'm doing wrong? :)

I've been hunting online for quite a while and haven't yet found an idiots guide. I'd like to be able to test my functions by passing in params that I'd expect. For ex:

describe("round results", function(){

    var myFunc = roundresult(a,b);
    var a = 99.923232;
    var b = 1;

    it("rounds the result to dec places", function(){
        expect(myFunc(a,b).toEqual(99.9));
    });
});

where this is the function:

function roundResult(value, places) {
   var multiplier = Math.pow(10, places);
   return (Math.round(value * multiplier) / multiplier);
}

the error:

ReferenceError: roundresult is not defined
at null.<anonymous> (http://localhost:9878/base/tests/objectTests.js:98:18)
at jasmine.Env.describe_ (http://localhost:9878/absolute/usr/local/lib/node_modules/karma-jasmine/lib/jasmine.js:884:21)
at jasmine.Env.describe (http://localhost:9878/absolute/usr/local/lib/node_modules/karma-jasmine/lib/jasmine.js:869:15)
at describe (http://localhost:9878/absolute/usr/local/lib/node_modules/karma-jasmine/lib/jasmine.js:629:27)
at http://localhost:9878/base/tests/objectTestTests.js:96:1 

Any help is hugely appreciated, thanks.

Was it helpful?

Solution

In your describe block, roundresult should be roundResult.

SOLVED: the order in which you require your files determines whether a statement has been defined by the time you try to invoke it. Use plnkr.co to host a sample with multiple files.

OTHER TIPS

expect(myFunc(a,b).toEqual(99.9)); // incorrect, should be as mentioned below

expect().toEqual();

And you have called the function and than initialize variables, which is also incorrect:

var myFunc = roundresult(a,b);

var a = 99.923232;

var b = 1;

Please have a look how I have done :

// I have created test.js

describe("round results", function(){ var a = 99.923232;

var b = 1; var myfunc = roundResult(a,b);

it("rounds the result to dec places", function(){
    expect(myfunc).toEqual(99.923232);
}); 

});

// main.js

function roundResult(value, places) {
var multiplier = value * places;
return multiplier;
}

I hope it will resolve your error.

Thanks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top