Patrick J. Collins
2012-Sep-26 16:16 UTC
[rspec-users] what would be the best way to stub out an XHR request''s response?
If I have a javascript ajax request happening that fetches json data from a remote service, and I want to be running an integration test with rspec-- what would be the best way to stub out that service? The ideas that I''ve come up is: manually add a route + open up application controller from within my spec and dynamically create an action that returns mock json... Then make my .js a .js.erb file, and do something like: $.ajax({ url: <%= Rails.env.test? ? "/my_added_route_that_hits_the_controller_method_that_returns_json" : "http://somewebservice.com/feed" %> }); Can anyone come up with any better solutions? Patrick J. Collins http://collinatorstudios.com
Ervin Weber
2012-Sep-26 17:26 UTC
[rspec-users] what would be the best way to stub out an XHR request''s response?
You could create some function (say window.GiveMeUrlForAjax) that returns uri for ajax request, your test could then easily overwrite that function before triggering event to perform ajax. Also you might consider creating a function that returns data (in production it would take url from "GiveMeUrlForAjax" and do real ajax)" so you could "stub" it in unit tests also. In my opinion it is good to avoid all "if test?" statements in production code (you always can refactor them all into single config file). On Wed, Sep 26, 2012 at 7:16 PM, Patrick J. Collins <patrick at collinatorstudios.com> wrote:> If I have a javascript ajax request happening that fetches json data > from a remote service, and I want to be running an integration test with > rspec-- what would be the best way to stub out that service? > > The ideas that I''ve come up is: > > manually add a route + open up application controller from within my > spec and dynamically create an action that returns mock json... Then > make my .js a .js.erb file, and do something like: > > $.ajax({ url: <%= Rails.env.test? ? > "/my_added_route_that_hits_the_controller_method_that_returns_json" : > "http://somewebservice.com/feed" %> }); > > Can anyone come up with any better solutions? > > Patrick J. Collins > http://collinatorstudios.com > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Adam Sroka
2012-Sep-26 17:36 UTC
[rspec-users] what would be the best way to stub out an XHR request''s response?
I would just wrap the method that makes the ajax call and spec it from Jasmine. Then you can just stub the wrapper to return the data you want and expect the right thing to happen in the callback method (or however the data gets stored/displayed.) On Wed, Sep 26, 2012 at 10:26 AM, Ervin Weber <webervin at gmail.com> wrote:> You could create some function (say window.GiveMeUrlForAjax) that > returns uri for ajax request, your test could then easily overwrite > that function before triggering event to perform ajax. Also you might > consider creating a function that returns data (in production it would > take url from "GiveMeUrlForAjax" and do real ajax)" so you could > "stub" it in unit tests also. > In my opinion it is good to avoid all "if test?" statements in > production code (you always can refactor them all into single config > file). > > On Wed, Sep 26, 2012 at 7:16 PM, Patrick J. Collins > <patrick at collinatorstudios.com> wrote: >> If I have a javascript ajax request happening that fetches json data >> from a remote service, and I want to be running an integration test with >> rspec-- what would be the best way to stub out that service? >> >> The ideas that I''ve come up is: >> >> manually add a route + open up application controller from within my >> spec and dynamically create an action that returns mock json... Then >> make my .js a .js.erb file, and do something like: >> >> $.ajax({ url: <%= Rails.env.test? ? >> "/my_added_route_that_hits_the_controller_method_that_returns_json" : >> "http://somewebservice.com/feed" %> }); >> >> Can anyone come up with any better solutions? >> >> Patrick J. Collins >> http://collinatorstudios.com >> >> _______________________________________________ >> rspec-users mailing list >> rspec-users at rubyforge.org >> http://rubyforge.org/mailman/listinfo/rspec-users > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users
Patrick J. Collins
2012-Sep-26 18:26 UTC
[rspec-users] what would be the best way to stub out an XHR request''s response?
> I would just wrap the method that makes the ajax call and spec it from > Jasmine. Then you can just stub the wrapper to return the data you > want and expect the right thing to happen in the callback method (or > however the data gets stored/displayed.)Sorry, I am not sure if I am understanding you correctly... Are you saying, in the test environment, load jasmine via <script> tags, and use it to stub out the method that call that does the ajax call? Patrick J. Collins http://collinatorstudios.com
Adam Sroka
2012-Sep-26 18:34 UTC
[rspec-users] what would be the best way to stub out an XHR request''s response?
On Wed, Sep 26, 2012 at 11:26 AM, Patrick J. Collins <patrick at collinatorstudios.com> wrote:>> I would just wrap the method that makes the ajax call and spec it from >> Jasmine. Then you can just stub the wrapper to return the data you >> want and expect the right thing to happen in the callback method (or >> however the data gets stored/displayed.) > > Sorry, I am not sure if I am understanding you correctly... Are you > saying, in the test environment, load jasmine via <script> tags, and use > it to stub out the method that call that does the ajax call? >That could work. What I would actually do is call jamine-node from a rake task, because I''m hoping I don''t really need the full stack to spec this behavior. Using rspec to test that a callback happens in a page sounds like it would be slower than I''d like.
Ervin Weber
2012-Sep-27 08:16 UTC
[rspec-users] what would be the best way to stub out an XHR request''s response?
On Wed, Sep 26, 2012 at 9:34 PM, Adam Sroka <adam.sroka at gmail.com> wrote:> On Wed, Sep 26, 2012 at 11:26 AM, Patrick J. Collins > <patrick at collinatorstudios.com> wrote: >>> I would just wrap the method that makes the ajax call and spec it from >>> Jasmine. Then you can just stub the wrapper to return the data you >>> want and expect the right thing to happen in the callback method (or >>> however the data gets stored/displayed.) >> >> Sorry, I am not sure if I am understanding you correctly... Are you >> saying, in the test environment, load jasmine via <script> tags, and use >> it to stub out the method that call that does the ajax call? >> > > That could work. What I would actually do is call jamine-node from a > rake task, because I''m hoping I don''t really need the full stack to > spec this behavior. Using rspec to test that a callback happens in a > page sounds like it would be slower than I''d like. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-usersActually I assumed rspec integration tests, say using selenium or something. In such case you could use something similar to the way we stub confirm method: page.evaluate_script(''window.confirm = function() { return true; }'') to simply override some functions before ajax is sent out.