JsMockito

74 views
Skip to first unread message

Mark Nijhof

unread,
Apr 19, 2010, 5:44:02 PM4/19/10
to js...@googlegroups.com
Hi,

Is the build in stub functionality as rich as f.ex. JsMockito? I
really like syntax and such, also the ability to spy on other code,
without replacing it with an mock is interesting.

http://jsmockito.org/

-Mark

--
Mark Nijhof
Cre8ive Thought
m: 0047 95 00 99 37
e: mark....@cre8ivethought.com
w: www.cre8ivethought.com

--
JSpec thanks you for your interest and support! To post simply reply
to this email.

documentation: http://jspec.info
unsubscribe: jspec+un...@googlegroups.com
group: http://groups.google.com/group/jspec

vision media [ Tj Holowaychuk ]

unread,
Apr 19, 2010, 6:07:03 PM4/19/10
to js...@googlegroups.com
yup:

    obj.stub('someMethod').and_return({ some: 'object'})

will stub it for that spec only, then restore the old behavior. 

also:

    obj.should.receive('someMethod', 'once').with_args(an_instance_of(Array)).and_return('something')

etc
--
Tj Holowaychuk
Vision Media
President & Creative Lead

Mark Nijhof

unread,
Apr 19, 2010, 6:15:15 PM4/19/10
to js...@googlegroups.com
If I wanted to use JsMockito then I have to import it into JSpec, what
namespace should it import its methods into?

I will look more into the build in stub as well do.

-Mark

vision media [ Tj Holowaychuk ]

unread,
Apr 19, 2010, 6:33:38 PM4/19/10
to js...@googlegroups.com
Uhhmmm im not to sure how they support frameworks but you would probably
want to merge them into JSpec.defaultContext

Mark Nijhof

unread,
Apr 19, 2010, 6:43:24 PM4/19/10
to js...@googlegroups.com
I am trying mimick the spy stuff using stub and am running into a
small problem, I am trying not to replace the method but I just want
to be able to check that it is only called once. So I tried this:

jQuery.templateLoader.stub('makeAjaxCall')

but that makes the method do nothing anymore, I still want it to work,
so I tried the following:

jQuery.templateLoader.stub('makeAjaxCall').and_return(jQuery.templateLoader.makeAjaxCall)

But no avail.

Btw using jQuery shortcut $ doesn't work in the stub :)

$.templateLoader.stub('templateLoader') becomes this .templateLoader....

-Mark

vision media [ Tj Holowaychuk ]

unread,
Apr 19, 2010, 7:03:24 PM4/19/10
to js...@googlegroups.com
stubbing is way different. I call it a "proxy assertion", but you would do:

    jQuery.templateLoader.should.receive('makeAjaxCall').with_args('whatever').and_return('something')

both with_args() and and_return() are optional

also you can do:

    foo.should.receive('something', 'twice')

or

    foo.should.receive('something', 5)

'once' and 'twice' are accepted over numbers to make things more readable, but
they are the only ones mapped to ints

Mark Nijhof

unread,
Apr 19, 2010, 7:18:43 PM4/19/10
to js...@googlegroups.com
Ah very nice!

I didn't get the JsMockito to work, it worked halfway meaning that I
could call the spy and verify method, but the chain was broken after
that. So verify(function).times(2) told me that times was not a
function. They have some nice syntax, but yours works very well as
well.

Thanks for your time! Already written 105 tests TDD way and have found
lots of value in having them! I added a location.reload every 2500
miliseconds which works great. Will blog about it soon :-)

-Mark


On Tue, Apr 20, 2010 at 1:03 AM, vision media [ Tj Holowaychuk ]

vision media [ Tj Holowaychuk ]

unread,
Apr 19, 2010, 7:23:49 PM4/19/10
to js...@googlegroups.com
no problem!

chrisleishman

unread,
May 25, 2010, 9:46:34 AM5/25/10
to jspec
You're not using the correct syntax for JsMockito. It should be
"verify(function, times(2))();", assuming that you're not wanting to
match any of the arguments passed to the function.

cheers,
chris


On Apr 20, 1:18 am, Mark Nijhof <mark.nij...@cre8ivethought.com>
wrote:
> Ah very nice!
>
> I didn't get the JsMockito to work, it worked halfway meaning that I
> could call the spy and verify method, but the chain was broken after
> that. So verify(function).times(2) told me that times was not a
> function. They have some nice syntax, but yours works very well as
> well.
>
> Thanks for your time! Already written 105 tests TDD way and have found
> lots of value in having them! I added a location.reload every 2500
> miliseconds which works great. Will blog about it soon :-)
>
> -Mark
>
> On Tue, Apr 20, 2010 at 1:03 AM, vision media [ Tj Holowaychuk ]
>
>
>
>
>
> <t...@vision-media.ca> wrote:
> > stubbing is way different. I call it a "proxy assertion", but you would do:
>
> >  jQuery.templateLoader.should.receive('makeAjaxCall').with_args('whatever'). and_return('something')
> >> >>> e:  mark.nij...@cre8ivethought.com
> >> >>> w:www.cre8ivethought.com
>
> >> >>> --
> >> >>> JSpec thanks you for your interest and support! To post simply reply
> >> >>> to this email.
>
> >> >>> documentation:http://jspec.info
> >> >>> unsubscribe: jspec+un...@googlegroups.com
> >> >>> group:http://groups.google.com/group/jspec
>
> >> >> --
> >> >> Tj Holowaychuk
> >> >> Vision Media
> >> >> President & Creative Lead
>
> >> >> --
> >> >> JSpec thanks you for your interest and support! To post simply reply
> >> >> to this email.
>
> >> >> documentation:http://jspec.info
> >> >> unsubscribe: jspec+un...@googlegroups.com
> >> >> group:http://groups.google.com/group/jspec
>
> >> > --
> >> > Mark Nijhof
> >> > Cre8ive Thought
> >> > m: 0047 95 00 99 37
> >> > e:  mark.nij...@cre8ivethought.com
> >> > w:www.cre8ivethought.com
>
> >> --
> >> Mark Nijhof
> >> Cre8ive Thought
> >> m: 0047 95 00 99 37
> >> e:  mark.nij...@cre8ivethought.com
> >> w:www.cre8ivethought.com
>
> >> --
> >> JSpec thanks you for your interest and support! To post simply reply
> >> to this email.
>
> >> documentation:http://jspec.info
> >> unsubscribe: jspec+un...@googlegroups.com
> >> group:http://groups.google.com/group/jspec
>
> > --
> > Tj Holowaychuk
> > Vision Media
> > President & Creative Lead
>
> > --
> > JSpec thanks you for your interest and support! To post simply reply
> > to this email.
>
> > documentation:http://jspec.info
> > unsubscribe: jspec+un...@googlegroups.com
> > group:http://groups.google.com/group/jspec
>
> --
> Mark Nijhof
> Cre8ive Thought
> m: 0047 95 00 99 37
> e:  mark.nij...@cre8ivethought.com
Reply all
Reply to author
Forward
0 new messages