Problem with rendering JSON

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])
xhr :get, :autocomplete_index, :term => String, :format => :json
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:inmap’
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:inencode’
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:inencode’
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:inencode’
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:inencode’
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:inmap’*
]]
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:inmap’
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:inencode’
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:inencode’
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:inencode’
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:inencode’
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:inmap’
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:inencode’
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:inencode’
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:inrender’
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:inms’
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:inautocomplete_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:inperform_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:inperform_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:inms’
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:inperform_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:inperform_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:inprocess_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:inprocess_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:inget’
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:inxhr’

On 11 Feb 2011, at 16:04, Ants P. wrote:

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.

As I said, this works from the browser but I get into a ‘stack level too deep’
with RSpec.

gems/activesupport-2.3.8/lib/active_support/json/encoding.rb:18:in encode' gems/activesupport-2.3.8/lib/active_support/json/encoding.rb:79:in encode’
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’


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

cheers,
Matt

[email protected]
07974 430184

On 11 February 2011 23:45, Matt W. [email protected] wrote:

Why are you using the class String here, instead of an actual sample term?

I looked in to gems/actionpack-2.3.8/lib/action_view/paths.rb and it’s
the program quits with the ‘stack level too deep’ message.
__send__' ]] gems/activesupport-2.3.8/lib/active_support/json/encoders/object.rb:4:in to_json’
ms' gems/actionpack-2.3.8/lib/action_controller/benchmarking.rb:68:in perform_action’
`send
Matt

[email protected]
07974 430184


rspec-users mailing list
[email protected]
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.

-ants

On 12 Feb 2011, at 12:49, Ants P. wrote:

Here is my test …

end

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:inencode’
gems/actionpack-2.3.8/lib/action_controller/base.rb:954:in
`render_without_benchmark’

gems/activesupport-2.3.8/lib/active_support/core_ext/benchmark.rb:17:in ms' gems/actionpack-2.3.8/lib/action_controller/test_process.rb:453:insend

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

[email protected]
07974 430184