测试-spec Hartl教程第8.2章

时间:2012-06-08 18:52:43

标签: ruby-on-rails testing rspec railstutorial.org

我一直在努力让我的考试通过。我认为问题出在我的signin_path上,但我无法确定问题的确切位置,任何帮助都将不胜感激!

这是失败消息(很长时间。抱歉):

bundle exec rspec spec/......................FFFFFFFFFFFFFFFFFF

Failures:

  1) Authentication signin page 
     Failure/Error: before { visit signin_path }
     ActionView::Template::Error:
       wrong number of arguments (0 for 1)
     # ./app/helpers/sessions_helper.rb:17:in `current_user'
     # ./app/helpers/sessions_helper.rb:10:in `signed_in?'
     # ./app/views/layouts/_header.html.erb:9:in          `    _app_views_layouts__header_html_erb__3520387809765840673_2170500560'
 # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___1084806681328616491_2159679840'
 # ./spec/requests/authentication_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

2) Authentication signin page 
     Failure/Error: before { visit signin_path }
     ActionView::Template::Error:
       wrong number of arguments (0 for 1)
     # ./app/helpers/sessions_helper.rb:17:in `current_user'
     # ./app/helpers/sessions_helper.rb:10:in `signed_in?'
     # ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__3520387809765840673_2170500560'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___1084806681328616491_2159679840'
     # ./spec/requests/authentication_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

  3) Authentication signin with invalid information 
     Failure/Error: before { visit signin_path }
     ActionView::Template::Error:
       wrong number of arguments (0 for 1)
     # ./app/helpers/sessions_helper.rb:17:in `current_user'
     # ./app/helpers/sessions_helper.rb:10:in `signed_in?'
     # ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__3520387809765840673_2170500560'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___1084806681328616491_2159679840'
     # ./spec/requests/authentication_pages_spec.rb:15:in `block (3 levels) in <top (required)>'

  4) Authentication signin with invalid information 
     Failure/Error: before { visit signin_path }
     ActionView::Template::Error:
       wrong number of arguments (0 for 1)
     # ./app/helpers/sessions_helper.rb:17:in `current_user'
     # ./app/helpers/sessions_helper.rb:10:in `signed_in?'
     # ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__3520387809765840673_2170500560'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___1084806681328616491_2159679840'
     # ./spec/requests/authentication_pages_spec.rb:15:in `block (3 levels) in <top (required)>'

  5) Authentication signin with invalid information after visiting another page 
     Failure/Error: before { visit signin_path }
     ActionView::Template::Error:
       wrong number of arguments (0 for 1)
     # ./app/helpers/sessions_helper.rb:17:in `current_user'
     # ./app/helpers/sessions_helper.rb:10:in `signed_in?'
     # ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__3520387809765840673_2170500560'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___1084806681328616491_2159679840'
     # ./spec/requests/authentication_pages_spec.rb:15:in `block (3 levels) in <top (required)>'

  6) Authentication signin with valid information 
     Failure/Error: before { visit signin_path }
     ActionView::Template::Error:
       wrong number of arguments (0 for 1)
     # ./app/helpers/sessions_helper.rb:17:in `current_user'
     # ./app/helpers/sessions_helper.rb:10:in `signed_in?'
     # ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__3520387809765840673_2170500560'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___1084806681328616491_2159679840'
     # ./spec/requests/authentication_pages_spec.rb:15:in `block (3 levels) in <top (required)>'

  7) Authentication signin with valid information 
     Failure/Error: before { visit signin_path }
     ActionView::Template::Error:
       wrong number of arguments (0 for 1)
     # ./app/helpers/sessions_helper.rb:17:in `current_user'
     # ./app/helpers/sessions_helper.rb:10:in `signed_in?'
     # ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__3520387809765840673_2170500560'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___1084806681328616491_2159679840'
    # ./spec/requests/authentication_pages_spec.rb:15:in `block (3 levels) in <top (required)>'

  8) Authentication signin with valid information 
     Failure/Error: before { visit signin_path }
     ActionView::Template::Error:
       wrong number of arguments (0 for 1)
     # ./app/helpers/sessions_helper.rb:17:in `current_user'
     # ./app/helpers/sessions_helper.rb:10:in `signed_in?'
     # ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__3520387809765840673_2170500560'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___1084806681328616491_2159679840'
     # ./spec/requests/authentication_pages_spec.rb:15:in `block (3 levels) in <top (required)>'

  9) Authentication signin with valid information 
     Failure/Error: before { visit signin_path }
     ActionView::Template::Error:
       wrong number of arguments (0 for 1)
     # ./app/helpers/sessions_helper.rb:17:in `current_user'
     # ./app/helpers/sessions_helper.rb:10:in `signed_in?'
     # ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__3520387809765840673_2170500560'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___1084806681328616491_2159679840'
     # ./spec/requests/authentication_pages_spec.rb:15:in `block (3 levels) in <top (required)>'

  10) Static pages Home page 
     Failure/Error: before { visit root_path }
     ActionView::Template::Error:
       wrong number of arguments (0 for 1)
     # ./app/helpers/sessions_helper.rb:17:in `current_user'
     # ./app/helpers/sessions_helper.rb:10:in `signed_in?'
     # ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__3520387809765840673_2170500560'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___1084806681328616491_2159679840'
     # ./spec/requests/static_pages_spec.rb:12:in `block (3 levels) in <top (required)>'

  11) Static pages Home page it should behave like all static pages 
     Failure/Error: before { visit root_path }
     ActionView::Template::Error:
       wrong number of arguments (0 for 1)
     Shared Example Group: "all static pages" called from ./spec/requests/static_pages_spec.rb:16
     # ./app/helpers/sessions_helper.rb:17:in `current_user'
     # ./app/helpers/sessions_helper.rb:10:in `signed_in?'
     # ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__3520387809765840673_2170500560'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___1084806681328616491_2159679840'
     # ./spec/requests/static_pages_spec.rb:12:in `block (3 levels) in <top (required)>'

  12) Static pages Home page it should behave like all static pages 
     Failure/Error: before { visit root_path }
     ActionView::Template::Error:
       wrong number of arguments (0 for 1)
     Shared Example Group: "all static pages" called from ./spec/requests/static_pages_spec.rb:16
     # ./app/helpers/sessions_helper.rb:17:in `current_user'
     # ./app/helpers/sessions_helper.rb:10:in `signed_in?'
     # ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__3520387809765840673_2170500560'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___1084806681328616491_2159679840'
     # ./spec/requests/static_pages_spec.rb:12:in `block (3 levels) in <top (required)>'

  13) User pages signup page 
     Failure/Error: before { visit signup_path }
     ActionView::Template::Error:
       wrong number of arguments (0 for 1)
     # ./app/helpers/sessions_helper.rb:17:in `current_user'
     # ./app/helpers/sessions_helper.rb:10:in `signed_in?'
     # ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__3520387809765840673_2170500560'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___1084806681328616491_2159679840'
     # ./spec/requests/user_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

  14) User pages signup page 
     Failure/Error: before { visit signup_path }
     ActionView::Template::Error:
       wrong number of arguments (0 for 1)
     # ./app/helpers/sessions_helper.rb:17:in `current_user'
     # ./app/helpers/sessions_helper.rb:10:in `signed_in?'
     # ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__3520387809765840673_2170500560'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___1084806681328616491_2159679840'
     # ./spec/requests/user_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

  15) User pages profile page 
     Failure/Error: before { visit user_path(user) }
     ActionView::Template::Error:
       wrong number of arguments (0 for 1)
     # ./app/helpers/sessions_helper.rb:17:in `current_user'
     # ./app/helpers/sessions_helper.rb:10:in `signed_in?'
     # ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__3520387809765840673_2170500560'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___1084806681328616491_2159679840'
     # ./spec/requests/user_pages_spec.rb:17:in `block (3 levels) in <top (required)>'

  16) User pages profile page 
     Failure/Error: before { visit user_path(user) }
     ActionView::Template::Error:
       wrong number of arguments (0 for 1)
     # ./app/helpers/sessions_helper.rb:17:in `current_user'
     # ./app/helpers/sessions_helper.rb:10:in `signed_in?'
     # ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__3520387809765840673_2170500560'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___1084806681328616491_2159679840'
     # ./spec/requests/user_pages_spec.rb:17:in `block (3 levels) in <top (required)>'

  17) User pages signup with invalid information should not create a user
     Failure/Error: before { visit signup_path }
     ActionView::Template::Error:
       wrong number of arguments (0 for 1)
     # ./app/helpers/sessions_helper.rb:17:in `current_user'
     # ./app/helpers/sessions_helper.rb:10:in `signed_in?'
     # ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__3520387809765840673_2170500560'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___1084806681328616491_2159679840'
     # ./spec/requests/user_pages_spec.rb:26:in `block (3 levels) in <top (required)>'

  18) User pages signup with valid information should create a user
     Failure/Error: before { visit signup_path }
     ActionView::Template::Error:
       wrong number of arguments (0 for 1)
     # ./app/helpers/sessions_helper.rb:17:in `current_user'
     # ./app/helpers/sessions_helper.rb:10:in `signed_in?'
     # ./app/views/layouts/_header.html.erb:9:in `_app_views_layouts__header_html_erb__3520387809765840673_2170500560'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___1084806681328616491_2159679840'
     # ./spec/requests/user_pages_spec.rb:26:in `block (3 levels) in <top (required)>'

