testing for thrown errors with jasmine

9,790 views
Skip to first unread message

Charlie

unread,
Jul 6, 2009, 5:57:54 PM7/6/09
to Jasmine
Hi,

Is there a matcher in the works that will test for thrown errors? I
have a few objects that when constructed with invalid parameters throw
Errors.

I would like to be able to have something like:

it('should throw an error on invalid parameter', function(){
expect(new mynamespace.MyObject(invalidParam)).toThrowError();
});


right now I have the following, but it is ugly and a pain to write for
every instance of this behavior:

it('should throw an error on invalid parameter', function(){
var error;
try{
var obj = new mynamespace.MyObject(invalidParam);
}
catch(err){
error = err;
}
expect(error).toBeDefined();
});

If I write up a matcher for this behavior, could one of the committers
review it and possibly include it in the Jasmine distribution?

Thanks

-Charlie

Christian Williams

unread,
Jul 6, 2009, 11:37:35 PM7/6/09
to jasmi...@googlegroups.com
Give toThrow() a try:


it('should throw an error on invalid parameter', function(){
    expect(function() { new mynamespace.MyObject(invalidParam); }).toThrow();
});

You can also pass it an expected exception or message:


it('should throw an error on invalid parameter', function(){
    expect(function() { throw new Error("the expected error message"; }).toThrow("the expected error message");
});

Note that you need to pass a function to expect.

--X [typos courtesy of my iPhone]

Charlie Meyer

unread,
Jul 6, 2009, 11:41:39 PM7/6/09
to jasmi...@googlegroups.com
thanks,

didnt see that matcher listed on the documentation page (http://github.com/pivotal/jasmine/tree/master). I should have taken a closer look at the actual source code. Given this, maybe the documentation page should be updated to reflect all the available matchers.

thanks

Charlie Meyer

cha...@charliemeyer.net
http://www.charliemeyer.net

Department of Computer Science
University of Illinois at Urbana-Champaign

dwf...@gmail.com

unread,
Jul 6, 2009, 11:57:58 PM7/6/09
to jasmi...@googlegroups.com
Charlie - 

There's now a doc directory on trunk w/ JSDoc HTML.

That said, we happily take doc & README patches.

--dwf


Michael Gaffney

unread,
Jul 7, 2009, 12:43:30 AM7/7/09
to jasmi...@googlegroups.com
Hey all,
I'm trying to figure out how to add jasmine to my app to be able to
do tdd. Is the test view another app, another stage, or just special
scene that only loads up in certain configurations?

-Mike

Davis W. Frank

unread,
Jul 7, 2009, 12:49:55 AM7/7/09
to jasmi...@googlegroups.com
Mike,

What type of app are you looking to use Jasmine for?

--dwf
--
--dwf
http://amaz.infe.ws -- Amazon.com optimized for your smartphone

Michael Gaffney

unread,
Jul 7, 2009, 12:52:38 AM7/7/09
to jasmi...@googlegroups.com
Palm pre apps. I have some app logic. One case is game and I want to
test the model I'm building behind the scenes, especially for completion
state. So no ajax or whatever at the moment. I'll likely be getting into
that later, but for now just simple TDD javascript.

-Mike

Davis W. Frank

unread,
Jul 7, 2009, 12:58:36 AM7/7/09
to jasmi...@googlegroups.com
We're about a week away from releasing an open source solution that
integrates Jasmine into webOS.

I'm spending this week cleaning up, documenting, etc. Expect an
announcement shortly. Sorry I can't say more just yet.

--dwf

Michael Gaffney

unread,
Sep 8, 2009, 6:02:52 PM9/8/09
to jasmi...@googlegroups.com
Hey all,
I'm on Ubuntu 9.04 with FF 3.5 or Opera 10 and running

rake jasmine:server from the root or viewing the example_suite.html file
from the examples just gives me an empty green bar with no tests.

Any help would be appreciated as I'm ready to delve into really
expanding some of my webos apps and want to get my BDD on.

-Mike

jasmine.png

Christian Williams

unread,
Sep 8, 2009, 7:53:32 PM9/8/09
to jasmi...@googlegroups.com, jasmi...@googlegroups.com
Hey Michael -- could you check the JS console (or Firebug console) for
any errors?

You could also try pulling from master and see if you get any errors
at the top of the page about missing files. You'll need to remove
references to jasmine.js, JSON2.js, and TrivialRunner.js from your
Rakefile.

--X [typos courtesy of my iPhone]

> <jasmine.png>

Michael Gaffney

unread,
Sep 8, 2009, 9:10:57 PM9/8/09
to jasmi...@googlegroups.com

Christian Williams wrote:
> Hey Michael -- could you check the JS console (or Firebug console) for
> any errors?
>

All of this has been done on master btw.

Firebug says nothing when I load the examples/html/example_suite.html
directly in firefox.


> You could also try pulling from master and see if you get any errors
> at the top of the page about missing files. You'll need to remove
> references to jasmine.js, JSON2.js, and TrivialRunner.js from your
> Rakefile.
>

So I'm trying to run the main Rakefile with:
pivotal-jasmine$ rake jasmine_serve

and I get

(in /home/mike/workspace/pivotal-jasmine)
rake aborted!
no such file to load -- json

because I'm guessing rubygems isn't required.

Here's the changes I made to the Rakefile per what you said:

mike@mgaffney-l1:~/workspace/pivotal-jasmine$ git diff Rakefile
diff --git a/Rakefile b/Rakefile
index f6ae1cd..b31ab59 100644
--- a/Rakefile
+++ b/Rakefile
@@ -16,9 +16,9 @@ end
def start_jasmine_server(jasmine_includes)
require File.expand_path(File.join(JasmineHelper.jasmine_root,
"contrib/ruby/jasmine_spec_builder"))

- includes = jasmine_includes +
- ['/lib/json2.js',
- '/lib/TrivialReporter.js']
+# includes = jasmine_includes +
+# ['/lib/json2.js',
+# '/lib/TrivialReporter.js']

puts "your tests are here:"
puts " http://localhost:8888/run.html"


-Mike

Christian Williams

unread,
Sep 9, 2009, 7:36:02 PM9/9/09
to jasmi...@googlegroups.com
Oops, that happens for me too, looks like I busted something with a recent checkin.  I'll look in more detail soon.

--X

Christian Williams

unread,
Sep 9, 2009, 8:51:30 PM9/9/09
to Rajan Agaskar, jasmi...@googlegroups.com
Thanks Rajan, my bad.


--X [typos courtesy of my iPhone]

On Sep 9, 2009, at 17:44, Rajan Agaskar <ra...@pivotallabs.com> wrote:

FWIW I noticed this this morning and have a fix I'll push tonight.

Basically the new scheme (which looks like a pretty awesome refactor otherwise) does not support jasmine source files. I made that optional -- just need to run the tests and then I'll commit it. Will send a note to jasmine-js and dev when I'm done.

Rajan Agaskar

unread,
Sep 13, 2009, 6:12:22 PM9/13/09
to jasmi...@googlegroups.com
Michael:

Sorry for the late notice, but we were able to replicate this bug and pushed a build last Friday that should've fixed the issues you were experiencing (essentially we had a couple minor regressions in our rake tasks). Please let us know if you run into any other problems.

Thanks!

Rajan

Michael Gaffney

unread,
Sep 13, 2009, 7:35:37 PM9/13/09
to jasmi...@googlegroups.com
I still get the "no such file 'json'" from the require from the
rubygems. Is there some environment variable I'm supposed to use?

I also still can't get any of the examples to show anything other than 0
of 0 tests passed.

0.8.0 seems to work.

-Mike

Christian Williams

unread,
Sep 15, 2009, 11:09:30 AM9/15/09
to jasmi...@googlegroups.com
I don't know a bunch about ruby, but do you have the json gem installed?

$ sudo gem install json

--X [typos courtesy of my iPhone]

Michael Gaffney

unread,
Sep 15, 2009, 11:10:51 AM9/15/09
to jasmi...@googlegroups.com
Yep

Michael Gaffney

unread,
Sep 15, 2009, 11:11:23 AM9/15/09
to jasmi...@googlegroups.com
If I put a
require 'rubygems'
at the top of the rakefile it works. But I get this without it.

-Mike

Davis Frank

unread,
Sep 15, 2009, 11:43:38 AM9/15/09
to jasmi...@googlegroups.com
Michael,

A few other questions:
- what's your dev environment (OS, etc.)
- which version of Ruby & Rubygems are you using?
- what have you named your rakefile?

--dwf

Michael Gaffney

unread,
Sep 15, 2009, 2:29:00 PM9/15/09
to jasmi...@googlegroups.com
Davis,
- Ubuntu 9.04.
- Ruby 1.8.7 Gems 1.3.1
- I'm just using the default Rakefile from master on the jasmine repo
cloned from pivotal.

-Mike

Davis Frank wrote:
> Michael,
>
> A few other questions:
> - what's your dev environment (OS, etc.)
> - which version of Ruby & Rubygems are you using?
> - what have you named your rakefile?
>
> --dwf
>
> On Tue, Sep 15, 2009 at 8:11 AM, Michael Gaffney <mr.g...@gmail.com
> <mailto:mr.g...@gmail.com>> wrote:
>
>
> If I put a
> require 'rubygems'
> at the top of the rakefile it works. But I get this without it.
>
> -Mike
>
> Christian Williams wrote:
> > I don't know a bunch about ruby, but do you have the json gem
> installed?
> >
> > $ sudo gem install json
> >
> > --X [typos courtesy of my iPhone]
> >
> > On Sep 13, 2009, at 16:35, Michael Gaffney <mr.g...@gmail.com
> <mailto:mr.g...@gmail.com>> wrote:
> >
> >> I still get the "no such file 'json'" from the require from the
> >> rubygems. Is there some environment variable I'm supposed to use?
> >>
> >> I also still can't get any of the examples to show anything other
> >> than 0
> >> of 0 tests passed.
> >>
> >> 0.8.0 seems to work.
> >>
> >> -Mike
> >>
> >> Rajan Agaskar wrote:
> >>> Michael:
> >>>
> >>> Sorry for the late notice, but we were able to replicate this
> bug and
> >>> pushed a build last Friday that should've fixed the issues you were
> >>> experiencing (essentially we had a couple minor regressions in our
> >>> rake
> >>> tasks). Please let us know if you run into any other problems.
> >>>
> >>> Thanks!
> >>>
> >>> Rajan
> >>>
> >>> On Tue, Sep 8, 2009 at 3:02 PM, Michael Gaffney
> <mr.g...@gmail.com <mailto:mr.g...@gmail.com>

Christian Williams

unread,
Sep 15, 2009, 2:52:23 PM9/15/09
to jasmi...@googlegroups.com
It could be that window.eval(xxx) vs eval(xxx, window) issue I
suppose...?

--X [typos courtesy of my iPhone]

Michael Gaffney

unread,
Sep 15, 2009, 2:58:09 PM9/15/09
to jasmi...@googlegroups.com
Christian,
There are 2 seperate issues here I think. When I load it in firebug I
don't see any errors.

I think Davis was asking me about the Rake issue, which now that I
pulled again seems to be working.

SOOOO....

It looks like when I run rake jasmine:server and hit the url, things
are working (it ran the full suite).

However, when I open firefox and browse to
examples/html/example_suite.html on the filesystem, it doesn't run any
tests.

Or am I not supposed to run the example_suite.html directly through the
browser?

-Mike

Rajan Agaskar

unread,
Sep 15, 2009, 4:03:11 PM9/15/09
to jasmi...@googlegroups.com
Response inlined:

On Tue, Sep 15, 2009 at 11:58 AM, Michael Gaffney <mr.g...@gmail.com> wrote:

Christian,
       There are 2 seperate issues here I think. When I load it in firebug I
don't see any errors.

       I think Davis was asking me about the Rake issue, which now that I
pulled again seems to be working.

       SOOOO....

       It looks like when I run rake jasmine:server and hit the url, things
are working (it ran the full suite).


Great!

 
       However, when I open firefox and browse to
examples/html/example_suite.html on the filesystem, it doesn't run any
tests.

Or am I not supposed to run the example_suite.html directly through the
browser?


There is an error in this file on line 12. It reads:
" jasmine.include('example_suite.js', true);"

it *should* read:
" jasmine.include('spec/example_suite.js', true);"

If you re-pull, this has been fixed on master.

Sorry, Mike, you seem to have hit the regression jackpot. Thanks for sticking in there. Let us know if you experience any more trouble.

Rajan

Michael Gaffney

unread,
Sep 15, 2009, 4:28:10 PM9/15/09
to jasmi...@googlegroups.com
It just feels to be verified 'not an idiot'. :)

Yep, head works.

Thanks Rajan.

-Mike

Christian Williams

unread,
Sep 15, 2009, 4:30:41 PM9/15/09
to jasmi...@googlegroups.com
Mea culpa for the half-assed merge a week ago, sorry all.

--X [typos courtesy of my iPhone]

Michael Gaffney

unread,
Sep 23, 2009, 5:45:37 PM9/23/09
to jasmi...@googlegroups.com
What's the best way to mock out / spy on openDatabase with jasmine?

-=gaffo=-

Rajan Agaskar

unread,
Sep 24, 2009, 4:25:36 PM9/24/09
to jasmi...@googlegroups.com
Michael:

I don't have any personal experience with openDatabase, but let me give you a couple tips that may help get you where you need to be.

If there is an instance method on a class you want to stub out, it is possible to do something like:

spyOn(MyObject.prototype, 'myMethod');

andReturn, andCallFake, etc are all available on this spy.

In your case, it looks like there is a openDatabase object that lives in the window that you can call from Safari. In this case what you would want to do is
spyOn(window, 'openDatabase');

(this sort of trick is also useful for spying on other things that might live in the global namespace).

On other javascript projects I've worked on, we found it useful to write a fake for the database that would store objects to an in-memory hash rather than writing a spy every time we needed to use the database. A cautionary note: we made some incorrect assumptions about how objects were re-hydrated from the database, which led to some bugs in our code. Check how things like objects and functions come back from the db.

Rajan

Michael Gaffney

unread,
Sep 24, 2009, 4:38:05 PM9/24/09
to jasmi...@googlegroups.com
Good pointers. I was trying to avoid the 'window' as it assumes you are
in a browser but I guess that's a decent assumption.

I ended up making an object that just wrapps the openDatabase call and
spy'd on that.

So how would I spy on a constructor?

Thanks!

-Mike

Rajan Agaskar

unread,
Sep 25, 2009, 4:18:33 PM9/25/09
to jasmi...@googlegroups.com
You can actually spy on a constructor in much the same fashion;

spyOn(window, Constructor);

usually you'll want to return an object, so you'd probably

spyOn(window, 'Constructor').andReturn(new MyFakeObj());

There are some issues with this method; we've got a story in the backlog about finding a better way to do this.

Michael Gaffney

unread,
Sep 25, 2009, 4:25:24 PM9/25/09
to jasmi...@googlegroups.com
Cool,
I was actually thinking of building a test layer with rollback like
rails has for the database testing. Not sure if it's impossible to do
with async calls in a test.

-Mike
Reply all
Reply to author
Forward
0 new messages