Michael Hartl的Rails教程第7.2.2章注册 - 失败测试

时间:2014-07-22 17:49:36

标签: ruby-on-rails ruby ruby-on-rails-3 rspec

我检查了所有页面的代码,它们与Hartl的教程完全相同。不过,当我运行 bundle exec rspec spec / 时,我得到了这些失败的测试错误,而且我很困惑:

    Pending:
      StaticPagesHelper add some examples to (or delete) /Users/thaobach/Desktop/sample_app/spec/helpers/static_pages_helper_spec.rb
        # No reason given
        # ./spec/helpers/static_pages_helper_spec.rb:14

    Failures:

      1) Static pages Contact page it should behave like all static pages 
     Failure/Error: it { should have_title('h1', text:heading) }
     ArgumentError:
       wrong number of arguments (2 for 1)
     Shared Example Group: "all static pages" called from ./spec/requests/static_pages_spec.rb:62
     # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

  2) Static pages About page it should behave like all static pages 
     Failure/Error: it { should have_title('h1', text:heading) }
     ArgumentError:
       wrong number of arguments (2 for 1)
     Shared Example Group: "all static pages" called from ./spec/requests/static_pages_spec.rb:53
     # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

  3) Static pages Home page it should behave like all static pages 
     Failure/Error: it { should have_title('h1', text:heading) }
     ArgumentError:
       wrong number of arguments (2 for 1)
     Shared Example Group: "all static pages" called from ./spec/requests/static_pages_spec.rb:34
     # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

  4) Static pages Help page it should behave like all static pages 
     Failure/Error: it { should have_title('h1', text:heading) }
     ArgumentError:
       wrong number of arguments (2 for 1)
     Shared Example Group: "all static pages" called from ./spec/requests/static_pages_spec.rb:44
     # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

  5) User pages profile page 
     Failure/Error: it { should have_title('title', text: user.name) }
     ArgumentError:
       wrong number of arguments (2 for 1)
     # ./spec/requests/user_pages_spec.rb:12:in `block (3 levels) in <top (required)>'

  6) User pages profile page 
     Failure/Error: it { should have_title('h1',    text: user.name) }
     ArgumentError:
       wrong number of arguments (2 for 1)
     # ./spec/requests/user_pages_spec.rb:11:in `block (3 levels) in <top (required)>'

  7) User pages signup page 
     Failure/Error: it { should have_title('title', text: 'Sign Up') }
     ArgumentError:
       wrong number of arguments (2 for 1)
     # ./spec/requests/user_pages_spec.rb:19:in `block (3 levels) in <top (required)>'

  8) User pages signup page 
     Failure/Error: it { should have_title('h1', text: 'Sign Up') }
     ArgumentError:
       wrong number of arguments (2 for 1)
     # ./spec/requests/user_pages_spec.rb:18:in `block (3 levels) in <top (required)>'

规格/控制器/ static_pages_controller_spec.rb

require 'spec_helper'

describe StaticPagesController do

  describe "GET '...'" do
    it "returns http success" do
      get '...'
      response.should be_success
    end
  end
end

config / routes.rb

SampleApp::Application.routes.draw do
  resources :users
  root to: 'static_pages#home'
  match '/signup',  to: 'users#new',            via: 'get'
  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

spec / requests / user_pages_spec.rb

    require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit user_path(user) }

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

  describe "signup page" do
    before { visit signup_path }

    it { should have_title('h1', text: 'Sign Up') }
    it { should have_title('title', text: 'Sign Up') }
  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

应用/功能/请求/ static_pages_spec.rb

require 'spec_helper'

describe "Static pages" do

  subject { page }

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

  it "should have the right links on the layout" do
    visit root_path
    click_link "About"
    expect(page).to have_title(full_title('About Us'))
    click_link "Help"
    expect(page).to have_title(full_title('Help'))
    click_link "Contact"
    expect(page).to have_title(full_title('Contact'))
    click_link "Home"
    click_link "Sign up now!"
    expect(page).to have_title(full_title('Sign up'))
    click_link "sample app"
    expect(page).to have_title(full_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_title('| Home') }
  end

  describe "Help page" do
    before { visit help_path }

    let (:heading) { 'Help'}
    let(:page_title) { 'Help' }

    it_should_behave_like "all static pages"
  end

  describe "About page" do
    before { visit about_path }

    let (:heading) { 'About'}
    let(:page_title) { 'About' }

    it_should_behave_like "all static pages"
  end

  describe "Contact page" do
    before { visit contact_path }

    let (:heading) { 'Contact'}
    let(:page_title) { 'Contact' }

    it_should_behave_like "all static pages"
  end
end

应用/控制器/ users_controller.rb

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
        #Handle successful save
    else
        render 'new'
    end
  end
end

的Gemfile

source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0

gem 'rails', '4.0.8'
gem 'bootstrap-sass', '2.3.2.0'
gem 'bcrypt-ruby', '~> 3.1.0'


gem 'sprockets', '2.11.0'
gem 'protected_attributes'
gem 'devise', '3.0.0.rc'

group :development, :test do
  gem 'spork-rails', '4.0.0'
  gem 'guard-spork', '1.5.0'
  gem 'childprocess', '0.3.6'
end

group :development, :test do
  gem 'sqlite3', '1.3.8'
  gem 'rspec-rails', '2.13.1'
  gem 'guard-rspec', '2.5.0'
end

group :test do
  gem 'selenium-webdriver', '2.35.1'
  gem 'capybara', '2.1.0'
  gem 'factory_girl_rails', '4.1.0' 

  # Uncomment this line on OS X.
  # gem 'growl', '1.0.3'

  # Uncomment these lines on Linux.
  # gem 'libnotify', '0.8.0'

  # Uncomment these lines on Windows.
  # gem 'rb-notifu', '0.0.4'
  # gem 'wdm', '0.1.0'

end

gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'

group :doc do
  gem 'sdoc', '0.3.20', require: false
end

group :production do
  gem 'pg', '0.15.1'
  gem 'rails_12factor', '0.0.2'
end

1 个答案:

答案 0 :(得分:0)

所有错误几乎都是自我解释的,它会为你打破它。

  1. 我不知道你想用描述get '...'做什么,请再次参考教程。

  2. 正如错误所说&#34;动作&#39;创建&#39;找不到UsersController&#34;,在路径文件中添加行resources :users, only: [:create]

  3. 与第二个相同的错误

  4. 4,5,6。在查看了您的UsersController文件后,我可以详细说明。