sinonjs / sinon

Test spies, stubs and mocks for JavaScript.
https://sinonjs.org/
Other
9.66k stars 771 forks source link

Standalone: sinon.test returns function as opposed to executing test code #15

Closed artemave closed 13 years ago

artemave commented 13 years ago

The expected usage:

it "should check if user is set", -> sinon.test ->
  User = current: this.spy()
  app = new AppController
  ( expect User.current.called ).toBeTruthy()

Does nothing unless sinon.test is wrapped in function invocation:

it "should check if user is set", -> (
  sinon.test ->
    User = current: this.spy()
    app = new AppController
    ( expect User.current.called ).toBeTruthy()
)()

The reason becomes apparent when examining how sinon.test is defined - it returns function.

cjohansen commented 13 years ago

This is actually intended. sinon.test is a utility to wrap a test function to sandbox it. It's the fast-lane integration with a test framework. The more "proper" way to integrate is to build a sandbox into the test execution (which e.g. the QUnit adapter does).

You'll have forgive me cluelessness, but the examples - coffeescript? I'm not familiar with its syntax, so I'm not entirely sure of the differences between those two.

artemave commented 13 years ago

Right. It clicked. My bad. The actual expected usage is:

it "should check if user is set", sinon.test ->
  User = current: this.spy()
  app = new AppController
  ( expect User.current.called ).toBeTruthy()

which works perfectly. Thanks!

Yes, it is coffeescript. And it totally rocks! Syntax (in the above examples) is easy to decipher: -> is function() {} the rest is just lack of parenthesis in favor of indentation.

cjohansen commented 13 years ago

Ah, that's good news. Syntax still looks foreign to me, but glad you enjoy it - and made Sinon work :)