0

I am getting no joy from my jasmine.Clock. My expectation is that the code will mock out the clock object and will trigger setTimeout events upon setting the tick past the specified intervals in the setTimeout, however this does not seem to be working and I cannot find my flaw. My code seems to be parallel to others applying the same clock control behavior.

Background: the 'callback' function sets the this.action.state() to Constants.state.Ready after execution, prior it should be Constants.state.WAITING. Please note I am using knockout observables; the state is supposed to be called as a fx to retrieve value.

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)

Results: Failures:

   1) should initialize with public accessible state of waiting
       Message:
         Expected 'ready' to equal 'waiting'.
       Stacktrace:
         Error: Expected 'ready' to equal 'waiting'.
techie.brandon
  • 1,638
  • 2
  • 18
  • 27

2 Answers2

0

Explicit use of jasmine.Clock.defaultFakeTimer.setTimeout resolves issue (ugly).

clientStub = 
  get: (key,callback) ->
    if callback?
      jasmine.Clock.defaultFakeTimer.setTimeout(callback(undefined, getReturnValue),1500)
techie.brandon
  • 1,638
  • 2
  • 18
  • 27
0

this is in my spec helper. it solves the problem 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);
  }
};

and so on. All 4 lines from jasmine.js

Vishnu
  • 521
  • 1
  • 6
  • 12