Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6814439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:41:21+00:00 2026-05-26T20:41:21+00:00

My tests fails for the following reason: ReferenceError: Can’t find variable: moving_canvas_context in file

  • 0

My tests fails for the following reason:

ReferenceError: Can’t find variable: moving_canvas_context in file
(line 5)

I understand the reason the test is failing. It doesn’t understand the variable since it is defined in a separate JavaScript file. However, it is declared globally and works in reality.

How do I write a jasmine test for this clear_canvas function?

JavaScript Canvas_Actions:

(function() {
  window.Canvas_Actions = (function() {
    function Canvas_Actions() {}
    Canvas_Actions.prototype.clear_canvas = function() {
      moving_canvas_context.clearRect(0, 0, moving_canvas.width, moving_canvas.height);
      main_canvas_context.drawImage(window.background_image, 0, 0, main_canvas.width, main_canvas.height);
      return window.canvas_objects = [];
    };
    return Canvas_Actions;
  })();
}).call(this);

Jasmine Test for Canvas_Actions:

(function() {
  describe('Canvas Actions', function() {
    return describe('clear_canvas', function() {
      return it('clears the canvases and deletes all objects', function() {
        var actions;
        jasmine.getFixtures().fixturesPath = "../spec/javascript/fixtures";
        loadFixtures("canvas_fixture.html");
        actions = new Canvas_Actions();
        actions.clear_canvas();
        return expect(canvas_objects).toEqual([]);
      });
    });
  });
}).call(this);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:41:21+00:00Added an answer on May 26, 2026 at 8:41 pm

    it is declared globally and works in reality

    Well, it also needs to be declared when the test runs. So you’re probably missing a reference to the script where it is defined in the testing fixture html.

    Also, global variables are normally not a good idea, they tend to create difficult bugs. Since you’re already using jasmine as a testing framework, try to abstract the dependency on that global variable in something that you pass to your code under test. Then, use jasmine’s mocking abilities to test it.

    If you remove the global references from Canvas_Actions, it could 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 test Canvas_Actions in isolation.

    As can be noted, this code might unearth a Canvas class, and you might find out that clear_canvas belongs in there. Use the tests to guide your design, one step at a time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't figure out why the following code fails: # test.ps1 `$args: ($args) `$args
The following test fails: #!/usr/bin/env python def f(*args): >>> t = 1, -1 >>>
Using NUnit 2.2 on .NET 3.5, the following test fails when using DateTime.Equals. Why?
The following test case fails in rhino mocks: [TestFixture] public class EnumeratorTest { [Test]
I currently trying to find a solution, how to ensure that a test fails
Can someone tell me why this unit test that checks for exceptions fails? Obviously
I'm following http://ruby.railstutorial.org/chapters/following-users#code:from_users_followed_by_final and these tests fails: describe status feed do it should have
I have a JUnit test that fails because the milliseconds are different. In this
How does one write a unit test that fails only if a function doesn't
Fails: object o = ((1==2) ? 1 : test); Succeeds: object o; if (1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.