testing - Hartl RoR Chap. 9.3.1 spec test failure -
i seem running failures left , right... checked code, , defined , spell check in check... here failures...:
failures: 1) user pages index should list each user failure/error: visit users_path actionview::template::error: wrong number of arguments (2 1) # ./app/helpers/users_helper.rb:4:in `gravatar_for' # ./app/views/users/index.html.erb:7:in `block in _app_views_users_index_html_erb___3453469340594189774_70210782785680' # ./app/views/users/index.html.erb:5:in `each' # ./app/views/users/index.html.erb:5:in `_app_views_users_index_html_erb___3453469340594189774_70210782785680' # ./spec/requests/user_pages_spec.rb:12:in `block (3 levels) in <top (required)>' 2) user pages index failure/error: visit users_path actionview::template::error: wrong number of arguments (2 1) # ./app/helpers/users_helper.rb:4:in `gravatar_for' # ./app/views/users/index.html.erb:7:in `block in _app_views_users_index_html_erb___3453469340594189774_70210782785680' # ./app/views/users/index.html.erb:5:in `each' # ./app/views/users/index.html.erb:5:in `_app_views_users_index_html_erb___3453469340594189774_70210782785680' # ./spec/requests/user_pages_spec.rb:12:in `block (3 levels) in <top (required)>' 3) user pages index failure/error: visit users_path actionview::template::error: wrong number of arguments (2 1) # ./app/helpers/users_helper.rb:4:in `gravatar_for' # ./app/views/users/index.html.erb:7:in `block in _app_views_users_index_html_erb___3453469340594189774_70210782785680' # ./app/views/users/index.html.erb:5:in `each' # ./app/views/users/index.html.erb:5:in `_app_views_users_index_html_erb___3453469340594189774_70210782785680' # ./spec/requests/user_pages_spec.rb:12:in `block (3 levels) in <top (required)>' finished in 1.87 seconds 70 examples, 3 failures failed examples: rspec ./spec/requests/user_pages_spec.rb:18 # user pages index should list each user rspec ./spec/requests/user_pages_spec.rb:16 # user pages index rspec ./spec/requests/user_pages_spec.rb:15 # user pages index here code: spec/requests/user_pages_spec.rb
require 'spec_helper' describe "user pages" subject { page } describe "index" before sign_in factorygirl.create(:user) factorygirl.create(:user, name: "bob", email: "bob@example.com") factorygirl.create(:user, name: "ben", email: "ben@example.com") visit users_path end { should have_selector('title', text: 'all users') } { should have_selector('h1', text: 'all users') } "should list each user" user.all.each |user| page.should have_selector('li', text: user.name) end end end describe "profile page" let(:user) { factorygirl.create(:user) } before { visit user_path(user) } { should have_selector('h1', text: user.name) } { should have_selector('title', text: user.name) } end describe "signup page" before { visit signup_path } { should have_selector('h1', text: 'sign up') } { should have_selector('title', text: 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 end describe "with valid information" before fill_in "name", with: "example user" fill_in "email", with: "user@example.com" fill_in "password", with: "foobar" fill_in "confirmation", with: "foobar" end describe "after saving user" before { click_button submit } let(:user) { user.find_by_email('user@example.com') } { should have_selector('title', text: user.name) } { should have_selector('div.alert.alert-success', text: 'welcome') } { should have_link('sign out') } end "should create user" expect { click_button submit }.to change(user, :count).by(1) end end end describe "edit" let(:user) { factorygirl.create(:user) } before sign_in user visit edit_user_path(user) end describe "page" { should have_selector('h1', text: "update profile") } { should have_selector('title', text: "edit user") } { should have_link('change', href: 'http://gravatar.com/emails') } 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_selector('title', text: new_name) } { should have_selector('div.alert.alert-success') } { should have_link('sign out', href: signout_path) } specify { user.reload.name.should == new_name } specify { user.reload.email.should == new_email } end end end users_controller.rb
class userscontroller < applicationcontroller before_filter :signed_in_user, only: [ :index, :edit, :update] before_filter :correct_user, only: [:edit, :update] def show @user = user.find(params[:id]) end def new @user = user.new end def create @user = user.new(params[:user]) if @user.save sign_in @user flash[:success] = "welcome sample app!" redirect_to @user else render 'new' end end def index @users = user.all end def edit end def update if @user.update_attributes(params[:user]) flash[:success] = "profile updated" sign_in @user redirect_to @user else render 'edit' end end private def signed_in_user unless signed_in? store_location redirect_to signin_url, notice: "please sign in." end end def correct_user @user = user.find(params[:id]) redirect_to(root_path) unless current_user?(@user) end end index.html.erb:
<% provide(:title, 'all users') %> <h1>all users</h1> <ul class="users"> <% @users.each |user| %> <li> <%= gravatar_for user, size: 52 %> <%= link_to user.name, user %> </li> <% end %> </ul> authentication_pages_spec.rb
require 'spec_helper' describe "authentication" subject { page } describe "signin page" before { visit signin_path } { should have_selector('h1', text: 'sign in') } { should have_selector('title', text: 'sign in') } end describe "signin" before { visit signin_path } describe "with invalid information" before { click_button "sign in" } { should have_selector('title', text: 'sign in') } { should have_selector('div.alert.alert-error', text: 'invalid') } describe "after visiting page" before { click_link "home" } { should_not have_selector('div.alert.alert-error') } end end describe "with valid information" let(:user) { factorygirl.create(:user) } before { sign_in user } { should have_selector('title', text: user.name) } { should have_link('users', href: users_path) } { should have_link('profile', href: user_path(user)) } { should have_link('settings', href: edit_user_path(user)) } { should have_link('sign out', href: signout_path) } { should_not have_link('sign in', href: signin_path) } describe "followed signout" before { click_link "sign out" } { should have_link('sign in') } end end end describe "authorization" describe "for non-signed-in users" let(:user) { factorygirl.create(:user) } describe "in users controller" describe "visiting edit page" before { visit edit_user_path(user) } { should have_selector('title', text: 'sign in') } end describe "submitting update action" before { set user_path(user) } specify { response.should redirect_to(signin_path) } end describe "visiting user index" before { visit users_path } { should have_selector('title', text: 'sign in') } end end describe "when attempting visit protected page" before visit edit_user_path(user) fill_in "email", with: user.email fill_in "password", with: user.password click_button "sign in" end describe "after signing in" "should render desired protected page" page.should have_selector('title', text: 'edit user') end end end end describe "as wrong user" let(:user) { factorygirl.create(:user) } let(:wrong_user) { factorygirl.create(:user, email: "wrong@example.com") } before { sign_in user } describe "visiting users#edit page" before { visit edit_user_path(wrong_user) } { should_not have_selector('title', text: full_title('edit user')) } end describe "submitting set request users#update action" before { set user_path(wrong_user) } specify { response.should redirect_to(root_path) } end end end end sussions_helper.rb
module sessionshelper def sign_in(user) cookies.permanent[:remember_token] = user.remember_token self.current_user = user end def signed_in? !current_user.nil? end def current_user=(user) @current_user = user end def current_user @current_user ||= user.find_by_remember_token(cookies[:remember_token]) end def current_user?(user) user == current_user end def sign_out self.current_user = nil cookies.delete(:remember_token) end def redirect_back_or(default) redirect_to(session[:return_to] || default) session.delete(:return_to) end def store_location session[:return_to] = request.url end end sessions_controller.rb
class sessionscontroller < applicationcontroller def new end def create user = user.find_by_email(params[:session][:email].downcase) if user && user.authenticate(params[:session][:password]) sign_in user redirect_back_or user else flash.now[:error] = 'invalid email/password combination' render 'new' end end def destroy sign_out redirect_to root_url end end what wrong here?? suggestions?
you need code listing 7.29. it's editing gavatars.
listing 7.29. defining optional :size parameter gravatar_for helper.
app/helpers/users_helper.rb
module usershelper # returns gravatar (http://gravatar.com/) given user. def gravatar_for(user, options = { size: 50 }) gravatar_id = digest::md5::hexdigest(user.email.downcase) size = options[:size] gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}" image_tag(gravatar_url, alt: user.name, class: "gravatar") end end testing ruby-on-rails-3.2 railstutorial.org
No comments:
Post a Comment