0

我正在尝试让 Jasmine 的设置和拆卸方法为我工作。我一定是用错了,因为设置后我无法让变量留在范围内。

不起作用:

describe 'classes/model', ->
  beforeEach ->
    model = new Model()
  describe 'the basics', ->
    it 'extends Backbone.Model', ->
      expect(model).toBeInstanceOf Model # Fails. 'model is not defined'

我想也许设置必须在描述的范围内。但这也不起作用:

describe 'classes/model', ->
  describe 'the basics', ->
    beforeEach ->
      model = new Model()
    it 'extends Backbone.Model', ->
      expect(model).toBeInstanceOf Model # Fails. 'model is not defined'

作品。(但不使用设置方便)

describe 'classes/model', ->
  describe 'the basics', ->
    it 'extends Backbone.Model', ->
      model = new Model()
      expect(model).toBeInstanceOf Model

我做错了什么会阻止beforeEach工作吗?

4

1 回答 1

4

找到了。可能应该早点注意到 :) 必须注意 CoffeeScript 管理变量范围的方式才能使其工作。

describe 'classes/model', ->
  model = null # get it in scope
  beforeEach ->
    model = new Model()
  describe 'the basics', ->
    it 'extends Backbone.Model', ->
      expect(model).toBeInstanceOf Model
于 2013-07-24T17:20:44.220 回答