0

我没有从我的茉莉花中得到快乐。时钟。我的期望是代码将模拟时钟对象并在将滴答设置超过 setTimeout 中的指定间隔时触发 setTimeout 事件,但这似乎不起作用,我找不到我的缺陷。我的代码似乎与其他应用相同时钟控制行为的代码平行。

背景:“回调”函数在执行后将 this.action.state() 设置为 Constants.state.Ready,之前它应该是 Constants.state.WAITING。请注意,我使用的是淘汰赛 observables;该状态应该被称为 fx 以检索值。

describe "Redis GET Action", () ->
  beforeEach () ->
    jasmine.Clock.useMock();
    this.getReturnValue = getReturnValue = "Some jasmine values from redis"
    clientStub = 
      get: (key,callback) ->
        if callback?
          setTimeout(callback(undefined, getReturnValue),1500)
      GET: (key,callback) ->
        if callback?
          setTimeout(callback(undefined, getReturnValue),1500)

    this.action = new RedisGetAction(
      client: clientStub
      key: "Standard"
      )


  it "should return a object", () ->
    expect(this.action).toEqual(jasmine.any(Object))

  requiredMethods = ['state','data','params'];

  requiredMethods.forEach (methodName) ->
    it "should have public "+methodName+" method", () ->
      expect(this.action[methodName]).toBeDefined();

  it "should initialize with public accessible state of #{Constants.state.WAITING}", () ->
    expect(this.action.state()).toEqual(Constants.state.WAITING)
    jasmine.Clock.tick(1501);
    expect(this.action.state()).toEqual(Constants.state.READY)

结果: 失败:

   1) should initialize with public accessible state of waiting
       Message:
         Expected 'ready' to equal 'waiting'.
       Stacktrace:
         Error: Expected 'ready' to equal 'waiting'.
4

2 回答 2

0

明确使用 jasmine.Clock.defaultFakeTimer.setTimeout 解决问题(丑陋)。

clientStub = 
  get: (key,callback) ->
    if callback?
      jasmine.Clock.defaultFakeTimer.setTimeout(callback(undefined, getReturnValue),1500)
于 2012-11-01T20:27:18.477 回答
0

这是我的规范助手。它解决了问题 jasmine.getGlobal = function() { return GLOBAL; }

jasmine.getGlobal().setTimeout = function(funcToCall, millis) {
  if (jasmine.Clock.installed.setTimeout.apply) {
    return jasmine.Clock.installed.setTimeout.apply(this, arguments);
  } else {
    return jasmine.Clock.installed.setTimeout(funcToCall, millis);
  }
};

等等。jasmine.js 中的所有 4 行

于 2012-12-10T13:43:25.330 回答