使用ngMockE2E的模拟httpBackend量角器不起作用

时间:2016-03-14 10:58:58

标签: angularjs protractor ngmocke2e

完成此处所述的基本指南https://www.npmjs.com/package/ng-mock-e2e

但仍然正在调用正常的REST调用。

'use strict'

var HttpBackend = require('httpbackend');
var backend = null;
var Injector = require('./helpers/injector');
var ngMockE2E = require('ng-mock-e2e');
var $httpBackend = ngMockE2E.$httpBackend;

describe("Login", function () {

    var loginJsonStub,
        loginPage = require('./pageObjects/LoginPage.js');

    beforeEach(function () {

        browser.get('http://localhost:9001/#');

        var injector = new Injector();
        injector.get('loginJson').then(function (result) {
            loginJsonStub = result;
        })
    });

    beforeEach(function () {
        ngMockE2E.addMockModule();
        ngMockE2E.addAsDependencyForModule('myApp');
        ngMockE2E.embedScript('../../app/bower_components/angular-mocks/angular-mocks.js');
    });

    afterEach(function () {
        ngMockE2E.clearMockModules();
    });

    describe("Routing", function () {

        it('should redirect to answerset page immediately if only 1 project', function () {

            $httpBackend.when('POST', '/authentication/login').respond({data: 123});

            element(by.id('userName')).sendKeys('xx\\svijver');
            element(by.id('passWord')).sendKeys('password');

            //browser.pause();

            loginPage.nextButton.click();

            browser.getLocationAbsUrl();
            expect(browser.getCurrentUrl()).toContain('answersets/1');
            expect(browser.getCurrentUrl()).toBe('answersets/1');

            browser.pause();

        });
    });
});

这应该不是那么难,有人能指出我忽略的东西吗? 尝试了几个其他第三方供应商嘲笑httpBackend,但无法让它工作。也许实际应用程序中的初始$ http调用是否会推翻被模拟的那个?

- 编辑 -

根据Angular文档,将ngMockE2e依赖项添加到我的主应用程序angular.module('qApp', ['...', '...', '...', 'ngMockE2E']);会导致各种奇怪的错误:

错误:意外请求:获取http://qubus7.test.kmsgroep.com/api/localizations/en2         没有更多的要求     在$ httpBackend(angular-mocks.js:1263)

Uncaught SyntaxError: Unexpected identifier
angular.js:78 Uncaught Error: [$injector:nomod] Module 'qubusApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

         http://errors.angularjs.org/1.2.26/$injector/nomod?p0=qubusApp(anonymous function) @ angular.js:78(anonymous function) @ angular.js:1677ensure @ angular.js:1601module @ angular.js:1675(anonymous function) @ MainController.js:4
angular.js:78 Uncaught Error: [$injector:nomod] Module 'qbs.models' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
    http://errors.angularjs.org/1.2.26/$injector/nomod?    p0=qbs.models(anonymous function) @ angular.js:78(anonymous function) @ angular.js:1677ensure @ angular.js:1601module @ angular.js:1675(anonymous function) @ loginModel.js:3

angular.js:78 Uncaught Error: [$injector:nomod] Module 'qbs.models' is not   available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

在runnin Protractor中,它突然看到对我的UI元素的引用了,这很有意义,不再是不加载的。

1 个答案:

答案 0 :(得分:0)

首先,好像你正在使用旧的独立版本的ng-mock-e2e。您应该使用角度为https://docs.angularjs.org/api/ngMockE2E

angular-mocks 中的版本

如果在项目中包含了角度模拟,则可以在测试文件中添加以下内容:

beforeEach(function () {
    // choose a unique name for your mock-module, like httpMocker
    browser.addMockModule('httpMocker', function () {
        angular.module('httpMocker', ['ngMockE2E'])
            .run(function ($httpBackend) {
                // define your routes
            });
        );
    });
});