declared globally and actually works
Well, it should also be declared when starting the test. Thus, you probably did not provide a link to the script where it is defined in the html test file.
In addition, global variables are usually not a good idea; they tend to create complex errors. Since you already use jasmine as the basis for testing, try to abstract the dependence on this global variable in what you pass into the code under test. Then use the draining powers of jasmine to test it.
If you remove global references from Canvas_Actions , it might look like this:
var Canvas_Actions = function(canvas) { this.canvas = canvas; } Canvas_Actions.prototype.clear_canvas = function(background_image) { var canvas = this.canvas; canvas.getContext().clearRect(0, 0, canvas.width, canvas.height); canvas.getContext().drawImage(background_image, 0, 0, canvas.width, canvas.height); canvas.clearObjects(); };
You can mock the canvas argument with jasmine and the Canvas_Actions test in isolation.
As you can see, this code can dig up the canvas class, and you may find that clear_canvas is clear_canvas . Use tests to follow your design step by step.
source share