jejacks0n / teaspoon

Teaspoon: Javascript test runner for Rails. Use Selenium, BrowserStack, or PhantomJS.
1.43k stars 243 forks source link

Teaspoon is not happy about passing .bind() .. can I just stub it instead? #139

Closed gotoAndBliss closed 10 years ago

gotoAndBliss commented 10 years ago

I have this inside my file.js..

window.addEventListener("message", this.receiveMessage.bind(this), false);

Which Teaspoon returns as..

Failure/Error: TypeError: 'undefined' is not a function (evaluating 'this.receiveMessage.bind(this)') in http://127.0.0.1:53048/assets/models/file.js?body=1 (line 12)

So I added a few console.log()'s and got the following to work properly..

window
window.addEventListener
window.addEventListener("message", this.receiveMessage(), false);

But what breaks is when I .bind(this) to receiveMessage which is required so that I can still reference this from within the method..

So seeing that Teaspoon isn't cool with this.. is there a way I can just stub that method with a spy? I tried this..

spyOn(quiz.receiveMessage, 'bind').andReturn(true);

But Teaspoon still returns the same error as it seems to be breaking the entire javascript rather than stepping over it because it was stubbed.

Any suggestions or ideas would be greatly appreciated. Thanks!

jedschneider commented 10 years ago

is it possible that this is an asynchronous issue, eg, this does not have the proper method at runtime but when you log it out, the method has resolved on the context of this by the time you are looking for it?

gotoAndBliss commented 10 years ago

Hmm.. how could I find out? The console is produced by Teaspoon itself, so I figured that this would be the same one that it fails on.

jejacks0n commented 10 years ago

How is this Teaspoon? You say "Teaspoon is not happy about passing .bind()", yet I see nothing in your code that's at all related to Teaspoon.

What it looks like, is that bind isn't being loaded into your test environment. So the question should be:

Where is the bind method coming from, and why isn't it defined?

If you figure that out, you will solve your issue. Again, this doesn't seem like anything related to Teaspoon, and I apologize if you can show me otherwise.

jejacks0n commented 10 years ago

Ok, I did some quick research.. I'd guess you're trying to use the ECMA-5.1 implementation.. which isn't supported everywhere -- and apparently not in phantomjs yet either?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

That link provides a polyfill, which you'll need to load. And you should consider loading that anyway, otherwise your JS won't work properly in some clients.

If you don't want to load the polyfill, you might want to consider using selenium as your driver (instead of phantomjs), as that might give you more expected results since I'd also guess you're only checking things in FF.

Am I at least close to understanding your issue?

gotoAndBliss commented 10 years ago

You were totes justified to close it before. With good reason. Its not Teaspoon's fault. It's my driver.

But nonetheless, I can't thank you enough for the advice. I am absolutely grateful. Thank you very very much!