Jasmine spec为受限制的计算属性

时间:2013-01-21 19:42:52

标签: javascript knockout.js jasmine

我正在尝试为计算的observable编写一个jasmine规范,如下所示:

self.positionDate = ko.computed(function () {
  var dateString = "";
  var start = moment(self.positionStartDate()).format("LL");
  var end = moment(self.positionEndDate()).format("LL");
  if (start !== end) {
    dateString = sprintf("%s - %s", start, end);
  }
  else {
    dateString = sprintf("%s", start);
  }
  return dateString;
}).extend({ throttle: 1 });

使用节流阀,positionDate计算功能不会更新。我试过人工等待以及调用valueHasMutated()都无济于事。

是否有人成功编写过限制计算机的规范?

1 个答案:

答案 0 :(得分:3)

由于使用了throttle,您可以观察到#34; async"所以你需要在Jasmine中使用asynchronous specs features

因此,您需要使用runswaits函数,例如:

describe('throttled obserable', function () {

    it('positionDate should be changed after positionStartDate changed', 
      function () {
        var vm = new ViewModel();

        runs(function () {
            vm.positionStartDate('January 21 2013');
            vm.positionEndDate('January 22 2013');
        });

        waits(1);

        runs(function () {
            expect(vm.positionDate())
                 .toEqual('January 21 2013 - January 22 2013');
            vm.positionStartDate('January 20 2013');
        });

        waits(1);

        runs(function () {
            expect(vm.positionDate())
                 .toEqual('January 20 2013 - January 22 2013');
        });
    });
});

演示JSFiddle