Created
October 21, 2012 19:13
-
-
Save fivetanley/3928130 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' | |
Also, I can't think of any case where I'd want an undefined variable floating around getting re-assigned in application code.
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
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 = nullthough if your testing framework doesn't work like that.