Skip to content

Instantly share code, notes, and snippets.

@fivetanley
Created October 21, 2012 19:13
Show Gist options
  • Select an option

  • Save fivetanley/3928130 to your computer and use it in GitHub Desktop.

Select an option

Save fivetanley/3928130 to your computer and use it in GitHub Desktop.
credentials = stub()
twitter = stub()
describe "Twitter, wrapper for ntwitter", ->
# just declare an undefined variable so it can get picked up later
# with closure scope.
# other alternative i can think of: twitter = "" # empty string or other val
# e.g. what I want is just a `var twitter;`
{twitter} = {}
beforeEach ->
twitter = new Twitter( credentials, twitterLib )
it "creates the twitter library with credentials", ->
expect(twitterLib).to.have.been.calledWith credentials
it "stores the created twitter library", ->
expect(twitter).to.have.ownProperty 'twitterLib'
@ryanflorence

Copy link
Copy Markdown

Normally a test framework lets you assign @twitter = new Twitter credentials, twitterLib, so you don't need to share that scoped variable.

I'd just do twitter = null though if your testing framework doesn't work like that.

@ryanflorence

Copy link
Copy Markdown

Also, I can't think of any case where I'd want an undefined variable floating around getting re-assigned in application code.

@fivetanley

Copy link
Copy Markdown
Author

It's only in the test code, as a test variable. People also use closures for mocking private javascript variables, but I dislike using private variables most of the time.

Just me doing dumb things to save typing an @ symbol. :P

Mocha does support using this for each spec, and for async tests, actually makes more sense than the thing I want to do, because each spec would have its own instance variable, versus getting overwritten by other tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment