2015-04-16 2 views
0

Я новичок в JavaScript и тестировании. Моя задача состоит в том, чтобы проверить эту функцию в модуле RequireJS:TypeError при тестировании функции JavaScript с Jasmine

editProfile: function() { 
    this.checkForReload(); 

    var editProfileView = new EditProfileView(); 
    this.changeView(editProfileView); 

    //needs to be called separately since whe it's passed as parameter view to change is replaced with sidebarView 
    editProfileView.generateConsistentElements(this.sidebarView, 'sidebar'); 

    //footer 
    editProfileView.generateConsistentElements(this.footerView, 'footer'); 
    }, 

Я написал этот тест с Жасмин:

describe("function editProfile", function(){ 
     it ("exists", function(){ 
      expect(router.editProfile).toBeDefined(); 
     }); 

     it ("checks for reload", function(){ 
      spyOn(router, "checkForReload"); 
      router.editProfile(); 
      expect(router.checkForReload).toHaveBeenCalled(); 
     }); 

     it ("changes view", function(){ 
      spyOn(router, "changeView"); 
      router.editProfile(); 
      expect(router.changeView).toHaveBeenCalled(); 
     }); 
    }); 

Но последние два теста потерпеть неудачу с этими ошибками:

TypeError: Cannot read property 'id' of null 
at BaseView.extend.render (http://localhost:63342/website/public/js/app/views/EditProfileView.js:36:41) 
at setView (http://localhost:63342/website/public/js/app/routers/DesktopRouter.js:143:39) 
at BaseRouter.extend.changeView (http://localhost:63342/website/public/js/app/routers/DesktopRouter.js:149:13) 
at BaseRouter.extend.editProfile (http://localhost:63342/website/public/js/app/routers/DesktopRouter.js:409:14) 
at Object.<anonymous> (http://localhost:63342/website/jasmine_test/jasmine-standalone-2.2.0_1/spec/DesktopRouterSpec.js:82:24) 
at attemptSync (http://localhost:63342/website/jasmine_test/jasmine-standalone-2.2.0_1/lib/jasmine-2.2.0/jasmine.js:1741:24) 
at QueueRunner.run (http://localhost:63342/website/jasmine_test/jasmine-standalone-2.2.0_1/lib/jasmine-2.2.0/jasmine.js:1729:9) 
at QueueRunner.execute (http://localhost:63342/website/jasmine_test/jasmine-standalone-2.2.0_1/lib/jasmine-2.2.0/jasmine.js:1714:10) 
at Spec.Env.queueRunnerFactory (http://localhost:63342/website/jasmine_test/jasmine-standalone-2.2.0_1/lib/jasmine-2.2.0/jasmine.js:608:35) 
at Spec.execute (http://localhost:63342/website/jasmine_test/jasmine-standalone-2.2.0_1/lib/jasmine-2.2.0/jasmine.js:346:10) 

TypeError: Cannot read property 'roles' of null 
at eval [as template] (eval at template (http://localhost:63342/website/public/js/libs/lodash/dist/lodash.js:6305:22), <anonymous>:8:30) 
at BaseView.extend.render (http://localhost:63342/website/public/js/app/views/SidebarView.js:29:28) 
at Backbone.View.extend.generateConsistentElements (http://localhost:63342/website/public/js/app/core/BaseView.js:417:46) 
at BaseRouter.extend.editProfile (http://localhost:63342/website/public/js/app/routers/DesktopRouter.js:412:25) 
at Object.<anonymous> (http://localhost:63342/website/jasmine_test/jasmine-standalone-2.2.0_1/spec/DesktopRouterSpec.js:88:24) 
at attemptSync (http://localhost:63342/website/jasmine_test/jasmine-standalone-2.2.0_1/lib/jasmine-2.2.0/jasmine.js:1741:24) 
at QueueRunner.run (http://localhost:63342/website/jasmine_test/jasmine-standalone-2.2.0_1/lib/jasmine-2.2.0/jasmine.js:1729:9) 
at QueueRunner.execute (http://localhost:63342/website/jasmine_test/jasmine-standalone-2.2.0_1/lib/jasmine-2.2.0/jasmine.js:1714:10) 
at Spec.Env.queueRunnerFactory (http://localhost:63342/website/jasmine_test/jasmine-standalone-2.2.0_1/lib/jasmine-2.2.0/jasmine.js:608:35) 
at Spec.execute (http://localhost:63342/website/jasmine_test/jasmine-standalone-2.2.0_1/lib/jasmine-2.2.0/jasmine.js:346:10) 

Кажется, что ошибка возникла перед оператором expect. Тесты даже не удались, когда я прокомментировал высказывания ожидающих. Возможно ли какой-либо объект в функции editProfile, который не может быть вызван? Или проблема с Жасмин пытается что-то сделать? Как я могу решить это из тестового кода? Спасибо заранее!

ответ

0

Я решил проблему, добавив глобальный флаг: TEST. Мои тестовые функции выглядят как это сейчас:

it ("checks for reload", function(){ 
    spyOn(router, "checkForReload"); 
    TEST = true; 
    router.editProfile(); 
    TEST = false; 
    expect(router.checkForReload).toHaveBeenCalled(); 
}); 

it ("changes view", function(){ 
    spyOn(router, "changeView"); 
    TEST = true; 
    router.editProfile(); 
    TEST = false; 
    expect(router.changeView).toHaveBeenCalled(); 
}); 

И я изменил функцию generateConsistentElements(), что вызвало проблемы:

generateConsistentElements: function (...) { 
    if(!TEST){ 
     // code 
    } 
}, 

Теперь я могу выполнить editProfile() без ошибок, но, возможно, это не лучший способ решить эту проблему. Кто-нибудь знает лучшее решение?

1

Где вы объявляете router? Попробуйте объявить его в функции beforeAll.

describe(... 
    beforeAll(function() { 
     // declare router 
    }); 

    // .... it .... 
    // .... it .... 
    // .... it .... 
); 
Смежные вопросы