ActiveSupport::JSON::Encoding::CircularReferenceErrorHere is the relevant controller code:
format.json do render :json => as_json(@questions) endHere is the test's mock:
question = mock_model(Question)A little cryptic, right? After a little digging, I changed the mock to this:
question = mock_model(Question, :as_json => {'foo' => 'bar'})Ah, there we go!
The spec ended up looking like this:
context "as json" do it "lists the questions" do question = mock_model(Question, :as_json => {'foo' => 'bar'}) Question.should_receive(:find_ordered_subjects).and_return([question]) get :index, :format => 'json' response.body.should == "[{\"foo\":\"bar\"}]" end endIn our application_controller.rb, an as_json() method called to_json, which would call as_json in the test, resulting in a circular reference. Oops.
Don't forget to stub as_json()!
No comments:
Post a Comment