Finished in 0.54083 seconds
40 examples, 18 failures

Failed examples:

rspec ./spec/requests/authentication_pages_spec.rb:10 # Authentication signin page 
rspec ./spec/requests/authentication_pages_spec.rb:11 # Authentication signin page 
rspec ./spec/requests/authentication_pages_spec.rb:20 # Authentication signin with invalid     information 
rspec ./spec/requests/authentication_pages_spec.rb:21 # Authentication signin with invalid information 
rspec ./spec/requests/authentication_pages_spec.rb:25 # Authentication signin with invalid information after visiting another page 
rspec ./spec/requests/authentication_pages_spec.rb:37 # Authentication signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:38 # Authentication signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:39 # Authentication signin with valid information 
rspec ./spec/requests/authentication_pages_spec.rb:40 # Authentication signin with valid information 
rspec ./spec/requests/static_pages_spec.rb:17 # Static pages Home page 
rspec ./spec/requests/static_pages_spec.rb:7 # Static pages Home page it should behave like all static pages 
rspec ./spec/requests/static_pages_spec.rb:8 # Static pages Home page it should behave like all static pages 
rspec ./spec/requests/user_pages_spec.rb:10 # User pages signup page 
rspec ./spec/requests/user_pages_spec.rb:11 # User pages signup page 
rspec ./spec/requests/user_pages_spec.rb:19 # User pages profile page 
rspec ./spec/requests/user_pages_spec.rb:20 # User pages profile page 
rspec ./spec/requests/user_pages_spec.rb:31 # User pages signup with invalid information should not create a user
rspec ./spec/requests/user_pages_spec.rb:47 # User pages signup with valid information should create a user

