jeudi 25 février 2016

Should Angular Service Execute In Unit Test

I am trying to integrate Karma and Jasmine in to my project.

I have started off with a very basic test to ensure my controller is defined and a $scope variable equals a string - which pass as expected.

My controller, also calls a service which performed a $http.get, when running my test, without any mention of a service, i get the error:

Error: Unexpected request: GET /my/endpoint/
No more request expected

Controller:

define(['module'], function (module) {
    'use strict';

    var MyController = function ($scope, MyService) {

        $scope.testScope = 'karma is working!';

        MyService.getData().then(function (data) {
            $scope.result = data.hour
        });
    };    

    module.exports = ['$scope', 'MyService', MyController ];
});

Test:

define(['require', 'angular-mocks'], function (require) {
    'use strict';

    var angular = require('angular');

    describe("<- MyController Spec ->", function () {    

        var controller, scope;

        beforeEach(angular.mock.module('myApp'));

        beforeEach(inject(function (_$controller_, _$rootScope_) {
            scope = _$rootScope_.$new();
            controller = _$controller_('MyController', {$scope: scope});  
            scope.$apply();
        }));

        it('should verify that the controller exists ', function() {
            expect(controller).toBeDefined();
        });    

        it('should have testScope scope equaling *karma is working*', function() {
            expect(scope.testScope ).toEqual('karma is working!');
        });
    });
});

Are the above errors expected?

Aucun commentaire:

Enregistrer un commentaire