在茉莉花中没有检测到模拟角度服务?

时间:2014-03-26 07:22:52

标签: angularjs service jasmine

我刚开始使用Jasmine进行测试,我在为AngularJS应用程序编写的服务编写测试时遇到了问题。

这是我的测试代码:

describe('AccountCtrl', function () {


    beforeEach(module('zamolxian.my_account'));


    var mockCountryService = {

        //console:log("hello")
        //countryList: {"name": "Zimbabwe", "cca2": "ZW", "callingCode": ["263"]}

         checkPhone: function (countryName, phoneNumber) {
         if (phoneNumber.length == 10 && countryName.length > 2) {
         return true;
         }
         return false;
         }
    };



it('should be able to call the check phone from the the country service', function () {
        inject(function ($rootScope, $controller, mockCountryService) {
            var scope = $rootScope.$new;


            var ctrl = $controller('AccountCtrl', {
                $scope: scope,
                countryListing: mockCountryService
            });
            console.log(countryListing.checkPhone("RO","235556789"));
            expect('1').toBeDefined();

        });
    });
});

我得到的错误是:

Chrome 32.0.1700 (Linux) AccountCtrl should be able to call the check phone from the the country service FAILED
        Error: [$injector:unpr] http://errors.angularjs.org/1.2.15-build.20+sha.d4ac254/$injector/unpr?p0=mockCountryServiceProvider%20%3C-%20mockCountryService
            at Error (native)
            at /var/www/Zamolxian/apps/source/vendor/angular/angular.min.js:6:471
            at /var/www/Zamolxian/apps/source/vendor/angular/angular.min.js:32:125
            at Object.c [as get] (/var/www/Zamolxian/apps/source/vendor/angular/angular.min.js:30:200)
            at /var/www/Zamolxian/apps/source/vendor/angular/angular.min.js:32:193
            at c (/var/www/Zamolxian/apps/source/vendor/angular/angular.min.js:30:200)
            at Object.d [as invoke] (/var/www/Zamolxian/apps/source/vendor/angular/angular.min.js:30:417)
            at workFn (/var/www/Zamolxian/apps/source/vendor/angular-mocks/angular-mocks.js:2143:20)
            at window.inject.angular.mock.inject (/var/www/Zamolxian/apps/source/vendor/angular-mocks/angular-mocks.js:2129:37)
            at null.<anonymous> (/var/www/Zamolxian/apps/source/src/app/my_account/my_account.spec.js:49:9)
        Error: Declaration Location
            at window.inject.angular.mock.inject (/var/www/Zamolxian/apps/source/vendor/angular-mocks/angular-mocks.js:2128:25)
            at null.<anonymous> (/var/www/Zamolxian/apps/source/src/app/my_account/my_account.spec.js:49:9)
Chrome 32.0.1700 (Linux): Executed 5 of 5 (1 FAILED) (0.411 secs / 0.037 secs)

有没有人建议如何继续?

1 个答案:

答案 0 :(得分:1)

由于您在mockCountryService函数中定义describe,您可以将其注入it测试用例,它将被封装在函数的闭包中< / p>

it('should be able to call the check phone from the the country service', function () {
    inject(function ($rootScope, $controller) {...