为什么单元测试响应总是成功的?

时间:2015-04-27 22:38:21

标签: angularjs unit-testing karma-jasmine

我正在阅读相关的帖子,不要重复这个问题。

我有下一个单元测试代码:

describe('service', function() {
var questionApiService;

beforeEach(module('myApp'));

beforeEach(inject(function (_questionApiService_) {
    questionApiService = _questionApiService_;
}));

// Test service availability
it('check the existence of get field question service', inject(function(questionApiService) {
//expect(1).toEqual(100);

    questionApiService.getField()
        .then(function(data) {

            //console.log(data);
            expect(1).toEqual(100);
        });
}));

});

如果我在服务之外运行代码expect(1).toEqual(100);,结果是Error,但是如果我在服务中写了相同的代码expect(1).toEqual(100);,结果就是Success,这让我觉得验证器没有进入该服务。

怎么了?

编辑1:

您好Asta,我认为您的想法非常好,我正在尝试实施它。我的代码中有错误,我不知道如何调试:

defer = $q.defer();
spyOn(questionApiService, 'getField').andReturn(defer.promise);
defer.resolve(data);
expect(data.nextQ).toEqual(1);

我的单元测试总是失败。如果promise成功,则“data”对象必须具有nextQ属性。

编辑2:

嗨Asta,你的代码太棒了。我正在尝试在我的系统中执行您的代码,但仍然有错误。 ut失败了:

错误:意外请求:获取http://mi.url.com/api/thefield不再需要预期

你知道怎么了?澄清代码在我的应用程序上正常工作但是问题就是。

问题Api服务代码:

angular.module('myApp.services')
    .factory('questionApiService', function($http, $q) {
        var myService = {
            getField: function() {
                var defer = $q.defer();

                $http.get('http://mi.url.com/api/thefield')
                .success( function(data) {
                    defer.resolve(data);
                })
                .error( function(data) {
                    defer.reject(data);
                });

                return defer.promise;
            };

        return myService;
    });

你的考试:

describe('myApp', function () {

    beforeEach(function () {
        module('myApp');
    });

    describe('questionApiService', function () {
        it('should check the existence of get field question service', inject(function($rootScope, questionApiService) {
            var response = null;
            var promise = questionApiService.getField();

            promise.then(function(data) {
                response = data;
            });

            $rootScope.$apply();

            var expectedResponse = { "nextQ": 1 };
            console.log(response);
            //expect(JSON.parse(response.nextQ)).toEqual(expectedResponse.nextQ);
        }));

    });
});

1 个答案:

答案 0 :(得分:0)

我认为您只需要将您的期望移到then之外并执行$rootScope.$apply()

it('should check the existence of the get field question service', inject(function($rootScope, questionApiService) {
    response = null;
    promise = questionApiService.getField()
    promise.then(function(data) {
        response = data;
        });
     $rootScope.$apply();

    expectedResponse = { "nextQ": "value" }
    expect(JSON.parse(response)).toEqual(expectedResponse);
}));

我创建了一个可以用来玩的jsFiddle。它设置了一个服务,通过我用来测试http://jsfiddle.net/neridum/9uumwfzc/

的promise来返回JSON

或者,如果您想从其他服务测试此服务,您可以使用间谍模拟它。在这里,您可以将响应模拟为承诺,然后解决它

defer = $q.defer();
spyOn(questionApiService, 'getField').andReturn(defer.promise);
defer.resolve(data);
expect(data).toEqual('100');
相关问题