简体   繁体   English

我可以使用Jasmine spy编写AngularJS服务的测试用例吗?

[英]Can I write test cases for AngularJS service using Jasmine spy?

I am building an application using AngularJS and I am right now developing test cases for my application. 我正在使用AngularJS构建应用程序,现在正在为我的应用程序开发测试用例。 Suppose I have a service like this; 假设我有这样的服务;

var app = angular.module('MyApp')
app.factory('SessionService', function () {

    return {
        get: function (key) {
            return sessionStorage.getItem(key);
        },
        set: function (key, val) {
            return sessionStorage.setItem(key, val);
        },
        unset: function (key) {
            return sessionStorage.removeItem(key);
        }
    };
});

Can I write test case for my service like this; 我可以像这样为我的服务编写测试用例吗?

beforeEach(module('MyApp'));
    describe('Testing Service : SessionService', function (SessionService) {
        var session, fetchedSession, removeSession, setSession;
        beforeEach(function () {
            SessionService = {
                get: function (key) {
                    return sessionStorage.getItem(key);
                },
                set: function (key, val) {
                    return sessionStorage.setItem(key, val);
                },
                unset: function (key) {
                    return sessionStorage.removeItem(key);
                }
            };
            spyOn(SessionService, 'get').andCallThrough();
            spyOn(SessionService, 'set').andCallThrough();
            spyOn(SessionService, 'unset').andCallThrough();
            setSession     = SessionService.set('authenticated', true);
            fetchedSession = SessionService.get('authenticated');
            removeSession  = SessionService.unset('authenticated');
        });
        describe('SessionService', function () {
            it('tracks that the spy was called', function () {
                expect(SessionService.get).toHaveBeenCalled();
            });
            it('tracks all the arguments used to call the get function', function () {
                expect(SessionService.get).toHaveBeenCalledWith('authenticated');
            });
            //Rest of the Test Cases
        });
    });

I am using Jasmine's spy method for developing this test case. 我正在使用Jasmine的间谍方法来开发此测试用例。 Is it fine or am I wrong? 很好还是我错了?

It looks nice. 看起来很好。 But I think you will have some problems with this: 但是我认为您会遇到一些问题:

get: function (key) {
        return sessionStorage.getItem(key);
},

You're not mocking the sessionStorage. 您不是在嘲笑sessionStorage。 So i guess you will get an error when trying to call getItem() from this object. 因此,我想尝试从此对象调用getItem()时会收到错误消息。 It seems that you're not interested in the return value of those calls in your tests. 您似乎对测试中这些调用的返回值不感兴趣。 You only check if they were called with the correct attributes. 您仅检查是否使用正确的属性调用了它们。 Like here: 像这儿:

it('tracks that the spy was called', function () {
   expect(SessionService.get).toHaveBeenCalled();
});

Why don't you change your SessionService's mocks to return anything? 您为什么不更改SessionService的模拟以返回任何内容? Like this: 像这样:

get: function (key) {
        return true;
},

If you want to test your getItem/setItem/removeItem you could do this in another test case 如果要测试getItem / setItem / removeItem,则可以在另一个测试用例中执行此操作

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

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