cleishm / jsmockito

Javascript mocking framework inspired by the awesome mockito

Home Page:http://jsmockito.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

QUnit Integration Failing

HitmanInWis opened this issue · comments

Wow - this library looks fantastic! Just what I'm looking for, as I've heavily used Mockito for Java. However, I'm trying to integrate w/QUnit, but some of the functions do not appear to be working.

Lets first take an example that does work, to prove that I have things set up correctly.

var mockArray = mock(Array);
mockArray.push(5);
verify(mockArray, never()).push(anything());

correctly fails with:
Died on test #1: undefined - "Never wanted but invoked: obj.push()"

However...

var mockArray = mock(Array);
mockArray.push(5);
verifyZeroInteractions(mockArray);
verifyNoMoreInteractions(mockArray);

This test passes, which is incorrect.

Furthermore, if I try a similar test on a spy object of my own...

var SpyUtil = spy(Util);
SpyUtil.parseInteger("1234");
verifyNoMoreInteractions(SpyUtil);

In Firefox, this test works correctly:
Died on test #1: undefined - "No interactions wanted, but 1 remains: obj.parseInteger()"
However, in IE8, the test breaks:
1.Died on test #1: Object doesn't support this property or method - { "name": "TypeError", "message": "Object doesn't support this property or method", "number": -2146827850, "description": "Object doesn't support this property or method" }

If I add a line of code to verify the call to parseInteger:

var SpyUtil = spy(Util);
SpyUtil.parseInteger("1234");
verify(SpyUtil, once()).parseInteger("1234");
verifyNoMoreInteractions(SpyUtil);

Now both browsers are correctly detecting no errors.

I have the latest version of qunit.js. Would it help to get an older version of qunit? Any other ideas?

It would be very helpful in debugging this if you could send me a sample project to look at.

I have a sample project put together - how do I send it to you? I cant find any options to upload an attachment.

Brett Birschbach
HS2 Solutions
773-296-2600, x251
brett.birschbach@hs2solutions.com

-----Original Message-----
From: chrisleishman [mailto:reply@reply.github.com]
Sent: Thursday, August 11, 2011 1:00 AM
To: Brett Birschbach
Subject: Re: [jsmockito] QUnit Integration Failing (#5)

It would be very helpful in debugging this if you could either send me a sample project to look at.

Reply to this email directly or view it on GitHub:
#5 (comment)

Perhaps put it on github? ;)

Well now that would be the obvious answer, wouldn't it? However, I just have a "free" account, and I can't see anywhere that I can upload anything. I am allowed 0 repositories and 0 collaborations :-/.

That's 0 "private" repositories. Public repositories are always free on GitHub (and this can be public as far as I'm concerned!).

Ah, thanks! My first experience with git version control, so I stumbled thru the instructions and think I have what you need uploaded to https://github.com/HitmanInWis/jsmockito-qunit-error-example

Chris, is this something you can take a look at in the near future? Or perhaps there has already been a committed patch that is just not yet released?

Reason I ask is because I love jsmockito already (great work!), and I am using it to test code for a production-level app.

I've taken a quick look and have verified that there is some issue there. I haven't had a chance to debug it yet and figure out what's going on. I'll get to it soon hopefully.

Thank you. Looking forward to the patch.

Ok, I've debugged the issues. I've fixed the first part, involving the mocked Array (the method iterators were getting confused as to whether it was an object or an array). Regarding the second issue, I've gone as far as discovering that simply prefixing the call to verifyNoMoreInteractions with the namespace, e.g. 'JsMockito.verifyNoMoreInteractions', fixes the issue and IE gives the right error message. Which suggests that IE is doing some crazy exception handling of it's own somewhere, and I don't know enough about IE to figure that out right now. Patches are welcome.

Chris, I downloaded 1.0.4 to get your fix for the first issue. However, test #2 ("util.js tests: This test should fail, but it doesnt.") is still incorrectly passing in IE8. It appears that your fix is working for Firefox, Safari, and Chrome, but not IE.

Also, thinking on the IE issue where verifyZeroInteractions causes and issue, but JsMockito.verifyZeroInteractions works correctly...seems like it must have something to do with the "this" context.

Yes, I have reproduced the issue in IE (I didn't test that second test in IE previously, given I don't have easy access to an instance of that browser) and fixed in HEAD.

Note that it still gives the wrong error message, in the same manner that the last test does. I really don't understand what IE is doing there: I doubt it is to do with the context, as the method is getting invoked correctly - it's just that IE is replacing the exception message when it does fail.

Got your fix for the first issue (where IE wasnt detecting an interaction) and it's now working - thanks!

As for IE showing an incorrect error message, IE does have a debugger that can be used to trace the code. I tried it for a little bit, but don't have the time to fully figure out where the exception is being thrown from.

The exception is valid - it's the exception that gets generated when a test fails. It should propagate all the way out to the test harness and then get stringified and displayed. For some reason however, on IE that exception is caught and replaced with the generic exception you're seeing. Not sure why, and I don't really have the motivation to dig into that one. Happy for patches though. :)