How to make sure this function is never called?

14,482 views
Skip to first unread message

Felipe Coury

unread,
Mar 3, 2012, 4:19:08 PM3/3/12
to jasmi...@googlegroups.com
I have this snippet of code (coffeescript):

@afterRender() if @afterRender

How can I test that afterRender is not called if it doesn't exist?

Here's the positive test:

    it 'calls afterRender when defined', ->
      called = false
      @view.afterRender = -> called = true
      @view.render()
      expect(called).toBeTruthy()

But everything I tried to come up with a good way to test failed me.

I am using sinon.js, so I can use its spies, mocks and stubs if you think that it would help.

Any ideas?

Davis W. Frank

unread,
Mar 3, 2012, 6:51:05 PM3/3/12
to jasmi...@googlegroups.com
You want to spy on the afterRender call and check the spy in the positive and negative test. I don't use Sinon.js, but here's a way to do it with JS and Jasmine's spies:

describe("MyView", function() {
var view;
beforeEach(function() {
view = new View();
spyOn(view, 'afterRender');
});

describe("when render is called", function() {
beforeEach(function() {
view.render();
});
it("should call the afterRender callback", function() {
 expect(view.afterRender).toHaveBeenCalled();
});
});
describe("when foo is called", function() {
beforeEach(function() {
view.foo();
});
it("should call the afterRender callback", function() {
 expect(view.afterRender).not.toHaveBeenCalled();
});
});
})



--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/jasmine-js/-/5iuNh8X6QvQJ.
To post to this group, send email to jasmi...@googlegroups.com.
To unsubscribe from this group, send email to jasmine-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jasmine-js?hl=en.



--
thx,
--dwf

Reply all
Reply to author
Forward
0 new messages