这是authentication_pages_spec.rb     需要'spec_helper'

   describe "Authentication" do

     subject { page }

      describe "signin page" do
        before { visit signin_path }

       it { should have_selector('h1', text: 'Sign in') }
       it { should have_selector('title', text: 'Sign in') }
     end

     describe "signin" do
       before { visit signin_path }

       describe "with invalid information" do
         before { click_button "Sign in"}

         it { should have_selector('title', text: 'Sign in') }
         it { should have_selector('div.alert.alert-error', text: 'Invalid') }

         describe "after visiting another page" do
           before { click_link "Home" }
           it { should_not have_selector('div.alert.alert-error') }
         end
       end

     describe "with valid information" do
       let(:user) { FactoryGirl.create(:user) }
       before do
         fill_in "Email", with: user.email
         fill_in "Password", with: user.password
         click_button "Sign in"
       end

        it { should have_selector('title', text: user.name) }
        it { should have_link('Profile', href: user_path(user)) }
       it { should have_link('Sign out', href: signout_path) }
       it { should_not have_link('Sign in', href: signin_path) }
       end
     end
   end

这是static_pages_spec.rb

   require 'spec_helper'

   describe "Static pages" do  
     subject { page }

     shared_examples_for "all static pages" do
       it { should have_selector('h1', text: heading) }
       it { should have_selector('title', text: full_title(page_title)) }
      end

     describe "Home page" do
       before { visit root_path }
      let(:heading)   { 'Sample App' }
      let(:page_title) { '' } 

      it_should_behave_like "all static pages"
      it { should_not have_selector 'title',   text: '| Home' }
       end

     describe "Help page" do
       before { visit help_path }
     let(:heading) { 'Help' }
     let(:page_title) { 'Help' }
      end

     describe "About Us page" do
       before { visit about_path }
     let(:heading) { 'About Us' }
     let(:page_title) { 'About Us' }
      end

     describe "Contact page" do
        before { visit contact_path }
     let(:heading) { 'Contact' }
     let(:page_title) { 'Contact' }
      end
   end

这是user_pages_spec.rb:

   require 'spec_helper'

   describe "User pages" do

     subject { page }

     describe "signup page" do
       before { visit signup_path }

       it { should have_selector('h1', text: 'Sign up') }
       it { should have_selector('title', text: full_title('Sign up')) }
     end

     describe "profile page" do
       let(:user) { FactoryGirl.create(:user) }
       #Code to make a user variable
       before { visit user_path(user) }

       it { should have_selector('h1', text: user.name) }
       it { should have_selector('title', text: user.name) }
     end

     describe "signup" do

       before { visit signup_path }

       let(:submit) { "Create my account" }

       describe "with invalid information" do
         it "should not create a user" do
           expect { click_button submit }.not_to change(User, :count)
         end   
       end

       describe "with valid information" do
         before do
           fill_in "Name",   with: "Example User"
           fill_in "Email",  with: "user@example.com"
           fill_in "Password", with: "foobar"
           fill_in "Confirmation",   with: "foobar"
         end

         it "should create a user" do
           expect { click_button submit }.to change(User, :count).by(1)
         end
       end
     end  
   end

这是session_helper.rb:

   module SessionsHelper
     require 'sessions_helper'

     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(user)
       @current_user ||= User.find_by_remember_token(cookies[:remember_token])
      end
end

我还没想出如何发布HTML,但如果看到applicait.html.erb和_header.html.erb会有所帮助,请告诉我,我会继续尝试。任何想法将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:2)

SessionsHelper模块中的最后一个方法current_user不需要使用参数用户。这就是为什么你看到错误“错误的参数数量(0表示1)。”