简体   繁体   English

使用模拟工厂在Jasmine中测试嵌套的Promise

[英]Testing nested promise in Jasmine using mocked factory

I am building AngularJS application and I am trying to unit test it. 我正在构建AngularJS应用程序,并且正在尝试对其进行单元测试。 I want to inject mock service instead of real dependencies. 我想注入模拟服务,而不是真正的依赖关系。

This is the service I am trying to test: 这是我要测试的服务:

.factory('LoginService',['Login', function(Login){
    return {
        getActiveUser: function() {
            return Login.query({}).$promise.then(
                function (users) {
                    return users[0];
                },
                function (error) {
                    console.log(error);
                }
            );
        }
    };
}]);

It is a simple service that returns the first user from the list of all users. 这是一项简单的服务,可从所有用户列表中返回第一个用户。 Bellow is my attempt of writing the unit test with mocks, but unsuccessful. 贝娄是我尝试用模拟编写单元测试的尝试,但是没有成功。

....
beforeEach(module('loginModule', function($provide) {

    mockedService = {
        getActiveUser: function() {
            return {
                then: function(callback) {
                    return callback({name: 'John'});
                }
            };
        }
    };

    $provide.value('LoginService', mockedService);

}));

beforeEach(inject(function (_LoginService_,  _Login_, _$httpBackend_, $rootScope) {
    LoginService = _LoginService_;
    Login = _Login_;
    scope = $rootScope;
    httpBackend = _$httpBackend_;

    spyOn(mockedService, 'getActiveEnvironment').andCallThrough();
}));

describe('getActiveUser', function () {
    it('should call the function', function() {
        LoginService.getActiveUser();
        expect(mockedService.getActiveUser).toHaveBeenCalled();
    });

    it('should return active user', function() {
        var user = {};

        mockedService.getActiveUser().then(
            function (env) {
                user = env;
            }
        )

        expect(user.name).toBe("John");

    });
});

I was able to make test passes using spies and injecting the services method directly. 我能够使用间谍进行测试通过并直接注入services方法。 Now I want to achieve the same, but with mocked instance. 现在,我想实现相同的功能,但要使用模拟实例。 The first test passes but the second one fails.Anybody has the idea? 第一个测试通过了,但是第二个测试失败了,有人知道吗?

Try to mock service this way: 尝试以这种方式模拟服务:

mockedService = {
    getActiveUser: function() {
        return $q.when({username: 'Thomas'});
    }
}

then you can spy it: 那么您可以监视它:

spyOn(mockedService, 'getActiveUser').and.callThrough();

and in thes you would run it 然后在这些中运行它

mockedService.getActiveUser().then(function(env) {
    user = env;
});

Also you can manually create promise-like API: 您也可以手动创建类似promise的API:

mockedService = {
    getActiveUser: function() {
        return {
            then: function(callback) {
                return callback({username: 'Thomas'});
            }
        };
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM