AngularJS Jasmine测试:TypeError:'undefined'不是对象 [英] AngularJS Jasmine test: TypeError: 'undefined' is not an object

查看:154
本文介绍了AngularJS Jasmine测试:TypeError:'undefined'不是对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Angular的新手,并从 angularjs jasmine tests:找不到变量vm
我在角度测试中遇到TypeError而不确定问题是什么。这是我的测试:

 (function(){
'use strict';
describe('测试DeliveriesController',function(){

beforeEach(module('app.deliveries'));

describe('测试交付控制器',函数(){
var vm,controller;

beforeEach(inject(function($ controller,$ rootScope){
vm = $ rootScope。$ new();
controller = $ controller ('DeliveriesController',{$ scope:vm});
}));

afterEach(function(){
vm = undefined;
controller = undefined ;
});

describe('priority length',function(){
it('它应该测试优先级长度',函数(){
expect (vm.priorities.length).toBe(0);
});
});
});

});

})();

我得到的错误如下:

  PhantomJS 1.9.8(Mac OS X 0.0.0)测试DeliveriesController测试交付控制器优先级长度它应该测试优先级长度FAILED 
错误:[$ injector:unpr]未知提供者:DeliveriesServiceProvider< - DeliveriesService< - DeliveriesController
http://errors.angularjs.org/1.3.20/$injector/unpr?p0=DeliveriesServiceProvider%20%3C-%20DeliveriesService%20%3C-%20DeliveriesController
at /Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4031
at getService(/ Users / rgoti / ingestion / external-ingestion / app / public /bower_components/angular/angular.js:4178)
at /Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4036
at getService(/ Users / rgoti / ingestion / external-ingestion / app / public / bower_components / angular / angular.js:4178)
at invoke(/ Users / rgoti / ingestion / ex ternal-ingestion / app / public / bower_components / angular / angular.js:4210)
at instantiate(/Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4227)
at /Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:8524
at / Users / rgoti / ingestion / external-ingestion / app / public / bower_components /angular-mocks/angular-mocks.js:1916
atUsers/rgoti/ingestion/external-ingestion/app/public/src/app/deliveries/deliveries.spec.js:12
at work(/Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4219)$ f $ b at workFn(/ Users / rgoti / ingestion / external-ingestion / app / public / bower_components /angular-mocks/angular-mocks.js:2475)
undefined
TypeError:'undefined'不是对象(评估'vm.priorities.length')
at / Users / rgoti /ingestion/external-ingestion/app/public/src/app/deliveries/deliveries.spec.js:23


解决方案

礼貌:来自@StubbbOrn的评论:



你能展示控制器的代码吗?这将有助于更好地理解问题。看起来DeliveriesController不仅取决于$ scope,还取决于DeliveriesService。当你实例化控制器时,你应该提供它所有的依赖项(真实的或模拟的)。



这个解决方案适用于我并且是解决方案。谢谢@StubbOrn


New to Angular and following up from my earlier post from angularjs jasmine tests: Variable vm not found I am having a TypeError in my angular tests and not sure what the problem is. Here is my test:

(function(){
'use strict';
describe('Testing DeliveriesController', function() {

    beforeEach(module('app.deliveries'));

    describe('Testing deliveries controller', function(){
        var vm, controller;

        beforeEach(inject(function($controller, $rootScope){
            vm = $rootScope.$new();
            controller = $controller('DeliveriesController', {$scope:vm});
        }));

        afterEach(function() {
            vm = undefined;
            controller = undefined;
        });

        describe('priorities length', function(){
            it('it should test priority length', function () {
                expect(vm.priorities.length).toBe(0);
            });
        });
    });

  });

})();

The error I get is as follows:

PhantomJS 1.9.8 (Mac OS X 0.0.0) Testing DeliveriesController Testing deliveries controller priorities length it should test priority length FAILED
Error: [$injector:unpr] Unknown provider: DeliveriesServiceProvider <- DeliveriesService <- DeliveriesController
http://errors.angularjs.org/1.3.20/$injector/unpr?p0=DeliveriesServiceProvider%20%3C-%20DeliveriesService%20%3C-%20DeliveriesController
    at /Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4031
    at getService (/Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4178)
    at /Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4036
    at getService (/Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4178)
    at invoke (/Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4210)
    at instantiate (/Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4227)
    at /Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:8524
    at /Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular-mocks/angular-mocks.js:1916
    at /Users/rgoti/ingestion/external-ingestion/app/public/src/app/deliveries/deliveries.spec.js:12
    at invoke (/Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular/angular.js:4219)
    at workFn (/Users/rgoti/ingestion/external-ingestion/app/public/bower_components/angular-mocks/angular-mocks.js:2475)
undefined
TypeError: 'undefined' is not an object (evaluating 'vm.priorities.length')
    at /Users/rgoti/ingestion/external-ingestion/app/public/src/app/deliveries/deliveries.spec.js:23

解决方案

Courtesy: comment from @StubbbOrn:

Could you show controller's code? It would help to understand problem better. Looks like DeliveriesController depends not only on $scope but also on DeliveriesService. When you instantiate controller you should provide it all dependencies (either real or mocked ones).

This solution worked for me and was the solution. Thanks @StubbOrn

这篇关于AngularJS Jasmine测试:TypeError:'undefined'不是对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