2012-12-03 75 views
1

在我的茉莉花測試套件中,我想導航我的路由器到正確的狀態。這樣的事情:如何在燼+茉莉花模擬路由器狀態

describe("The Router", function(){ 
     beforeEach(function(){ 
      router = App.Router.create(); 
      router.transitionTo('foo.bar'); 
     } 
    }); 

但如果我這樣做,我會得到一個Cannot call method 'disconnectOutlet' of undefined錯誤。發生這種情況因爲我打電話

 bar: Ember.Route.extend({  
     connectOutlets: function(router, context){ 
      router.get('applicationController').connectOutlet('bla', 'blub'); 
     } 
     }), 

在此路由器轉換。我試圖啓動我的applicationController像

applicationController = App.ApplicationController.create(); 

但它不會改變錯誤。那麼我怎樣才能模擬到正確的路由器狀態?

回答

1

你今天怎麼用茉莉花來測試它?你是否要去瀏覽每個變更或試圖從命令行運行它,比如jasmine-phantom-node/jasmine-node?

一個例子可能如下所示。在下面的設置中,我正在測試一個視圖,但同樣的嘲笑/間諜想法適用於上面的路由器示例。

require('static/script/vendor/filtersortpage.js'); 
require('static/script/app/person.js'); 

describe ("PersonApp.PersonView Tests", function(){ 

    var sut, router, controller; 

    beforeEach(function(){ 
    sut = PersonApp.PersonView.create(); 
    router = new Object({send:function(){}}); 
    controller = PersonApp.PersonController.create({}); 
    controller.set("target", router); 
    sut.set("controller", controller); 
    }); 

    it ("does not invoke send on router when username does not exist", function(){ 
    var event = {'context': {'username':'', 'set': function(){}}}; 
    var sendSpy = spyOn(router, 'send'); 
    sut.addPerson(event); 
    expect(sendSpy).not.toHaveBeenCalledWith('addPerson', jasmine.any(String)); 
    }); 

    it ("invokes send on router with username when exists", function(){ 
    var event = {'context': {'username':'foo', 'set': function(){}}}; 
    var sendSpy = spyOn(router, 'send'); 
    sut.addPerson(event); 
    expect(sendSpy).toHaveBeenCalledWith('addPerson', 'foo'); 
    }); 

    it ("does not invoke set context when username does not exist", function(){ 
    var event = {'context': {'username':'', 'set': function(){}}}; 
    var setSpy = spyOn(event.context, 'set'); 
    sut.addPerson(event); 
    expect(setSpy).not.toHaveBeenCalledWith('username', jasmine.any(String)); 
    }); 

    it ("invokes set context to empty string when username exists", function(){ 
    var event = {'context': {'username':'foo', 'set': function(){}}}; 
    var setSpy = spyOn(event.context, 'set'); 
    sut.addPerson(event); 
    expect(setSpy).toHaveBeenCalledWith('username', ''); 
    }); 
}); 

Here是茉莉以上測試在真實的項目,如果你想與一些背景

這裏看到它是測試中的視圖(幫助作出上述茉莉測試的意義上)

PersonApp.PersonView = Ember.View.extend({ 
    templateName: 'person', 
    addPerson: function(event) { 
    var username = event.context.username; 
    if (username) { 
     this.get('controller.target').send('addPerson', username); 
     event.context.set('username', ''); 
    } 
    } 
});