【问题标题】:Sinon not working with Require and BackboneSinon 不使用 Require 和 Backbone
【发布时间】:2013-08-08 18:00:42
【问题描述】:

我正在关注this 教程,我正在尝试删除一个对象。这是我的代码:

define(function(require) {
  var sinon = require('sinon');
  beforeEach(function() {
    this.patientStub = sinon.stub(window, "Patient");
    this.model = new Backbone.Model({
      id: 5,
      title: "Foo"
    });
  });
});

我不断收到错误TypeError: Attempted to wrap undefined property Patient as function。关于 SO 或在线的信息很少,而且都没有帮助。我不知道这是否有什么不同,但这是我的目录结构。

Patient ->  index.html
            karma.conf.js
            css -> 
            js -> 
            test -> 

我试图模拟的对象位于 js/models/Patient.js。拜托,任何帮助都会很棒。

【问题讨论】:

    标签: javascript backbone.js require sinon


    【解决方案1】:

    问题是您在测试运行时试图模拟一个不存在的函数(意味着undefined)。您应该在 index.html 中加载该文件,否则它将不会在全局命名空间中可见。在测试中用 requireJs 加载 sinon 也不常见,通常你也会把它放在你的 index.html

    【讨论】:

    • 我只是在学习 TDD 并尝试开始我的设置。我的理解是我需要在我的测试 index.html 中加载我的模型和视图。如果我这样做,那么它将与Sinon一起使用。你知道我可以在哪里获得更多关于诗乃和 Jasmine/Karma/Require 的信息吗?我知道这是一种专门的设置。
    猜你喜欢
    • 2012-12-02
    • 1970-01-01
    • 2013-03-19
    • 2015-11-10
    • 2014-07-12
    • 2012-12-09
    • 2016-08-04
    • 2014-01-11
    • 2023-03-28
    相关资源
    最近更新 更多