2017-03-08 304 views
1

當我試圖用Jasmine和Karma設置一些基本測試時,我得到了ReferenceError: browser is not definedReferenceError:瀏覽器未定義 - 使用Karma和Jasmine

這是我噶配置文件

// Karma configuration 
// Generated on Wed Mar 08 2017 13:29:09 GMT+0000 (GMT) 

module.exports = function(config) { 
    config.set({ 

    // base path that will be used to resolve all patterns (eg. files, exclude) 
    basePath: '', 


    // frameworks to use 
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter 
    frameworks: ['jasmine'], 


    // list of files/patterns to load in the browser 
    files: [ 
     './spec/**/*.js' 
    ], 


    // list of files to exclude 
    exclude: [ 
    ], 


    // preprocess matching files before serving them to the browser 
    // available preprocessors: https://npmjs.org/browse/keyword/karma- preprocessor 
    preprocessors: { 
    }, 


    // test results reporter to use 
    // possible values: 'dots', 'progress' 
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter 
    reporters: ['progress'], 


    // web server port 
    port: 9876, 


    // enable/disable colors in the output (reporters and logs) 
    colors: true, 


    // level of logging 
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 
    logLevel: config.LOG_INFO, 


    // enable/disable watching file and executing tests whenever any file changes 
    autoWatch: true, 


    // start these browsers 
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 
    browsers: ['Chrome'], 


    // Continuous Integration mode 
    // if true, Karma captures browsers, runs the tests and exits 
    singleRun: false, 

    // Concurrency level 
    // how many browser should be started simultaneous 
    concurrency: Infinity 
    }) 
}; 

後來才知​​道有所謂的一個測試文件,如下所示loginSpec.js

describe('login page', function() { 

    beforeEach(function(){ 
     browser().navigateTo('/'); 
    }); 

    it('should have the correct title', function() { 
     expect(browser.getTitle()).toEqual('Title'); 

    }); 
}); 

每當我跑在噶工具窗口中的測試,我得到以下錯誤:

ReferenceError: browser is not defined 
    at Object.<anonymous> (spec/loginSpec.js:11:9) 
ReferenceError: browser is not defined 
    at Object.<anonymous> (spec/loginSpec.js:15:16) 

我不明白爲什麼瀏覽器突然不defin因爲我在設置Karma之前進行了測試(使用WebdriverIO和硒 - 獨立)。這些測試是用相同的方式寫的,沒有錯誤browser

我也研究過並發現很多其他人都有同樣的問題,但他們有它,因爲與Angular的問題,我我沒有使用?

回答

0

使用業力,茉莉花沒有處理器/對象作爲browser可用像WebdriverIO和硒獨立。

如果您正在尋找編寫使用茉莉測試路線的單元測試的情況下,可以通過注射的角度$location服務按以下步驟進行:

describe('login page', function() { 

    beforeEach(angular.mock.inject((_$rootScope_, _$location_) => { 
     _$location_.path('/'); 
     _$rootScope_.$digest(); 
    })); 

    it('should have the correct title', function() { 
     expect(browser.getTitle()).toEqual('Title'); 

    }); 
}); 
+0

我之所以不能使用$位置是因爲我我沒有在我的應用中使用Angular –

+0

@CGrant好的。你能通過在單元測試中導航到'/'來告訴我你想要達到/測試什麼? – Abhishek

+0

我最初試圖測試諸如'它應該使用有效憑證登錄'以及'它不應該使用無效憑證登錄'等。但是經過大量研究,我想我已經瞭解到我正在測試我的應用錯誤的方式。我應該測試'routes'文件夾中的函數,並且似乎這樣做我應該使用** mocha **(或類似框架)與茉莉花來測試我的node.js路由。 –

相關問題