2017-03-08 305 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路由。 –

相关问题