Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with create/update namespace responders #193

Closed
jonlhouse opened this issue Jan 5, 2012 · 3 comments
Closed

Problem with create/update namespace responders #193

jonlhouse opened this issue Jan 5, 2012 · 3 comments

Comments

@jonlhouse
Copy link

I'm trying to track down this problem with rspec testing namespaced IR controllers. I thought it could be with Responders but I've tried with an without them.

Note: These work in development environment in a browser it just the test cases that fail.

# in app/controllers/abc/encounters_controller.rb
Abc::EncountersController < Abc::ApplicationController
  inherited_resources
end

Here is the RSpec2 controller test (note... I'm only including one failing case. Only the update/create actions fail). These are basically the scaffold test cases.

# in spec/controllers/abc/encounters_controller_spec.rb
describe Abc::EncountersController do
  login_user    # from devise -- this seems to work with the rest of my tests

  describe "PUT update" do
    describe "with invalid params" do
       it "re-renders the 'edit' template" do
          encounter = Encounter.create! valid_attributes
          # Trigger the behavior that occurs when invalid params are submitted
          Encounter.any_instance.stub(:save).and_return(false)
          put :update, :id => encounter.id, :encounter => {}
          response.should render_template("edit")
      end
   end
end

Now the error I'm getting is undefined methodencounter_url' for #Smd::EncountersController:0x00000005dd2c50` as its not correctly prepending the 'abc_' namespace to the redirect url.

Here is the portion of the stack trace I think is critical:

     # /home/jonlhouse/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.3/lib/action_controller/metal/instrumentation.rb:59:in `redirect_to'
     # /home/jonlhouse/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.3/lib/action_controller/metal/responder.rb:135:in `redirect_to'
     # /home/jonlhouse/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.3/lib/action_controller/metal/responder.rb:193:in `navigation_behavior'
     # /home/jonlhouse/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.3/lib/action_controller/metal/responder.rb:162:in `rescue in to_html'
     # /home/jonlhouse/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.3/lib/action_controller/metal/responder.rb:160:in `to_html'
     # /home/jonlhouse/.rvm/gems/ruby-1.9.2-p0/gems/responders-0.6.4/lib/responders/flash_responder.rb:93:in `to_html'
     # /home/jonlhouse/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.3/lib/action_controller/metal/responder.rb:153:in `respond'
     # /home/jonlhouse/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.3/lib/action_controller/metal/responder.rb:146:in `call'
     # /home/jonlhouse/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.1.3/lib/action_controller/metal/mime_responds.rb:238:in `respond_with'
     # /home/jonlhouse/.rvm/gems/ruby-1.9.2-p0/gems/inherited_resources-1.3.0/lib/inherited_resources/base_helpers.rb:262:in `respond_with_dual_blocks

Again its only a problem in RSpec and only on POST and PUT actions that are namespaced. Thanks.

@geneshuman
Copy link

Have you resolved this issue? I'm running into the exact same problem.

@ryandrake08
Copy link

I am experiencing the same issue. The only work-around I have found is to simply disable the tests. Interestingly, everything seems to work on the actual site, i.e. the "edit" template does get rendered.

Yet, response.should render_template("edit") (for update) and response.should render_template("new") (for create) tests fail.

@rafaels
Copy link

rafaels commented Mar 8, 2013

I think I figured out why it is happening. The problem is we are stubbing only the save method and the controller will use respond_with to handle the response.

The catch is that respond_with uses to_format, that will check if get? || !has_errors? || response_overridden?. If true, respond with default_renderer, else, display errors.

The request isn't a get, neither has_errors, and was not overriden. So, it will try to call url_for with our object (an instance of Encounter) that was not saved, but has no errors and persisted? is false.

I think it will, somehow, be passed to url_for, that relies on the class name. Encounter is not scoped, and url_for(Encouter.first) will try to call encouter_url(@encounter.id)

A solution is to stub Encouter.any_instance.stub(:errors).and_return('whatever').

The key concept is that respond_with relies on object errors, instead of the return of save.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants