Protractor - injection is not detected when testing angular services

I am writing a very simple test specification in the transporter to get the gist of the framework. Everything worked fine until I tried to test the services. I usually called injectto get a dependency, but now I get an error inject is not defined. Here is the code for my test spec:

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);
        });
    });   
});  
+4
source share
2 answers

In general, the protractor is designed to be tested from end to end. You must use karma to test services.

Otherwise, theoretically, you can access your services as follows:

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

And there is an example:

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

+4

, angular . karma-require, , "karma init".

0

All Articles