Skip to content

Make sure the Proxy being returned is actually for this object.

Created by: jshraibman-mdsol

I was getting failures in my helper specs (seems like the same problem mentioned here). I couldn't call a mocked method on my object right after defining it:

        helper_obj = helper
        helper_obj.stub(:blahblah).and_return( 'blah')
        helper_obj.blahblah.should == 'blah'

I eventually traced the issue:

  • helper is really an alias of view
  • When view is called ActionView::TestCase::TestController passes all its instance variables, including @mock_proxy, into the new view
  • Calls to stub were adding stubs to the proxy for the controller, not the object I was calling stub on.

I tried a fancier solution than this that hid the proxy in the eigenclass of the eigenclass but that resulted in a spec failure:

  1) RSpec::Mocks::Serialization marshals the same with and without stubbing
     Failure/Error: after = Marshal.dump(serializable_object)
     TypeError:
       singleton can't be dumped
     # ./spec/rspec/mocks/serialization_spec.rb:99:in `block (2 levels) in <module:Mocks>'

This solution seems a little ugly but it works.

rake passes with these changes:

Finished in 0.34682 seconds
691 examples, 0 failures, 2 pending

47 scenarios (47 passed)
151 steps (151 passed)
0m34.032s

I considered adding a test case for this but it seemed a little weird to me to be copying the proxy from one object to another in a spec. Do you think I should?

Merge request reports