Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何在promise中单元测试条件()karma和jasmine_Angularjs_Unit Testing_Karma Jasmine - Fatal编程技术网

Angularjs 如何在promise中单元测试条件()karma和jasmine

Angularjs 如何在promise中单元测试条件()karma和jasmine,angularjs,unit-testing,karma-jasmine,Angularjs,Unit Testing,Karma Jasmine,我将AngularJS 1.7与Karma和Jasmine一起使用。我已经开始学习单元测试用例 我的控制器中有一个示例方法 _this.method = function () { Service.getData().then(function (response) { if (response.productId === "ClientAPI") { // Some code } else {

我将AngularJS 1.7与Karma和Jasmine一起使用。我已经开始学习单元测试用例

我的控制器中有一个示例方法

_this.method = function () {
    Service.getData().then(function (response) {
        if (response.productId === "ClientAPI") {
            // Some code
        }
        else {
            // Some Code
        }
    }, function (error) {
        _this.inProgress = false;
        if (error.status === 400) {
            // Some Code
        } else {
            // Some Code
        }
    })
}
下面是我的测试用例:

describe('Some Route :: Controller => ', function () {
    var $componentController;
    var Service;

    beforeEach(module('app'));
    beforeEach(inject(function (_$componentController_, _Service_) {
        Service = _Service_;
        spyOn(Service, 'getData').and.callFake(function() {
            var deferred = $q.defer();
            var response = {};
            response.productId = "ClientAPI";
            deferred.resolve(result);
            return deferred.promise;
        });
        ctrl = $componentController('controllerName', { Service: Service });
    }));

    it('Ctrl Method : should true', function () {
        ctrl.method();

        expect(Service.getData).toHaveBeenCalled();

        Service.getData().then(function (response) {
            expect(response.productId).toBe("ClientAPI")
        })

    });
});
但是如果(response.productId==“ClientAPI”){


不确定在promise中测试时我做错了什么。

您需要调用$scope。$apply()来触发promise回调的调用:

beforeEach(inject(function (_$componentController_, _Service_) {
    Service = _Service_;
    spyOn(Service, 'getData').and.returnValue($q.resolve({ productId: 'ClientAPI' }));
    ctrl = $componentController('controllerName', { Service: Service });
}));

it('Ctrl Method : should true', inject(function($rootScope) {
    ctrl.method();

    expect(Service.getData).toHaveBeenCalled();
    $rootScope.$apply();

    // now test that the ctrl state has been changed as expected.
    // testing that the service has returned ClientAPI is completely useless:
    // the service is a mock, and you have told the mock to return that
    // this should test the component, based on what you've told the service
    // to return. It's not supposed to test the mock service.
    // testing what the service returns tests jasmine, not your code.
});