SimplGy
SimplGy

Reputation: 20437

Using Jasmine's `beforeEach` method

I'm trying to get Jasmine's setup and teardown methods to work for me. I must be using it wrong, because I can't get variables to stay in scope after setup.

Doesn't work:

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

I thought maybe setup has to be in the scope of the describe. But this also doesn't work:

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

Works. (but doesn't use setup convenience)

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

Am I doing something wrong that prevents beforeEach from working?

Upvotes: 0

Views: 650

Answers (1)

SimplGy
SimplGy

Reputation: 20437

Found it. Probably should have noticed sooner :) Have to pay attention to the way CoffeeScript manages variable scope to make this work.

describe 'classes/model', ->
  model = null # get it in scope
  beforeEach ->
    model = new Model()
  describe 'the basics', ->
    it 'extends Backbone.Model', ->
      expect(model).toBeInstanceOf Model

Upvotes: 4

Related Questions