【问题标题】:Failing unit test of factory with dependency in AngularJS using Jasmine & Karma使用 Jasmine 和 Karma 在 AngularJS 中对具有依赖性的工厂进行单元测试失败
【发布时间】:2014-03-17 17:00:11
【问题描述】:

我正在使用 Jasmine 测试一个 AngularJS 工厂。

我在测试具有依赖关系的工厂时遇到了困难。我已经包含了我正在测试的工厂的代码和测试代码。

问题是我遇到了错误并且测试失败了。

这是我看到的错误:

Chrome 33.0.1750 (Mac OS X 10.8.5) Testing my Test Service can get an instance of my factory FAILED
    Error: [$injector:unpr] http://errors.angularjs.org/1.2.8/$injector/unpr?p0=TestThisServiceProvider%20%3C-%20TestThisService
        at Error (native)
        at /Users/.../www/lib/angular/angular.min.js:6:449
        at /Users/.../www/lib/angular/angular.min.js:32:125
        at Object.c [as get] (/Users/.../www/lib/angular/angular.min.js:30:200)
        at /Users/.../www/lib/angular/angular.min.js:32:193
        at c (/Users/.../www/lib/angular/angular.min.js:30:200)
        at Object.d [as invoke] (/Users/.../www/lib/angular/angular.min.js:30:417)
        at workFn (/Users/.../www/lib/angular/angular-mocks.min.js:6:20731)
    Error: Declaration Location
        at window.inject.angular.mock.inject (/Users/.../www/lib/angular/angular-mocks.min.js:6:20394)
        at null.<anonymous> (/Users/.../www/tests/myapp/TestServices/controllers.tests.js:54:43)
        at /Users/.../www/tests/myapp/TestServices/controllers.tests.js:1:1
Firefox 27.0.0 (Mac OS X 10.8) Lighting Control Service can get an instance of my factory FAILED
    Error: [$injector:unpr] http://errors.angularjs.org/1.2.8/$injector/unpr?p0=TestThisServiceProvider%20%3C-%20TestThisService
    F/<@/Users/.../www/lib/angular/angular.min.js:6
    $b/l.$injector<@/Users/.../www/lib/angular/angular.min.js:32
    c@/Users/.../www/lib/angular/angular.min.js:30
    $b/p.$injector<@/Users/.../www/lib/angular/angular.min.js:32
    c@/Users/.../www/lib/angular/angular.min.js:30
    d@/Users/.../www/lib/angular/angular.min.js:30
    workFn@/Users/.../www/lib/angular/angular-mocks.min.js:6
    angular.mock.inject@/Users/.../www/lib/angular/angular-mocks.min.js:6
    @/Users/.../www/tests/myapp/TestServices/controllers.tests.js:54
    @/Users/.../www/tests/myapp/TestServices/controllers.tests.js:1

工厂测试

angular.module('myApp.Module', ["ngResource"])

.factory('TestThisService', function(WebSocketSrvc, $q, $rootScope, $log) {

    return {
        createData: function(type, msg_object, cb_URI, cbfunction) {
            // creates 
        },
        requestData: function(type, cb_URI) {
            // requests
        },
        updateData: function(type, id, msg_object, cb_URI, cbfunction) {
            // updates
        },
        deleteData: function(type, id, cb_URI, cbfunction) {
            // deletes
        }
    }
});

测试代码:

describe("My Service", function() {

  // Load your module.
  beforeEach(module('myApp.Module','myApp.ModuleTwo'));

  // Setup the mock service in an anonymous module.
  beforeEach(module(function ($provide) {
    $provide.value('WebSocketSrvc', {
        variable: 'value',

        get: function() {

            return 'test';
        },

        send: function(request, cb_URI) {

            return 'result';
        }
            // ... more functions included not displayed.
    });
  }));

  it('can get an instance of my factory', inject(function(TestThisService) {
    expect(TestThisService).toBeDefined();
  }));

});

我的问题是我需要做什么才能让我的单元测试成功通过?

【问题讨论】:

    标签: angularjs unit-testing jasmine


    【解决方案1】:

    在您提供的代码中有 2 个拼写错误,但我想这不是您的真实代码:

    • 测试代码,第 4 行:

      beforeEach(module('MyApp.Module'));
      

      应该是:

      beforeEach(module('myApp.Module'));
      
    • 第 13 行,您忘记了引用:return 'test';

    由于您得到的错误是“TestThisServiceProvider...”,我会在beforeEach 中使用$injector 来注入您的服务进行测试。就在第二个beforeEach(module(function(){...});

    beforeEach(inject(function($injector) {
        myService = $injector.get('TestThisService');
    }));
    

    我认为 $injector 负责处理依赖项(例如 ngResource 在您的情况下)。

    【讨论】:

    • 我已修正错别字。你是对的,它们实际上并没有出现在我的实际代码中,只是我输入到 Stack Overflow 的代码中的一个错误。
    • 这并不能解决问题。也许我没有提到的一件事是依赖的代码位于不同的模块中。无论如何,我不希望测试这种依赖关系。我宁愿测试它的依赖模拟。
    • 那么你必须将它包含在beforeEach(module(myApp.Module, myApp.otherModule,...));中!
    • 太棒了,它现在正在工作。必须确保在 $injector.get('SpellServiceNameCorrectly') 中正确拼写 service。感谢您的帮助。
    猜你喜欢
    • 2013-05-10
    • 2013-09-29
    • 2014-09-20
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多