简体   繁体   English

Angularjs - Jasmine范围变量在测试中未定义

[英]Angularjs - Jasmine scope variable undefined in test

I am trying to test my angularjs code. 我正在尝试测试我的angularjs代码。

my controller is as bollow. 我的控制器像bollow一样。

'use strict';
(function() {
     angular.module('myApp')
    .controller('TestController', function($scope, $http, Auth, myService, $state) {
        try{
              $scope.testVar  = "Hello";
        }catch(e){
           console.log(e);
        }
     });
});

My test code is below: 我的测试代码如下:

'use strict';

 describe('Controller: TestController', function () {

     beforeEach(module('myApp'));

     var TestController, scope;

     beforeEach(inject(function ($controller, $rootScope) {
        scope = $rootScope.$new();

        TestController = $controller('TestController', {
             $scope: scope
        });
      }));

     it('should testVar to be defined', function () {
       expect(scope).toBeDefined();
       expect(scope.testVar).toBeDefined();
      });
});

When i run it, it failed test as Expected undefined to be defined. 当我运行它时,它未通过测试,因为Expected undefined to be defined.

I don't know what i am doing wrong? 我不知道我做错了什么?

I already checked many post on SO but did't get it resolved yet. 我已经在SO上查了很多帖子,但还没有得到解决。

Possibly issue is related to your variable declaration. 可能问题与您的变量声明有关。 You have declared ManualBookingController, but then you use TestController for the $controller line. 您已经声明了ManualBookingController,但是您将$ Control行使用TestController。 Change ManualBookingController to TestController, see if that works. 将ManualBookingController更改为TestController,看看是否有效。

I took your code almost as is, put it in a fiddle , test passes just fine. 我把你的代码几乎按原样,把它放在一个小提琴里 ,测试通过就好了。 What I think might be missing in your case is you have some dependencies that are present in your controller but not mocked while creating the mock controller using $controller in your test suite. 在您的情况下,我认为可能缺少的是您在控制器中存在一些依赖项,但在使用测试套件中的$controller创建模拟控制器时不会进行模拟。 I removed them from your original controller (for now) and it worked fine. 我从原来的控制器中删除了它们(现在)并且工作正常。

And in order to mock $state (provider) and other services, you can see this same example fiddle passing with dependencies injected and read about underscore wrapping . 并且为了模拟$state (提供者)和其他服务,你可以看到这个相同的例子小提琴传递依赖注入和读取下划线包装


Also, keep in mind that sharing exact details on your question is very important. 另外,请记住,在您的问题上分享确切的详细信息非常重要。 When I ran your test suite with your exact code, it first gave me following error: 当我使用您的确切代码运行您的测试套件时,它首先给了我以下错误:

Error: [$injector:unpr] Unknown provider: $stateProvider <- $state http://errors.angularjs.org/1.2.9/ $injector/unpr?p0=%24stateProvider%20%3C-%20%24state Error: [$injector:unpr] Unknown provider: $stateProvider <- $state http://errors.angularjs.org/1.2.9/ $ injector / unpr?p0 =%24stateProvider%20%3C-%20%24state

..which is not mentioned in your question at all. ..在你的问题中根本没有提到。 You mentioned only the next error Expected undefined to be defined which was very common and less-useful to imagine what's going on. 你只提到了下一个错误Expected undefined to be defined ,这是非常常见的,并且无法想象发生了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM