【问题标题】:Jasmine and angular mocks : mocking a service that handles local storageJasmine 和 Angular 模拟:模拟处理本地存储的服务
【发布时间】:2016-01-28 11:38:08
【问题描述】:

我有一个名为 wd$cache 的服务,它基本上是 localStorage.setItem 和 get.item 的包装器。

现在我正在尝试测试一个使用该服务来实现特定结果的控制器。主要问题是我有一个 IF 语句,只有当你已经设置了 localstorage 时才会触发,这让我发疯了! (我们在这里做 TDD)

服务

(function () {

angular
    .module('hub')
    .controller('promotionNotificationCtrl', promotionNotificationCtrl);

promotionNotificationCtrl.$inject = [
    'hub$promotions',
    'hub$client',
    'wd$cache'
];

function promotionNotificationCtrl(
    hub$promotions,
    hub$client,
    wd$cache) {

    var vm = this;
    activate();

    //////////

    function activate () {

        hub$promotions.get(hub$client.brand, hub$client.subbrand).success(function (data) {

            if (!wd$cache.get('hub$notification')) {

                wd$cache.add('before', 123);
            } else {

                wd$cache.add('after', 321);
            }
        });
    }
}
})(); 

测试

describe('The promotion notification controller', function () {

var controller,
    hub$client,
    $httpBackend,
    wd$cache,
    mockData = [{

        "foo": "bar"
    },
    {
        "faa": "boo"
    }];


beforeEach(module('hub'));
beforeEach(module('wired.core'));

beforeEach(module(function ($provide) {

    hub$client = {

        brand: 'bw',
        subbrand: 'plus'
    };

    wd$cache = {

        add: function () {

        },

        get: function () {

        }
    };

    $provide.value('hub$client', hub$client);
    $provide.value('wd$cache', wd$cache);
    spyOn(wd$cache, 'add');
}));

beforeEach(inject(function ($controller, _$httpBackend_, _hub$promotions_) {

    controller = $controller('promotionNotificationCtrl');
    $httpBackend = _$httpBackend_;
    hub$promotions = _hub$promotions_;

    // request
    $httpBackend.expectGET("/umbraco/api/promotions/get/?brand=bw&lang=en&subbrand=plus").respond(200, mockData);
    $httpBackend.flush();
}));

it('should attempt to add a cache with a "before" key if no previous "hub$notification" cache was found', function () {


    expect(wd$cache.add).toHaveBeenCalledWith('before', 123); //WORKING
})

it('should attempt to add a cache with a "after" key if a previous "hub$notification" cache was found', function () {

    localStorage.setItem('hub$notification');
    wd$cache.add('hub$notification');

    expect(wd$cache.add).toHaveBeenCalledWith('after', 123); // NOT WORKING
    // CANT GET THROUGH THE IF STATEMENT
})
});

基本上,无论我做什么,我都无法在 BeforeEach 块之后进入“测试用例”。自从模拟它使用实际存储以来,我已经尝试了一切。

有什么想法吗?

【问题讨论】:

  • 你能发布你在 Karma 测试运行器中遇到的错误吗?
  • 完全忘记了!预期 wd$cache.add 已使用 'after' 调用,但实际调用是 'before',123

标签: javascript angularjs unit-testing jasmine angular-mock


【解决方案1】:

您可以提供一个已经填充了一些数据的模拟实现:

var cache = {};    

beforeEach(module(function ($provide) {
    // ...
    wd$cache = {
        add: function (key, value) {
            cache[key] = value;
        },
        get: function (key) {
            return cache[key];
        }
    };

    // add initial data here or in the individual tests, e.g.


    // ...
}));

要为特定测试用例正确设置缓存,您可以使用 cache 字段,如下所示:

cache['hub$notification'] = 'whatever value makes sense here';

当然您也可以在beforeEach 中执行此操作。

目前你正在尝试这样做:

wd$cache.add('hub$notification');
expect(wd$cache.add).toHaveBeenCalledWith('after', 123);

这有两个问题:

  • 您没有更新缓存,因为您正在监视没有.andCallThrough()add 方法。你应该解决这个问题(在间谍创建后添加.andCallThrough())否则来自控制器的更新不会影响缓存。

  • 间谍会记录您的通话。您不希望将其用于设置代码,因为它会使后续断言更加复杂。

【讨论】:

  • 只有在我设置 cache['hub$notification'] = 123; 时才有效在每个之前,这将使第一次测试中断,因为有一个可用的缓存! it('如果找到先前的 "hub$notification" 缓存,则应尝试使用 "after" 键添加缓存', function () { wd$cache.hub$notification = 123; expect(wd$cache.add) .toHaveBeenCalledWith('after', 123); })
  • 那是一个愚蠢的评论!但基本上通过这样做,你最终只能完成一个 it 语句,因为让它工作的唯一方法是将它放在每个语句之前!
  • 只需将var cache = {} 移动到外部范围。然后,您可以在每个单独的测试中修改缓存内容,而不仅仅是在 beforeEach 中。
  • 从一开始就在我的代码之外。即使我在每个 IT 上设置了一个新的缓存,它也只会获得第一个值。不知道为什么!
  • 我更新了我的答案。它现在包括解释为什么您当前的方法不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-12
  • 1970-01-01
  • 2014-11-22
  • 1970-01-01
  • 2015-07-07
相关资源
最近更新 更多