0

angularのジャスミンテストには次のものがあります...

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

  beforeEach(module(function ($provide, Config) {

    reservationCallerJquery = {
      reserveBooking: function (reservationRequestDto, reserveBookingSuccess) {
        doJqueryPost(reservationRequestDto.item,
          "I need a value from my config class",
          reserveBookingSuccess);
      }

    $provide.value("ReservationCaller", reservationCallerJquery);


  }));

しかし、私はエラーが発生します:

エラー: [$injector:modulerr] 次の理由により、モジュール関数 ($provide,Config) のインスタンス化に失敗しました: エラー: [$injector:unpr] 不明なプロバイダー: Config

では、スタブのその文字列を構成から何かに設定するにはどうすればよいですか? (構成は「app.services」に存在します)...そこから取得する必要があると思いますが、どうすればよいですか?

4

1 に答える 1

1

注入がありません。サービス、定数、コントローラーなどを注入できます。次の2つの方法のいずれかです。

inject コールバックでは、$injector を取得して、必要なものを取得できます。

var myService, location;
beforeEach(function() {
        inject(function($injector) {
            myService = $injector.get('myService');
            location = $injector.get("$location");
        });
    });

また

サービスを直接取得します。ServiceName として解決される _ServiceName_ を追加できますが、テストで ServiceName 変数を使用できます。

var scope, $rootScope, $location;
beforeEach(inject(function (_$rootScope_, _$location_) {

        scope = $rootScope.$new();
        $rootScope = _$rootScope_;
        $location = _$location_
    }));

私の典型的なテスト設定は次のとおりです。

    beforeEach(module('MyApp'));
    beforeEach(module('MyApp.services'));
    beforeEach(module('MyApp.controllers'));

    var ctrl, scope, myService;
    beforeEach(inject(function ($rootScope, $controller, $injector) {
        scope = $rootScope.$new();
        ctrl = $controller('MyCtrl', {$scope: scope});
        myService = $injector.get('MyService');
    }));
于 2014-11-10T14:45:34.963 に答える