I am currently following a tutorial but Rails and Rspec have evolved, especially for writing tests.
My goal is to test that when I visit the page "http://domain.fr/users/1" the page title follow the format : "#{base_title} | #{@user.name}" where base_title is constant.
Before, I saw it was possible to use render_views in controller specs but it is not the best way and it does not exist anymore in Rails 4/RSpec 3.
My last try is :
require 'rails_helper'
describe "users/show.html.erb", type: :view do
it "Shoud finally render a correct title" do
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 an helper for rendering in application.html.erb : <title><%= title %></title>
Here is the helper :
def title
base_title = "Simple App du Tutoriel Ruby on Rails"
@title.nil? ? base_title : "#{base_title} | #{@title}"
end
And the show method from users_controller.rb :
def show
@user = User.find(params[:id])
@title = @user.name
end
I also added resources :users to my routes.rb file.
For FactoryGirl here is user.rb :
FactoryGirl.define do
factory :user do
name "Antoine Foucault"
email "a@b.c"
password "password"
password_validation "password"
end
end
The above test fail because only the constant part of the title is rendered. Thus, I think Users#show is not called and @title not defined but I don't see how to achieve this.
Thanks for your help :)
Aucun commentaire:
Enregistrer un commentaire