English 中文(简体)
与Jasmine合著的后堡联合试验模型
原标题:Testing model binding in Backbone JS with Jasmine

我有一种观点包含一种模式。 这一观点聆听了模型的活动,一旦活动启动,将采取行动。 下面是我的法典

window.Category = Backbone.Model.extend({})

window.notesDialog = Backbone.View.extend({
  initialize: function() {
    this.model.bind("notesFetched", this.showNotes, this);
  },
  showNotes: function(notes) {
    //do stuffs here
  }
})

我想用Jasmine来测试这一点,以下是我的检验标准(它不工作)。

it("should show notes", function() {
   var category = new Category;

   var notes_dialog = new NotesDialog({model: category})

   spyOn(notes_dialog, "showNotes");
   category.trigger("notesFetched", "[]");
   expect(notes_dialog.showNotes).toHaveBeenCalledWith("[]");
})

是否有任何人知道上述检验为什么不奏效? 我发现的错误是“用[]字眼显示说明,但从来不称......”。

问题回答

在我的看法中,我做了类似的事情,但是,除非我把它添加到原型中,而且在我提出看法之前,我无法得到适当的工作。

这里,我最终的工作是:

view.js

view = Backbone.View.extend({
   initialize: function(){
      this.collection.bind("change", this.onChange, this);
   },
   ...
   onChange: function(){
      console.log("Called...");
   }
});

jasmine_spec.js

describe("Test Event", function(){
   it("Should spy on change event", function(){
      var spy = spyOn(view.prototype,  onChange ).andCallThrough()
      var v = new view( {collection: some_collection });

      // Trigger the change event
      some_collection.set();

      expect(spy).toHaveBeenCalled()
   });
});

我首先将使用<代码>至HaveBeenCalled(),在您开始工作之后,对<代码>至HaveBeenCalledWith()进行测试。

www.un.org/Depts/DGACM/index_spanish.htm

3. 修改现行试验守则如下:

it("should show notes", function() {
   var category = new Category;

   spyOn(NotesDialog.prototype, "showNotes");
   var notes_dialog = new NotesDialog({model: category})

   category.trigger("notesFetched", "[]");
   expect(notes_dialog.showNotes).toHaveBeenCalledWith("[]");
})

在你最初的法典中,你所呼吁的方法就是在约束性关闭中界定的,而你正在做的就是在笔记中。 通过将间谍移至原型,你在进行之前就替换了这种间谍,因此对封闭装置进行约束,以控制间谍而不是原始方法。

Using a spy means to replace the function you spying on. So in your case you replace the bind function with the spy, so the internal logic of the original spy will not call anymore. And thats the right way to go cause you dont wanna test that Backbones bind is work but that you have called bind with the specific paramaters "notesFetched", this.showNotes, this.

因此,如何检验。 如你所知,每个间谍都有<代码>至HaveBeenCalledWith(标记)方法。 在你看来,它应当这样做:

expect(category.bind).toHaveBeenCalledWith("notesFetched", category. showNotes, showNotes)

So how to test that trigger the "notesFetched" on the model will call your showNotes function. Every spy saves the all parameters he was called with. You can access the last one with mostRecentCall.args.

category.bind.mostRecentCall.args[1].call(category.bind.mostRecentCall.args[2], "[]");
expect(notes_dialog.showNotes).toHaveBeenCalledWith("[]");

mostRecentCall.args[1] is the the second argument in your bind call (this.showNotes). mostRecentCall.args[2] is the the third argument in your bind call (this).

由于我们测试了<代码>bind,并用你的公开方法showNotes/code>,你也可以直接打电话到贵国的公开方法showNotes,但有时通过的论点可以外部查阅,以便你能够使用所显示的方式。

你们的法典看重的是罚款,但你在描述职能及其职能方面是否做了总结?


describe("show notes", function(){
  it("should show notes", function(){
    // ... everything you already have here
  });
});

此时此刻,主想总数,但因为你没有显示我可以认为的描述功能。 如果你没有工作,那么你必须有一个描述。

You are pretty close ;) spyOn replaces the function with your spy and returns you the spy. So if you do:

   var dialog_spy = spyOn(notes_dialog, "showNotes");
   category.trigger("notesFetched", "[]");
   expect(dialog_spy).toHaveBeenCalledWith("[]");

work work!





相关问题
Selenium not working with Firefox 3.x on linux

I am using selenium-server , selenium rc for UI testing in my application . My dev box is Windows with FireFox 3.5 and every thing is running fine and cool. But when i try to run selenium tests on my ...

Best browser for testing under Safari Mobile on Linux?

I have an iPhone web app I m producing on a Linux machine. What s the best browser I can use to most closely mimic the feature-limited version of Safari present on the iPhone? (It s a "slimmed down" ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Is there any error checking web app cralwers out there?

Wondering if there was some sort of crawler we could use to test and re-test everything when changes are made to the web app so we know some new change didn t error out any existing pages. Or maybe a ...