Angular + Jasmine,在测试和控制器之间共享服务

Angular + Jasmine, sharing service between tests and controller

我的测试片段:

describe('SaleTypeCtrl', function(){
var scope, ctrl, $httpBackend;
var operator = {"OperatorId" : 24, "Number" : 4, "OperatorName" : "Heyah", "LogoUrl" : "http://www.sample.pl/logo.jpg" };

beforeEach(inject(function(_$httpBackend_, $rootScope, $controller, $state, _Sale_) {
  $httpBackend = _$httpBackend_;
  state = $state;
  scope = $rootScope.$new();
  Sale = _Sale_;
  ctrl = $controller('SaleTypeCtrl', {$scope: scope});
  Sale.setOperator(operator);
}));

it('Loads transaction types', function() {
    expect(Sale.getOperator().OperatorId).toEqualData(24); 
   $httpBackend.expectGET('api/transactionTypes?OperatorId=24').
  respond(
    [
      {"Type" : "Offline", "ButtonText" : "OFFLINE [KOD DOŁADOWUJĄCY]", "ButtonConfirmText" : "POBIERZ KOD", "Available" : true, "ConfirmationTypes" : ["Wydruk","Email"] },
      {"Type" : "Online", "ButtonText" : "ONLINE [DOŁADOWANIE]", "ButtonConfirmText" : "DOŁADUJ TELEFON", "Available" : true, "ConfirmationTypes" : ["Brak","Wydruk","Email"] }
    ]);
  $httpBackend.flush();
  expect(scope.data.TransactionTypes).toEqualData(
    [
      {"Type" : "Offline", "ButtonText" : "OFFLINE [KOD DOŁADOWUJĄCY]", "ButtonConfirmText" : "POBIERZ KOD", "Available" : true, "ConfirmationTypes" : ["Wydruk","Email"] },
      {"Type" : "Online", "ButtonText" : "ONLINE [DOŁADOWANIE]", "ButtonConfirmText" : "DOŁADUJ TELEFON", "Available" : true, "ConfirmationTypes" : ["Brak","Wydruk","Email"] }
    ]);
});

我的服务片段:

.factory('Sale', function($http){
var service = {};

service.operator = null;

service.getOperator = function(){
    return service.operator;
}

service.setOperator = function(operator){
    service.operator = operator;
}

service.getTransactionTypes = function () {
    return $http.get('api/transactionTypes', {params: {"OperatorId" : service.getOperator().OperatorId}});
}

我的控制器:

.controller('SaleTypeCtrl', function ($scope, $state, Sale) {
$scope.data = {};
$scope.loadTransactionTypes = function(){
    Sale.getTransactionTypes().success(function(transactionTypes){
        $scope.data.TransactionTypes = transactionTypes;
    }).error(function(err){
        console.error("Błąd");
    });
}
$scope.loadTransactionTypes();
})

我的测试失败:

Sales controllers SaleTypeCtrl Loads transaction types FAILED
        TypeError: Cannot read property 'OperatorId' of null at Object.service.getTransactionTypes

Sale.setOperator(operator) 为我的测试正确设置值。

expect(Sale.getOperator().OperatorId).toEqualData(24) 正确通过。

我试过这样改:

ctrl = $controller('SaleTypeCtrl', {$scope: scope, Sale:Sale});

但它仍然没有解决我的问题。控制器似乎有不同的 Sale 实例。

我应该如何修改我的测试来为注入控制器的工厂设置一些初始值?

在 beforeEach() 中,您正在构造您的控制器,它调用 $scope.loadTransactionTypes(),它调用 Sale.getTransactionTypes(),它执行 service.getOperator().OperatorId,它失败了,因为运算符设置在仅在创建控制器后才提供服务。