`render` method with explicit `template` needs request params in view specs #872

Closed
waiting-for-dev opened this Issue Dec 11, 2013 · 5 comments

Projects

None yet

3 participants

@waiting-for-dev

In a view spec, calling render method with explicit template option seems to go all the routing layer way before rendering the template.

For example, let's say we have a books controller and a new action, with its new template. If we are specifying something about that template, we can easily do:

describe `users/new` do
  let(:user) { double('user') }
  before :each do
    assign(:user, user)
  end
  it "has some text" do
    render
    expect(rendered).to have_content 'something'
  end
end

But if we want to describe something that is related with the layout, for example the title:

  it "has some title" do
    render template: 'users/new', layout: 'layouts/application'
    expect(rendered).to have_title 'Some title'
  end

That gives the error No route matches.... To make it pass, we must indicate the needed id parameter before:

  it "has some title" do
    controller.request.path_parameters[:user] = user
    render template: 'users/new', layout: 'layouts/application'
    expect(rendered).to have_title 'Some title'
  end

Is this behavior intended? In order to mix template and layout, is it needed to go through de routing stack? If so, maybe that should be advised in the docs. But I think ideally view specifications should be isolated always from the routing.

Thanks a lot.

@JonRowe
RSpec member

This is how the Rails test helpers work for controllers, as controllers are coupled to their routes you're required to have a valid route to trigger an action, once the action is complete you may then optionally assert on the output, we'd normally isolate views from the controllers as this is considered a sub optimal way to test views. Have you considered writing a view spec if all you care about is elements like this?

@JonRowe JonRowe closed this Dec 12, 2013
@waiting-for-dev

Hey, I found this issue precisely in a view spec. It is a call to render from a view spec, with the template parameter because the layout one is needed. If I don't use template parameter there is no problem, but I must use it because I also need the layout one.

@JonRowe JonRowe reopened this Dec 12, 2013
@JonRowe
RSpec member

Sorry, I misread your original comment because you talked about a controller, which is irrelevant to a view spec. I still believe this is a Rails issue, or even intended behaviour however, as there is very little of our code handling this.

@samphippen
RSpec member

@waiting-for-dev can you confirm this still affects you? Would you be able to provide a reproduction case of the following form:

1) rails new an app up, and commit it
2) do a second commit which contains all the stuff that causes the bug you're talking about

then push that to a repo and link it here.

@waiting-for-dev

Hey, sorry, not working in that anymore. It is from more than two years ago! ;) I think if nobody has any complain we can close it...

@samphippen samphippen closed this Feb 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment