Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
183
rated 0 times [  185] [ 2]  / answers: 1 / hits: 31035  / 12 Years ago, thu, march 8, 2012, 12:00:00

Suppose I have a function as follows



function fun1(a) {
var local_a = a;
local_a += 5;
return local_a/2;
}


Is there a way to test for the value of local_a being what it should be (for example in the first line of code)? I'm a bit new to Jasmine, so am stuck. Thanks in advance.


More From » jasmine

 Answers
7

Not really. You can do the following though:



Test the result of fun1():



expect(fun1(5)).toEqual(5);


Make sure it's actually called (useful if it happens through events) and also test the result:



var spy = jasmine.createSpy(window, 'fun1').andCallThrough();
fire_event_calling_fun1();
expect(spy).toHaveBeenCalled();
expect(some_condition);


Really reproduce the whole function inspecting intermediate results:



var spy = jasmine.createSpy(window, 'fun1').andCallFake(function (a) {
var local_a = a;
expect(local_a).toEqual(a);
local_a += 5;
expect(local_a).toEqual(a+5);
return local_a/2;
});
fun1(42);
expect(spy).toHaveBeenCalled();

[#86985] Wednesday, March 7, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarvisjovannia

Total Points: 127
Total Questions: 123
Total Answers: 101

Location: Netherlands
Member since Mon, Jun 7, 2021
3 Years ago
;