量角器 - 在测试角度服务时未定义注入

时间:2013-11-06 17:15:46

标签: angularjs protractor

我在量角器中编写了一个非常简单的测试规范,以获得框架的要点。在我尝试测试服务之前,一切正常。通常我会调用inject来获取依赖项,但现在我得到inject is not defined错误。这是我的测试规范的代码:

function SingleModelPage() {

    this.getDataList = function() {
        return element.all(by.repeater('d in data'));
    };

    this.get = function() {
        browser.get('http://localhost:8080/');
        element(by.css('#ln-single-model')).click();
    };
}

describe('Single Model Page', function() {
    var page = new SingleModelPage();
    var dataService;           

    beforeEach(function() {    
        page.get();            
    });     

    // I tried to add mock module but angular is not defined as well, so
    // I couldn't call angular.module
    beforeEach(inject(function(SingleModelDataService) {
        dataService = SingleModelDataService;
    }));  

    describe('Testing Setup', function() { 
        it('should load the single model page by default', function() {
            expect(page.getDataList().count()).toEqual(1);
        });
    });

    describe('Single Model Service', function() {
        it('should contain single model data service', function() {
            //expect(dataService).not.toEqual(null);
        });
    });   
});  

2 个答案:

答案 0 :(得分:4)

通常,量角器设计用于端到端测试。对于测试服务,你应该使用业力。

否则理论上你可以像这样访问你的服务:

browser.executeAsyncScript(function(callback) {
  var service = angular.injector(['MyModule']).get('myService');
  service.query({}, function(data) {
    callback(data);
  });
}).then(function (output) {
  console.log(output);
});

还有一个例子:

https://github.com/andresdominguez/protractor-meetup/blob/master/test/e2e/api-helper.js

答案 1 :(得分:0)

这是因为您的命名空间中不存在angular。 创建一个karma-require配置,例如使用" karma init"命令。