使用角度ng-mock的Jasmine测试不能与控制器和承诺一起使用

时间:2017-06-09 11:02:05

标签: angularjs unit-testing testing jasmine ngmock

我使用下面的代码玩了一下而且我无法找到问题,测试失败了以下消息:“预期未定义不定义。”

我有一个服务,它向我的控制器返回一个承诺。在控制器中,我使用$ q.all在我解决了承诺后立即做了一些事情。

我试着按照这个例子,但我看到的最大区别是,在示例中,它在控制器的根目录中有调用,并且我在方法“$ scope.CustomerTest”中进行了服务调用,所以我有在apply之前的这一额外行($ scope。$ apply())“$ scope.CustomerTest('Mr');”:

http://www.bradoncode.com/blog/2015/07/13/unit-test-promises-angualrjs-q/

这是我的测试代码:

var $scope;
var $q;
var deferred;
var $httpBackend;

//Inject the module "before each test"
beforeEach(angular.mock.module('marketingApp'));

beforeEach(inject(function ($controller,_$httpBackend_,  _$rootScope_, _$q_, marketingService) {
    $q = _$q_;
    $scope = _$rootScope_.$new();
    $httpBackend = _$httpBackend_;

    // We use the $q service to create a mock instance of defer
    deferred = _$q_.defer();

    // Use a Jasmine Spy to return the deferred promise
    spyOn(marketingService, 'getTitleSuggested').and.returnValue(deferred.promise);

    // Init the controller, passing our spy service instance
    $controller('customerController', {
        $scope: $scope,
        marketingService: marketingService
    });
}));

it('should resolve promise', function () {
    // Setup the data we wish to return for the .then function in the controller
    var titles = [{ "Id": 1, "Name": "Mr" }];

    deferred.resolve(titles);

    $httpBackend.when('GET', '/MarketingCustomers/GetTitleSuggested')
        .respond(200, titles);

    //I call to the controller method here.
    $scope.CustomerTest('Mr');

    $scope.$apply();

    // Since we called apply, not we can perform our assertions
    //expect($scope.TitlesTest).not.toBe(undefined);
    expect($scope.SelectedCustomerTitle).toEqual('Mr');
    //expect($scope.error).toBe(undefined);
});

这就是吸血鬼:

http://plnkr.co/edit/3IMzqH1yKW8kazZFWaA0?p=preview

评论controller.spec.js的第一个测试(it),另外两个测试工作。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

如果某人遇到类似的问题,我的问题再解决了这个问题,希望它有所帮助。

以下是测试:

describe('CustomerController.js', function() {

var results = [{
 "Id": 1,
 "Name": "Mr"
}];

var $controller;
var $q;
var $rootScope;
var $scope;
var marketingService;

beforeEach(angular.mock.module('marketingApp'));

beforeEach(inject(function(_$rootScope_, $controller, _$q_, 
_marketingService_) {
$scope = _$rootScope_.$new();
$q = _$q_;
$rootScope = _$rootScope_;
marketingService = _marketingService_;

$controller('customerController', {
  $scope: $scope,
  $rootScope: $rootScope
});

spyOn(marketingService, 'getTitleSuggested').and.callFake(function() {
  var deferred = $q.defer();
  deferred.resolve(results);
  return deferred.promise;
});

}));

it('It should call the service" ', function() {

$scope.CustomerTest('Mr');
expect(marketingService.getTitleSuggested).toHaveBeenCalledWith('Mr');
});


it('It should populate the "$scope.TitlesTest" ', function() {

$scope.CustomerTest('Mr');
$rootScope.$apply();

expect($scope.hello).toEqual('Hello Mr Marcos');
expect($scope.Titles[0].Name).toBe('Mr');
expect($scope.Titles).toBe(results);
});

});

这就是吸虫:     http://plnkr.co/edit/3IMzqH1yKW8kazZFWaA0?p=preview