karma / jasmine测试是否首先实例化并运行控制器?

时间:2015-07-16 17:37:01

标签: jasmine

这是我的测试:

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

  // First, we load the app's module
  beforeEach(module('F1FeederApp'));

  // Then we create some variables we're going to use
  var driversController, scope;

  beforeEach(inject(function ($controller, $rootScope, $httpBackend) {

    // Here, we create a mock scope variable, to replace the actual $scope variable the controller would take as parameter
    scope = $rootScope.$new();

    // Then we create an $httpBackend instance. I'll talk about it below.
    httpMock = $httpBackend;

    // Here, we set the httpBackend standard reponse to the URL the controller is supposed to retrieve from the API
    httpMock.expectJSONP("http://ergast.com/api/f1/2013/driverStandings.json?callback=JSON_CALLBACK").respond(
      {"MRData": {"StandingsTable": {"StandingsLists" : [{"DriverStandings":[
        {
          "Driver": {
              "givenName": 'Sebastian',
              "familyName": 'Vettel'
          },
          "points": "397",
          "nationality": "German",
          "Constructors": [
              {"name": "Red Bull"}
          ]
        },
        {
          "Driver": {
              "givenName": 'Fernando',
              "familyName": 'Alonso'
          },
          "points": "242",
          "nationality": "Spanish",
          "Constructors": [
              {"name": "Ferrari"}
          ]
        },
        {
          "Driver": {
              "givenName": 'Mark',
              "familyName": 'Webber'
          },
          "points": "199",
          "nationality": "Australian",
          "Constructors": [
              {"name": "Red Bull"}
          ]
        }
      ]}]}}}
    );

    // Here, we actually initialize our controller, passing our new mock scope as parameter
    driversController = $controller('driversController', {
      $scope: scope
    });

    // Then we flush the httpBackend to resolve the fake http call
    httpMock.flush();

  }));


  // Now, for the actual test, let's check if the driversList is actually retrieving the mock driver array
  it('should return a list with three drivers', function () {
    expect(scope.driversList.length).toBe(3);
  });

  // Let's also make a second test checking if the drivers attributes match against the expected values
  it('should retrieve the family names of the drivers', function () {
    expect(scope.driversList[0].Driver.familyName).toBe("Vettel");
    expect(scope.driversList[1].Driver.familyName).toBe("Alonso");
    expect(scope.driversList[2].Driver.familyName).toBe("Webber");
  });

});

问题:

  1. 在我的第一次测试中,我写道:

    期望(scope.driversList.length).toBe(3);

  2. 在我的控制器中的此方法触发之前,此范围变量不会被设置:

    angular.module('F1FeederApp.controllers', []).
    
      /* Drivers controller */
      controller('driversController', function($scope, ergastAPIservice) {
        $scope.nameFilter = null;
        $scope.driversList = [];
        $scope.searchFilter = function (driver) {
            var re = new RegExp($scope.nameFilter, 'i');
            return !$scope.nameFilter || re.test(driver.Driver.givenName) || re.test(driver.Driver.familyName);
        };
    
        ergastAPIservice.getDrivers().success(function (response) {
            //Digging into the response to get the relevant data
            $scope.driversList = response.MRData.StandingsTable.StandingsLists[0].DriverStandings;
        });
      }).
    

    我的控制器真的是一个在我的测试中自动调用的函数吗?怎么样?什么时候被召唤?

    1. 为什么我们在测试中使用$ rootScope?

1 个答案:

答案 0 :(得分:1)

对于每个测试套件,控制器首先与beforeEach设置(如果有)进行初始化 来你的问题 1.由于控制器是初始化的,并且你的下面的方法没有包含在任何函数中,它被调用并设置你的 driverList 数组

ergastAPIservice.getDrivers().success(function (response) {
    //Digging into the response to get the relevant data
    $scope.driversList = response.MRData.StandingsTable.StandingsLists[0].DriverStandings;
});

避免简单地将其包装在函数中。要在控制器负载上调用此功能,您可以使用

  

NG-INIT

  1. 您可以查看this