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

知识管理

我正在使用AngularJS构建应用程序,现在正在为我的应用程序开发测试用例。假设我有这样的服务;

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);
        }
    };
});

我可以像这样为我的服务编写测试用例吗?

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
        });
    });

我正在使用Jasmine的间谍方法来开发此测试用例。很好还是我错了?

费利佩·斯金纳(Felipe Skinner)

看起来很好。但是我认为您会遇到一些问题:

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

您不是在嘲笑sessionStorage。因此,我想尝试从此对象调用getItem()时会收到错误消息。您似乎对测试中这些调用的返回值不感兴趣。您仅检查是否使用正确的属性调用了它们。像这儿:

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

您为什么不更改SessionService的模拟以返回任何内容?像这样:

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

如果要测试getItem / setItem / removeItem,则可以在另一个测试用例中执行此操作

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我应该为同一服务的不同分支编写不同的测试用例吗?

我可以使用robot.api 获取失败的测试用例名称吗?

我们可以使用soapui中的闭包为每个测试用例执行测试吗

我可以使用单个 Zephyr 测试用例来维护多个设备上的测试执行记录吗?

这个JavaScript单元可以使用Jasmine进行测试吗?

Katalon中的e2e测试用例可以用Javascript语言编写吗

TestCafe 开源可以运行在 TestCafe studio 中编写的测试用例吗?

我可以为我的测试用例编写我自己版本的 go 的 http client.Do() 函数吗?

我可以在测试用例中模拟功能输入吗?

如何为 jQuery on() 方法编写 Jasmine 测试用例

如何为CombineLatestrxjs Angular编写Jasmine Unit测试用例

无法使用Marbel测试编写测试用例

我可以使用通配符替换吗

骨骼可以使用我的插件吗?

我可以使用多个“ with”吗?

我可以使用RTF格式吗?

我可以使用迭代器吗?

我可以使用javascript表单吗?

在哪里可以学习编写测试用例?

如何为我的函数编写测试用例?的PHP

我可以使用Windows编写Mac软盘格式吗?

我可以使用if else语句编写Firestore查询吗?

我可以使用ClassMap编写CsvHelper吗?

PrimeNg确认服务的Jasmine测试用例无法正常工作

使用 mocha 在 node.js 中编写单元测试用例来模拟 azure 服务总线队列

使用 mocha 在 node.js 中编写单元测试用例来模拟 Azure 服务总线队列以接收消息

如何为使用juid的uuid编写函数的测试用例?

使用 mockito 以否定方法编写测试用例

为使用jdbc的应用程序编写测试用例