如何为包含来自另一个类/文件的全局变量的方法编写茉莉花测试?

发布于 2024-12-16 19:46:13 字数 1323 浏览 0 评论 0原文

我的测试失败的原因如下:

ReferenceError:找不到变量:文件中的 moving_canvas_context (第 5 行)

我了解测试失败的原因。它不理解该变量,因为它是在单独的 JavaScript 文件中定义的。然而,它是在全球范围内宣布并在现实中发挥作用的。

如何为此 clear_canvas 函数编写 jasmine 测试?

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);

Canvas_Actions 的 Jasmine 测试:

(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);

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凤舞天涯 2024-12-23 19:46:13

它在全球范围内声明并在现实中发挥作用

有效的,测试运行时也需要声明。因此,您可能缺少对测试装置 html 中定义的脚本的引用。

此外,全局变量通常不是一个好主意,它们往往会产生困难的错误。由于您已经使用 jasmine 作为测试框架,因此请尝试在传递给测试代码的内容中抽象对该全局变量的依赖关系。然后,利用茉莉的嘲讽能力来测试一下。

如果从 Canvas_Actions 中删除全局引用,它可能如下所示:

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();
};

您可以使用 jasmine 模拟 canvas 参数并单独测试 Canvas_Actions

可以注意到,此代码可能发现一个Canvas类,并且您可能会发现clear_canvas属于其中。使用测试来指导您的设计,一次一步。

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.

夜深人未静 2024-12-23 19:46:13

Jordão 绝对正确,但也有一个丑陋的选择。
在 beforeEach 方法中将全局对象附加到窗口。下面的代码可能不起作用(尚未测试过),但应该足以理解如何解决这个 jasmine 全局对象问题。

(function() {
  describe('Canvas Actions', function() {
    beforeEach(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;
})();
    });
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 = window.Canvas_Actions;
    actions.clear_canvas();
    return expect(canvas_objects).toEqual([]);
  });
});
  });
}).call(this);

编辑:根据@John Henckel和@serv-inc的评论,显然可能存在错误(ReferenceError:窗口未定义)来修复它,而不是window 使用 global,例如:将 window.Canvas_Actions 更改为 global.Canvas_Actions

Jordão is absolutely right, however there's an ugly option too.
Attach your global object to the window in beforeEach method. Code below probably does not work (haven't tested it), but should be good enough to understand how to work around this jasmine global object problem.

(function() {
  describe('Canvas Actions', function() {
    beforeEach(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;
})();
    });
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 = window.Canvas_Actions;
    actions.clear_canvas();
    return expect(canvas_objects).toEqual([]);
  });
});
  });
}).call(this);

EDIT: as per comments by @John Henckel and @serv-inc apparently there might be an error (ReferenceError: window is not defined) to fix it instead of window use global like: window.Canvas_Actions change to global.Canvas_Actions

涙—继续流 2024-12-23 19:46:13

似乎 JasmineJS 使用 global财产。所以尽管@Jordão 的回答,你可以替换

window.Canvas_Actions = (function() {

global.Canvas_Actions = (function() {

It seems like JasmineJS uses the global property. So @Jordão's answer nonwithstanding, you could replace

window.Canvas_Actions = (function() {

with

global.Canvas_Actions = (function() {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文