ruby on rails - RSpec tests broken after application i18n -
after finishing railstutorial.org have decided i18n application , suddently tests started failing following,
failures:
1) user pages index failure/error: sign_in user capybara::elementnotfound: unable find field "email" # ./spec/support/utilities.rb:5:in
sign_in' # ./spec/requests/user_pages_spec.rb:11:in
block (3 levels)
the changes have made where,
routes.rb
sampleapp::application.routes.draw scope '(:locale)' resources :users resources :sessions, only: [:new, :create, :destroy] root to: 'static_pages#home' match '/signup', to: 'users#new', via: 'get' match '/signin', to: 'sessions#new', via: 'get' match '/signout', to: 'sessions#destroy', via: 'delete' match '/help', to: 'static_pages#help', via: 'get' match '/about', to: 'static_pages#about', via: 'get' match '/contact', to: 'static_pages#contact', via: 'get' end
and config/initializers/i18n.rb
i18n.default_locale = :en languages = [ ['english', 'en'], ['portuguese', 'pt'] ]
before i18n routes looked following,
users_path /users(.:format) users#index post /users(.:format) users#create new_user_path /users/new(.:format) users#new edit_user_path /users/:id/edit(.:format) users#edit user_path /users/:id(.:format) users#show patch /users/:id(.:format) users#update put /users/:id(.:format) users#update delete /users/:id(.:format) users#destroy sessions_path post /sessions(.:format) sessions#create new_session_path /sessions/new(.:format) sessions#new session_path delete /sessions/:id(.:format) sessions#destroy root_path / static_pages#home signup_path /signup(.:format) users#new signin_path /signin(.:format) sessions#new signout_path delete /signout(.:format) sessions#destroy help_path /help(.:format) static_pages#help about_path /about(.:format) static_pages#about contact_path /contact(.:format) static_pages#contact
and now,
users_path (/:locale)/users(.:format) users#index post (/:locale)/users(.:format) users#create new_user_path (/:locale)/users/new(.:format) users#new edit_user_path (/:locale)/users/:id/edit(.:format) users#edit user_path (/:locale)/users/:id(.:format) users#show patch (/:locale)/users/:id(.:format) users#update put (/:locale)/users/:id(.:format) users#update delete (/:locale)/users/:id(.:format) users#destroy sessions_path post (/:locale)/sessions(.:format) sessions#create new_session_path (/:locale)/sessions/new(.:format) sessions#new session_path delete (/:locale)/sessions/:id(.:format) sessions#destroy root_path /(:locale)(.:format) static_pages#home signup_path (/:locale)/signup(.:format) users#new signin_path (/:locale)/signin(.:format) sessions#new signout_path delete (/:locale)/signout(.:format) sessions#destroy help_path (/:locale)/help(.:format) static_pages#help about_path (/:locale)/about(.:format) static_pages#about contact_path (/:locale)/contact(.:format) static_pages#contact
user_pages_spec.rb
require 'spec_helper' describe "user pages" subject { page } describe "index" let(:user) { factorygirl.create(:user) } before (:each) sign_in user visit users_path end { should have_title('all users') } { should have_content('all users') } describe "pagination" before(:all) { 30.times { factorygirl.create(:user) } } after(:all) { user.delete_all } { should have_selector('div.pagination') } "should list each user" user.paginate(page: 1).each |user| expect(page).to have_selector('li', text: user.name) end end end describe "delete links" { should_not have_link('delete') } describe "as admin user" let(:admin) { factorygirl.create(:admin) } before sign_in admin visit users_path end { should have_link('delete', href: user_path(user.first)) } "should able delete user" expect{ click_link('delete') }.to change(user, :count).by(-1) end { should_not have_link('delete', href: user_path(admin)) } end end end describe "profile page" let(:user) { factorygirl.create(:user) } before { visit user_path(user) } { should have_content(user.name) } { should have_title(user.name) } end describe "signup page" before { visit signup_path } { should have_content('sign up') } { should have_title(full_title('sign up')) } end describe "signup" before { visit signup_path } let(:submit) { "create account" } describe "with invalid information" "should not create user" expect { click_button submit }.not_to change(user, :count) end describe "after submission" before { click_button submit } { should have_title('sign up') } { should have_content('error') } end end describe "with valid information" before fill_in "name", with: "example user" fill_in "email", with: "user@example.com" fill_in "password", with: "password" fill_in "confirm password", with: "password" end "should create user" expect { click_button submit }.to change(user, :count).by(1) end describe "after saving user" before { click_button submit } let(:user) { user.find_by(email: 'user@example.com') } { should have_link('sign out') } { should have_title(user.name) } { should have_selector('div.alert.alert-success', text: 'welcome') } end end end describe "edit" let(:user) { factorygirl.create(:user) } before sign_in user visit edit_user_path(user) end describe "page" { should have_content("update profile") } { should have_title("edit user") } end describe "with invalid information" before { click_button "save changes" } { should have_content('error') } end describe "with valid information" let(:new_name) { "new name" } let(:new_email) { "new@example.com" } before fill_in "name", with: new_name fill_in "email", with: new_email fill_in "password", with: user.password fill_in "confirm password", with: user.password click_button "save changes" end { should have_title(new_name) } { should have_selector('div.alert.alert-success') } { should have_link('sign out', href: signout_path) } specify { expect(user.reload.name).to eql(new_name) } specify { expect(user.reload.email).to eql(new_email) } end describe "forbidden attributes" let(:params) { user: { admin: true, password: user.password, password_confirmation: user.password } } end before { patch user_path(user), params } specify { expect(user.reload).not_to be_admin } end end end
utilities.rb
include applicationhelper def sign_in(user) visit signin_path fill_in "email", with: user.email fill_in "password", with: user.password click_button "sign in" #sign in when not using capybara well. cookies[:remember_token] = user.remember_token end
could please provide tips beginners? support.
if you're going scope routes include locale
, you'll need pass in locale
paths when test them (as can see in new output rake routes
, :locale
expected), put, say, visit users_path
, put
visit users_path(locale)
. locale
list of i18n.available_locales
. you'll need change strings you're using find fields i18n equivalent eg
fill_in "email"
becomes fill_in i18n.t('sessions.new.email')
i i18n-ized sample app, here's user_pages_spec.rb give idea on kind of changes you'll need make.
Comments
Post a Comment