Proxy assertion to JQuery AJAX method

20 views
Skip to first unread message

Alan Rubin

unread,
Jun 8, 2010, 3:10:35 PM6/8/10
to js...@googlegroups.com
Hi,

I'm trying to execute the following spec - as you can see, the name('') method changes the page name and executes a jquery ajax call.

it 'should change page name when asked for'
            jQuery.should.receive('ajax', 'once').with_args({data:'pageId=1&value=NewName', type:'post', url:'/ews_pages/set_data'})
            var page = new Page(1,'OldName')
            page.name('NewName')
            page.name().should.eql 'NewName'
end

I got the following error:
expected function (selector, context) { return new (jQuery.fn.init)(selector, context); }.ajax() to be called once, but was not calledTypeError: $.modal is undefined

I have searched the list for any clues, but I didn't find anything relevant - I found another thread that talk about using proxy assertions for JQuery methods but it is exactly the same way I do. Can someone help me ?

Thanks,
Alan

arubin

unread,
Jun 29, 2010, 7:14:40 AM6/29/10
to jspec
Anyone ? Is it ok to proxy a call to JQuery ? Do you recommend doing
it differently ?

Thanks,
Alan

On Jun 8, 10:10 pm, Alan Rubin <a...@frubin.com> wrote:
> Hi,
>
> I'm trying to execute the following spec - as you can see, the name('')
> method changes the page name and executes a jquery ajax call.
>
> it 'should change page name when asked for'
>             jQuery.should.receive('ajax',
> 'once').with_args({data:'pageId=1&value=NewName', type:'post',
> url:'/ews_pages/set_data'})
>             var page = new Page(1,'OldName')
>             page.name('NewName')
>             page.name().should.eql 'NewName'
> end
>
> I got the following error:
> *expected function (selector, context) { return new
> (jQuery.fn.init)(selector, context); }.ajax() to be called once, but was not
> called**TypeError: $.modal is undefined*

arubin

unread,
Jul 5, 2010, 5:38:28 AM7/5/10
to jspec
Ok, I have solved the issue and would like to share the solution with
you.

My problem was that even by creating a proxy call to the jQuery "post"
method, the original method is being executed at the end for result
comparison with the jspec ".receive" syntax. As I have not configured
to proxy an AJAX request, the AJAX call being executed is returning an
error. As I have a hook at jQuery (using $.ajaxSetup method) that
specifies that every AJAX error should create a modal with a
notification (using $.modal jquery plugin), I got the error presented
above (TypeError: $.modal is undefined).

To solve the issue, I have wrote the following:

it 'should save module information to server side'
var module = new Module('10','TestTitle')
jQuery.should.receive('post', 'once').with_args('/ews_widgets/
set_data', { widgetId : module.id(), widgetTitle: module.title(),
widgetCollapsed: module.collapsed(), widgetColorClass:
module.colorClass() })
mock_request().and_return('{}', 'application/json')
module.save()
end

It was not clear for me that Proxy Assertions indeed executes the
requested method... I thought it needs to behave as a Mock. So a final
question: If a need a Mock behavior with JSpec, what do you
recommend ? Using an external library or using JSpec's Method Stubbing
(although I understand stubbing is not mocking)) ?

Thanks,
Alan
Reply all
Reply to author
Forward
0 new messages