#19 ✓invalid
James Mead

[#22563] CircularReferenceError calling #to_json on a mocha-ified object

Reported by James Mead | January 1st, 2009 @ 07:49 PM

Date: 2008-10-26 19:09 Opener: James Mead

Bill Kocik reports:-

def do_something
 @something = Something.find(:first)
 render :json => @something.to_json

A test might be this:

context "when do_something is called" do
 setup do
   @something = Something.new()
   get :do_something
 should_respond_with :success

Because I stubbed a method on my @something object (and this also happens with expects()), I get this:

1) Error:
test: when do_something is called should respond with success. (SomethingControllerTest):
ActiveSupport::JSON::CircularReferenceError: object references itself

If I print out the object @something, I see that it contains an @mocha attribute that references the containing object, which is the cause of to_json's heartburn and the error. But that @mocha attribute is not present if I never call stubs() or expects() on the object that's going to be converted to JSON, and I do not get the error in that case. Can anyone show me what I'm doing wrong, or is this actually a known issue?

James Mead comments:-

It sounds like this is a bug and will be an issue with any object on which #stubs or #expects has been called. Depending on how #to_json is implemented it ought to be possible to fix it.

Comments and changes to this ticket

  • James Mead

    James Mead January 26th, 2009 @ 05:23 PM

    • State changed from “new” to “invalid”

    I can't reproduce this bug. I've run your test as Test::Unit tests with Rails 2.2, Mocha 0.9.4 and Ruby 1.8.6. The test passes.

    I'm marking this ticket as "invalid". If this is still causing you a problem, please give us some more details e.g. Rails version, RSpec version, Ruby version.

    Thanks, James.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

A mocking & stubbing library for Ruby.

* <a href="http://github.com/floehopper/mocha">GitHub repository</a>
* <a href="http://mocha.rubyforge.org">Documentation</a>
* <a href="http://groups.google.com/group/mocha-developer">Mailing List</a>

People watching this ticket