Test "dynamic" page title in a view spec (Rails 4 + RSpec 3) -
i following tutorial rails , rspec have evolved, writing tests.
my goal test when visit page "http://domain.fr/users/1" page title follow format : "#{base_title} | #{@user.name}"
base_title constant.
before, saw possible use render_views in controller specs not best way , not exist anymore in rails 4/rspec 3.
my last try :
require 'rails_helper' describe "users/show.html.erb", type: :view "should render correct title" user = factorygirl.create(:user) assign(:user, user) render template: "users/show.html.erb", layout: "layouts/application.html.erb" expect(rendered).to have_selector("title", text: user.name) end end
i use helper rendering in application.html.erb : <title><%= title %></title>
here helper :
def title base_title = "simple app du tutoriel ruby on rails" @title.nil? ? base_title : "#{base_title} | #{@title}" end
and show method users_controller.rb :
def show @user = user.find(params[:id]) @title = @user.name end
i added resources :users
routes.rb file.
the above test fail because constant part of title rendered. thus, think users#show not called , @title not defined don't see how achieve this.
also goal avoid calling assign() each variable in view can become problematic when have lot of variables render.
thanks :)
you forgot assign title
:
assign(:user, user) assign(:title, user.name)
Comments
Post a Comment