How to stub jquery dialog method

967 views
Skip to first unread message

niki4810

unread,
Sep 5, 2012, 11:12:06 PM9/5/12
to sin...@googlegroups.com
Hi,

I have a set up where one of my function calls $.dialog() and renders the dom element as a model dialog. More specifically, I use $.dialog("open) to launch my dialog, I want to prevent this call i.e. $(el).dialog("open") from happening when i am writing a unit test to test the dialog elements.  Is there a way to stub out the $(el).dialog("open") using sinon stub ?

pesudo code:


Thanks

MRoderick

unread,
Sep 6, 2012, 2:08:45 AM9/6/12
to Sinon.JS
Hello,

You should be able to stub it with sinon.stub($.fn, 'dialog')

The secret is that in jQuery functions on return values from the
jQuery function are stored under the 'fn' property.

/Morgan

niki4810

unread,
Sep 6, 2012, 2:09:48 PM9/6/12
to sin...@googlegroups.com
Hi Morgan,

Thanks for your reply. I did  try out your suggestion, it seemed to stub the jQuery dialog functionality but my requirement is a little different, i.e. I wanted to stub out the call to $().dialog("open") to prevent the dailog form getting launched.

I did try this,

stub = sinon.stub( $.fn, 'dialog' );

stub.withArgs("open" ).returns("");

but this dint seem to work. Do you have any suggestions/recommendations to just stub out $().dialog("open") while still rendering a dialog an in-memory?

Thanks





but this is overriding the entire dialog functionality. I want my dialog to get rendered but, i dont want to lauch

Christian Johansen

unread,
Sep 6, 2012, 3:19:47 PM9/6/12
to sin...@googlegroups.com
but this is overriding the entire dialog functionality. I want my dialog to get rendered but, i dont want to lauch

You have to look into how the dialog is implemented in order to stub out only parts of it.

Christian
 


On Wednesday, September 5, 2012 11:08:46 PM UTC-7, MRoderick wrote:
Hello,

You should be able to stub it with sinon.stub($.fn, 'dialog')

The secret is that in jQuery functions on return values from the
jQuery function are stored under the 'fn' property.

/Morgan

On Sep 6, 5:12 am, niki4810 <nikhiles...@gmail.com> wrote:
> Hi,
>
> I have a set up where one of my function calls $.dialog() and renders the
> dom element as a model dialog. More specifically, I use $.dialog("open) to
> launch my dialog, I want to prevent this call i.e. $(el).dialog("open")
> from happening when i am writing a unit test to test the dialog elements.
>  Is there a way to stub out the $(el).dialog("open") using sinon stub ?
>
> pesudo code:
>
> Thanks



--
MVH
Christian

niki4810

unread,
Sep 6, 2012, 6:17:01 PM9/6/12
to sin...@googlegroups.com, chri...@cjohansen.no

Hi Christian.

I am not sure I got that, i am using a jquery UI dialog for rendering a dialog.

Thanks,

Nikhilesh

Christian Johansen

unread,
Sep 9, 2012, 1:57:33 PM9/9/12
to sin...@googlegroups.com
Sorry. To answer your question more directly: It's not really possible to stub a function only for specific arguments.
--
MVH
Christian

niki4810

unread,
Sep 10, 2012, 12:23:10 PM9/10/12
to sin...@googlegroups.com, chri...@cjohansen.no
Thanks Christian.
Reply all
Reply to author
Forward
0 new messages