【问题标题】:Error: Expected undefined to equal in karma错误:预期未定义等于业力
【发布时间】:2020-02-13 13:27:33
【问题描述】:

测试用例文件

describe('homeController', function() {
beforeEach(module('moduleInjectionApp'));

var $controller;
var $rootScope;
beforeEach(inject(function(_$controller_, _$rootScope_) {
    $controller = _$controller_('homeController', {'$scope': scope});
    $rootScope = _$rootScope_;
}));

describe('$scope.ID', function() {
    it('Check the scope object', function() {
        var $scope = {};
        expect($scope.ID).toEqual(5);
    });
  });
});

控制器文件

 angular.module('moduleInjectionApp').controller('homeController', homeController)
 homeController.$inject = ["$scope", "$rootScope", "$location"];
 function homeController($scope, $rootScope, $location) {
     console.log("entered homeController")
     $scope.ID = 5;
     $rootScope.loginObj = JSON.parse(localStorage.getItem('login_data'));
 }

错误

Error: Expected undefined to equal 5.
        at <Jasmine>
        at UserContext.<anonymous> (WebContent/test/home/homeSpec.js:14:31)
        at <Jasmine>

Chrome 75.0.3770 (Windows 10.0.0):执行 1 of 1 (1 FAILED) (0.036 secs / 0.012 secs) 总计:1 次失败,0 次成功

【问题讨论】:

  • 我认为范围变量无法从控制器文件访问到测试用例文件。如果我做错了,请告诉我。
  • 你声明了var $scope = {},你怎么能指望ID在你初始化为空对象时被创建
  • 好的。所以现在我把它改成了 var scope;
  • 但仍然出现同样的错误。
  • 看看我的回答,如果有帮助,请告诉我。 \

标签: javascript angularjs unit-testing jasmine karma-jasmine


【解决方案1】:

试试

describe('homeController', function() {
    beforeEach(module('moduleInjectionApp'));

    var $controller;

    beforeEach(inject(function(_$controller_){
              $controller = _$controller_;
    }));

    describe('$scope.ID', function() {
        it('Check the scope object', function() {
            var $scope = {};
            var controller = $controller('homeController', { $scope: $scope });
            expect($scope.ID).toEqual(5);
        });
    });
});

当您声明var $scope = {}; 时,您将始终将$scope.ID 视为未定义。你需要做的

var $scope = { ID: 5}

无论如何,在单元测试中,您不会创建一些值,然后对其进行expect 断言。您验证已定义或已修改的值。在这里你试图声明然后输入expect(它总是会通过)

【讨论】:

  • 试过这段代码并得到错误` TypeError: $controller is not a function at at UserContext. (WebContent/test/home/homeSpec.js:52:20) at 错误:在 Chrome 75.0.3770 (Windows 10.0.0) 的 UserContext. (WebContent/test/home/homeSpec.js:56:27) 的 预期未定义等于 5:执行 1 of 1 (1 FAILED) (0.02 secs / 0.011 secs)`
  • 然后我在 beforeEach 函数中将 $controller 作为参数传递,然后得到错误 ``` 错误:预期未定义等于 5。在 UserContext. (WebContent/test/home/ homeSpec.js:56:27) 在 Chrome 75.0.3770 (Windows 10.0.0): 执行 1 of 1 (1 FAILED) (0.027 secs / 0.014 secs) TOTAL: 1 FAILED, 0 SUCCESS```跨度>
  • 错误:[$injector:modulerr] errors.angularjs.org/1.7.8/$injector/…) 在 WebContent/test/angular.min.js:7:168 在 Object.fb [作为注入器] (WebContent/test/angular.min .js:46:460) 在 UserContext.WorkFn (WebContent/test/angular-mocks.js:3449:52) 在 错误:预期未定义等于 5. 在 在 UserContext. (WebContent /test/home/homeSpec.js:56:27)
  • 这是我在 karma.config.js 中添加所有依赖项后的最新错误
  • 错误:在 在 UserContext. (WebContent/test/home/homeSpec.js:55:27) 在 Chrome 75.0.3770 ( Windows 10.0.0): 1 of 1 (1 FAILED) (0.05 secs / 0.031 secs) TOTAL: 1 FAILED, 0 SUCCESS
猜你喜欢
  • 1970-01-01
  • 2019-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-04
  • 2017-12-06
相关资源
最近更新 更多