javascript - 引用错误: browser is not defined - Using Karma and Jasmine

标签 javascript node.js express jasmine karma-jasmine

尝试使用 Jasmine 和 Karma 设置一些基本测试时,我收到 ReferenceError: browser is not Defined

这是我的 Karma 配置文件

// 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');

    });
});

每当我在 Karma 工具窗口中运行测试时,都会收到以下错误:

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)

我不明白为什么浏览器突然没有定义,因为我在设置 Karma 之前进行了测试(使用 WebdriverIO 和 selenium-standalone)。这些测试以相同的方式编写,并且没有关于浏览器

的错误

我也研究过,发现很多人也有同样的问题,但他们之所以遇到这个问题是因为我没有使用 Angular 的问题?

最佳答案

使用 karma,jasmine 没有像 WebdriverIO 和 selenium-standalone 一样可用的处理程序/对象作为浏览器

如果您想使用 jasmine 编写单元测试用例来测试路由,可以通过注入(inject) Angular 的 $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');

    });
});

关于javascript - 引用错误: browser is not defined - Using Karma and Jasmine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42675084/

相关文章:

javascript - MongoDB:对文档集进行设置操作,如何?

node.js - 无法在express框架中使用.then

javascript - Express/Node.js |从 glob 调用中生成新的 JSON 对象

javascript - NodeJS MongoDB 连接字符串问题

javascript - 如何在 nodejs 上使用对象 jSON 数据进行嵌套循环

javascript - 获取 iframe 标签内的所有元素

javascript - Redux-Saga 连接多个生成器存储

javascript - 将数字限制为百分比

node.js - expressjs req.body.username 未定义

javascript - nodejs获取配置文件中的当前url