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

view specs doesn't render "current_page?" condition? #1347

Closed
PascalTurbo opened this issue Apr 4, 2015 · 2 comments
Closed

view specs doesn't render "current_page?" condition? #1347

PascalTurbo opened this issue Apr 4, 2015 · 2 comments
Labels

Comments

@PascalTurbo
Copy link

Hi There,

I'm trying to test a css class generated while using the current_page? helper:

RSpec.describe 'my_day/index.html.erb', type: :view do
  it 'should have an active link to my_days' do
    render template: 'my_day/index', layout: 'layouts/application'
    expect(rendered).to have_css('li.active a', text: 'MyDays')
  end
end

<li class="<%= 'active' if current_page?(controller: 'my_day', action: 'index') %>"><%= link_to('MyDays', my_day_index_path) %></li>

But this will never match.

So I tried setting the condition true:

<li class="<%= 'active' if true %>"><%= link_to('MyDays', my_day_index_path) %></li>

And this works. So I think that current_page? isn't evaluated correctly.

@cupakromer
Copy link
Member

😧 I can see why this behavior is surprising. TLDR; you will need to stub this helper in your spec ❤️:

allow(view).to receive(:current_page?).with(
  controller: 'my_day',
  action: 'index'
).and_return(true)

RSpec view specs are intended to allow testing view templates in isolation. When working with a view template in isolation the idea is to treat it as detached from a controller, action, or request path.

However, many applications tend to have a single template used by a single controller and action. RSpec tries to help with this by adding that information via a controller object: https://www.relishapp.com/rspec/rspec-rails/v/3-2/docs/view-specs/view-spec-infers-controller-path-and-action

The root of the issue is that current_page? doesn't use this information. Checking the current source shows this implementation of the helper only checks the request path.

I think we may be able to adjust things to accommodate this.

@fables-tales
Copy link
Member

Closing because of #1402

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

No branches or pull requests

3 participants