茉莉花coffeescript时钟无效

时间:2012-10-31 21:31:55

标签: javascript jasmine specifications jasmine-node

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

背景:'callback'函数在执行之后将this.action.state()设置为Constants.state.Ready,之前它应该是Constants.state.WAITING。请注意我使用的是淘汰观察者;该状态应该被称为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'.

2 个答案:

答案 0 :(得分:0)

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

clientStub = 
  get: (key,callback) ->
    if callback?
      jasmine.Clock.defaultFakeTimer.setTimeout(callback(undefined, getReturnValue),1500)

答案 1 :(得分:0)

这是我的规范助手。它解决了这个问题     jasmine.getGlobal = function(){       返回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行