[rspec-users] Problem with rendering JSON

Matt Wynne matt at mattwynne.net
Sun Feb 13 06:39:55 EST 2011


On 12 Feb 2011, at 12:49, Ants Pants wrote:

> 
> 
> On 11 February 2011 23:45, Matt Wynne <matt at mattwynne.net> wrote:
> 
> On 11 Feb 2011, at 16:04, Ants Pants wrote:
> 
>> You are my last resort for solving this issue as I have tried and tried to solve it myself but can't.
>> 
>> I'm not even sure if it's an RSpec issue but the thing is, if I run the request from the browser, it works. Sadly, my test fails. So either there's something about Rails I don't know about (but should) or I'm doing something wrong in RSpec.
>> 
>> Here is my test ....
>> 
>> let(:charity) { mock_model(Charity).as_null_object }
>> ...
>> context "params[:term] has data" do
>>   it "calls Charity::fulltext_search" do
>>     Charity.should_receive(:fulltext_search).with(String).and_return([charity])
> 
> Why are you using the class String here, instead of an actual sample term?
> 
>>     xhr :get, :autocomplete_index, :term => String, :format => :json
> 
> ditto.
> 
>>   end
>> end
>> 
>> And this is my controller ....
>> 
>> def autocomplete_index
>>   @charities = (params[:term].blank?) ? [] : Charity.fulltext_search(params[:term])
>>   render :json => @charities   ##.to_json(:only => [:name, :official_website])
>> end
>> 
>> As I said, this works from the browser but I get into a 'stack level too deep' with RSpec.
>> 
>> I looked in to gems/actionpack-2.3.8/lib/action_view/paths.rb and it's responsible for finding the template.
>> 
>> It raises MissingTemplate.new(self, original_template_path, format) and I printed the original_template_path (charities/) and the format (json)
>> 
>> If anyone can me in the right direction, I would be v grateful. Thank you.
>> 
>> -ants
>> 
>> This is a snippet of the output (the bit between [[ ]] is repeated until the program quits with the 'stack level too deep' message. 
>> 
>> [[
>> gems/activesupport-2.3.8/lib/active_support/json/encoders/hash.rb:37:in `each'
>> gems/activesupport-2.3.8/lib/active_support/json/encoders/hash.rb:37:in `map'
>> gems/activesupport-2.3.8/lib/active_support/json/encoders/hash.rb:37:in `to_json'
>> gems/activesupport-2.3.8/lib/active_support/json/encoding.rb:79:in `encode'
>> gems/activesupport-2.3.8/lib/active_support/json/encoding.rb:18:in `__send__'
>> gems/activesupport-2.3.8/lib/active_support/json/encoding.rb:18:in `encode'
>> gems/activesupport-2.3.8/lib/active_support/json/encoders/object.rb:4:in `to_json'
>> gems/activesupport-2.3.8/lib/active_support/json/encoding.rb:79:in `encode'
>> gems/activesupport-2.3.8/lib/active_support/json/encoding.rb:18:in `__send__'
>> gems/activesupport-2.3.8/lib/active_support/json/encoding.rb:18:in `encode'
>> gems/activesupport-2.3.8/lib/active_support/json/encoders/hash.rb:38:in `to_json'
>> gems/actionpack-2.3.8/lib/action_view/paths.rb:74:in `map'
>> ]]
>> gems/activesupport-2.3.8/lib/active_support/json/encoders/hash.rb:37:in `each'
>> gems/activesupport-2.3.8/lib/active_support/json/encoders/hash.rb:37:in `map'
>> gems/activesupport-2.3.8/lib/active_support/json/encoders/hash.rb:37:in `to_json'
>> gems/activesupport-2.3.8/lib/active_support/json/encoding.rb:79:in `encode'
>> gems/activesupport-2.3.8/lib/active_support/json/encoding.rb:18:in `__send__'
>> gems/activesupport-2.3.8/lib/active_support/json/encoding.rb:18:in `encode'
>> gems/activesupport-2.3.8/lib/active_support/json/encoders/object.rb:4:in `to_json'
>> gems/activesupport-2.3.8/lib/active_support/json/encoding.rb:79:in `encode'
>> gems/activesupport-2.3.8/lib/active_support/json/encoding.rb:18:in `__send__'
>> gems/activesupport-2.3.8/lib/active_support/json/encoding.rb:18:in `encode'
>> gems/activesupport-2.3.8/lib/active_support/json/encoders/enumerable.rb:11:in `to_json'
>> gems/activesupport-2.3.8/lib/active_support/json/encoders/enumerable.rb:11:in `map'
>> gems/activesupport-2.3.8/lib/active_support/json/encoders/enumerable.rb:11:in `to_json'
>> gems/activesupport-2.3.8/lib/active_support/json/encoding.rb:79:in `encode'
>> gems/activesupport-2.3.8/lib/active_support/json/encoding.rb:18:in `__send__'
>> gems/activesupport-2.3.8/lib/active_support/json/encoding.rb:18:in `encode'
>> gems/actionpack-2.3.8/lib/action_controller/base.rb:954:in `render_without_benchmark'
>> gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:51:in `render'
>> gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in `ms'
>> gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in `ms'
>> gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:51:in `render'
>> /home/anthony/Development/websites/ruby/project/trunk/app/controllers/charities_controller.rb:23:in `autocomplete_index'
>> gems/actionpack-2.3.8/lib/action_controller/base.rb:1331:in `send'
>> gems/actionpack-2.3.8/lib/action_controller/base.rb:1331:in `perform_action_without_filters'
>> gems/actionpack-2.3.8/lib/action_controller/filters.rb:617:in `call_filters'
>> gems/actionpack-2.3.8/lib/action_controller/filters.rb:610:in `perform_action_without_benchmark'
>> gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
>> gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in `ms'
>> gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in `ms'
>> gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
>> gems/actionpack-2.3.8/lib/action_controller/rescue.rb:160:in `perform_action_without_flash'
>> gems/actionpack-2.3.8/lib/action_controller/flash.rb:151:in `perform_action'
>> gems/actionpack-2.3.8/lib/action_controller/base.rb:532:in `send'
>> gems/actionpack-2.3.8/lib/action_controller/base.rb:532:in `process_without_filters'
>> gems/actionpack-2.3.8/lib/action_controller/filters.rb:606:in `process'
>> gems/actionpack-2.3.8/lib/action_controller/test_process.rb:567:in `process_with_test'
>> gems/actionpack-2.3.8/lib/action_controller/test_process.rb:447:in `process'
>> gems/actionpack-2.3.8/lib/action_controller/test_process.rb:398:in `get'
>> gems/actionpack-2.3.8/lib/action_controller/test_process.rb:453:in `__send__'
>> gems/actionpack-2.3.8/lib/action_controller/test_process.rb:453:in `xhr'
>> 
>> 
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users
> 
> cheers,
> Matt
> 
> matt at mattwynne.net
> 07974 430184
> 
> 
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
> 
> Hi,
> 
> The reason why I'm using String is because I'm not bothered about return values and I'm using it more as a bit more documentation - fulltext_search accepts a string and returns an array of charity records.
> 
> Also, in this example, I don't even need with() nor and_return() as all I'm really interested in is if fulltext_search is called.
> 
> If I'm interested in the return value, then I will use real values. Otherwise I'll just use generic Fixnum/String etc. 
> 
> Now I've explained that, am I using it wrong?  For me, it works but like I say, If I'm doing something dangerously wrong, then I will change my way of working.

Well, the error you're getting seems to be coming from deep within the bowels of Rails, where it's trying to encode a hash as JSON. I have no idea what that code will try to do if you give it a String class as a value within the Hash, but I can imagine it might cause the kind of weirdness you're experiencing.

What I'd do in a simliar situation is to use a more realistic value (i.e. something that actually is an instance of String) that still explains to the reader that it's of low importance to the test case. For example:

    Charity.should_receive(:fulltext_search).with('a query term').and_return([charity])

Sometimes I even push these off into let blocks.


cheers,
Matt

matt at mattwynne.net
07974 430184

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110213/a3c1a652/attachment.html>


More information about the rspec-users mailing list