0

让三名工程师努力解决这个问题,但没有运气。完成Ruby on rails 教程 - Michael Hartl,第 8 章,似乎无法取得进展。如果您是绝地工程师,由于新手经验/知识,请完整填写答案。提前致谢!

错误

    Failures:

  1) when email format is invalid should be invalid
     Failure/Error: @user.email = invalid_address
     NoMethodError:
       undefined method `email=' for nil:NilClass
     # ./spec/models/user_spec.rb:33:in `block (3 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:32:in `each'
     # ./spec/models/user_spec.rb:32:in `block (2 levels) in <top (required)>'

  2) when email format is valid should be valid
     Failure/Error: @user.email = valid_address
     NoMethodError:
       undefined method `email=' for nil:NilClass
     # ./spec/models/user_spec.rb:43:in `block (3 levels) in <top (required)>'
     # ./spec/models/user_spec.rb:42:in `each'
     # ./spec/models/user_spec.rb:42:in `block (2 levels) in <top (required)>'

  3) when email address is already taken 
     Failure/Error: user_with_same_email = @user.dup
     TypeError:
       can't dup NilClass
     # ./spec/models/user_spec.rb:51:in `dup'
     # ./spec/models/user_spec.rb:51:in `block (2 levels) in <top (required)>'

  4) when password is not present 
     Failure/Error: before { @user.password = @user.password_confirmation = " " }
     NoMethodError:
       undefined method `password_confirmation=' for nil:NilClass
     # ./spec/models/user_spec.rb:60:in `block (2 levels) in <top (required)>'

  5) when password doesn't match confirmation 
     Failure/Error: before { @user.password_confirmation = "mismatch" }
     NoMethodError:
       undefined method `password_confirmation=' for nil:NilClass
     # ./spec/models/user_spec.rb:65:in `block (2 levels) in <top (required)>'

  6) when password confirmation is nil 
     Failure/Error: before { @user.password_confirmation = nil }
     NoMethodError:
       undefined method `password_confirmation=' for nil:NilClass
     # ./spec/models/user_spec.rb:70:in `block (2 levels) in <top (required)>'

  7) with a password that's too short 
     Failure/Error: before { @user.password = @user.password_confirmation = "a" * 5 }
     NoMethodError:
       undefined method `password_confirmation=' for nil:NilClass
     # ./spec/models/user_spec.rb:76:in `block (2 levels) in <top (required)>'

  8) return value of authenticate method with valid password 
     Failure/Error: before { @user.save }
     NoMethodError:
       undefined method `save' for nil:NilClass
     # ./spec/models/user_spec.rb:81:in `block (2 levels) in <top (required)>'

  9) return value of authenticate method with invalid password 
     Failure/Error: before { @user.save }
     NoMethodError:
       undefined method `save' for nil:NilClass
     # ./spec/models/user_spec.rb:81:in `block (2 levels) in <top (required)>'

  10) return value of authenticate method with invalid password 
     Failure/Error: before { @user.save }
     NoMethodError:
       undefined method `save' for nil:NilClass
     # ./spec/models/user_spec.rb:81:in `block (2 levels) in <top (required)>'

  11) Authentication signin page 
     Failure/Error: before { visit signin_path }
     ActionView::Template::Error:
       undefined local variable or method `root_path' for #<#<Class:0x007f841ca913c8>:0x007f8419313cc0>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___3328719763930508541_70102689576460'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___3500649280077332684_70102687772720'
     # ./spec/requests/authentication_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

  12) Authentication signin page 
     Failure/Error: before { visit signin_path }
     ActionView::Template::Error:
       undefined local variable or method `root_path' for #<#<Class:0x007f841ca913c8>:0x007f841bba0030>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___3328719763930508541_70102689576460'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___3500649280077332684_70102687772720'
     # ./spec/requests/authentication_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

  13) Static pages Home page 
     Failure/Error: before { visit root_path }
     NameError:
       undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_11::Nested_1:0x007f8419475898>
     # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

  14) Static pages Home page 
     Failure/Error: before { visit root_path }
     NameError:
       undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_11::Nested_1:0x007f84195c97f8>
     # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

  15) Static pages Home page 
     Failure/Error: before { visit root_path }
     NameError:
       undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_11::Nested_1:0x007f84196d96e8>
     # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top (required)>'

  16) Static pages Help page 
     Failure/Error: before { visit help_path }
     ActionView::Template::Error:
       undefined local variable or method `root_path' for #<#<Class:0x007f841b879528>:0x007f84196268b8>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___3328719763930508541_70102689576460'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___3500649280077332684_70102687772720'
     # ./spec/requests/static_pages_spec.rb:16:in `block (3 levels) in <top (required)>'

  17) Static pages Help page 
     Failure/Error: before { visit help_path }
     ActionView::Template::Error:
       undefined local variable or method `root_path' for #<#<Class:0x007f841b879528>:0x007f841bd4b4e8>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___3328719763930508541_70102689576460'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___3500649280077332684_70102687772720'
     # ./spec/requests/static_pages_spec.rb:16:in `block (3 levels) in <top (required)>'

  18) Static pages About page 
     Failure/Error: before { visit about_path }
     ActionView::Template::Error:
       undefined local variable or method `root_path' for #<#<Class:0x007f841b879528>:0x007f841bbab728>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___3328719763930508541_70102689576460'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___3500649280077332684_70102687772720'
     # ./spec/requests/static_pages_spec.rb:23:in `block (3 levels) in <top (required)>'

  19) Static pages About page 
     Failure/Error: before { visit about_path }
     ActionView::Template::Error:
       undefined local variable or method `root_path' for #<#<Class:0x007f841b879528>:0x007f841a1f8c18>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___3328719763930508541_70102689576460'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___3500649280077332684_70102687772720'
     # ./spec/requests/static_pages_spec.rb:23:in `block (3 levels) in <top (required)>'

  20) Static pages Contact page 
     Failure/Error: before { visit contact_path }
     ActionView::Template::Error:
       undefined local variable or method `root_path' for #<#<Class:0x007f841b879528>:0x007f841b8baf00>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___3328719763930508541_70102689576460'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___3500649280077332684_70102687772720'
     # ./spec/requests/static_pages_spec.rb:30:in `block (3 levels) in <top (required)>'

  21) Static pages Contact page 
     Failure/Error: before { visit contact_path }
     ActionView::Template::Error:
       undefined local variable or method `root_path' for #<#<Class:0x007f841b879528>:0x007f841be450b0>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___3328719763930508541_70102689576460'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___3500649280077332684_70102687772720'
     # ./spec/requests/static_pages_spec.rb:30:in `block (3 levels) in <top (required)>'

  22) User pages profile page 
     Failure/Error: before { visit user_path(user) }
     ActionView::Template::Error:
       undefined local variable or method `root_path' for #<#<Class:0x007f841bed35e0>:0x007f841bedc2d0>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___3328719763930508541_70102689576460'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___3500649280077332684_70102687772720'
     # ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'

  23) User pages profile page 
     Failure/Error: before { visit user_path(user) }
     ActionView::Template::Error:
       undefined local variable or method `root_path' for #<#<Class:0x007f841bed35e0>:0x007f841b8890e0>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___3328719763930508541_70102689576460'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___3500649280077332684_70102687772720'
     # ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'

  24) User pages signup with invalid information should not create a user
     Failure/Error: before { visit signup_path }
     ActionView::Template::Error:
       undefined local variable or method `root_path' for #<#<Class:0x007f841bed35e0>:0x007f841bfd8940>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___3328719763930508541_70102689576460'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___3500649280077332684_70102687772720'
     # ./spec/requests/user_pages_spec.rb:17:in `block (3 levels) in <top (required)>'

  25) User pages signup with valid information should create a user
     Failure/Error: before { visit signup_path }
     ActionView::Template::Error:
       undefined local variable or method `root_path' for #<#<Class:0x007f841bed35e0>:0x007f841bd018c0>
     # ./app/views/layouts/_header.html.erb:4:in `_app_views_layouts__header_html_erb___3328719763930508541_70102689576460'
     # ./app/views/layouts/application.html.erb:11:in `_app_views_layouts_application_html_erb___3500649280077332684_70102687772720'
     # ./spec/requests/user_pages_spec.rb:17:in `block (3 levels) in <top (required)>'

Finished in 0.60263 seconds
33 examples, 25 failures

user_spec.rb

    require 'spec_helper'

describe User do

  before do
    @user = User.new(name: "Example User", email: "user@example.com", 
                     password: "foobar", password_confirmation: "foobar")
  end

  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }
  it { should respond_to(:password_digest) }
  it { should respond_to(:password) }
  it { should respond_to(:password_confirmation) }

  it { should respond_to(:authenticate) }

  it { should be_valid }

  describe "when name is too long" do
      before { @user.name = "a" * 51 }
      it { should_not be_valid }
    end
  end

  describe "when email format is invalid" do
      it "should be invalid" do
        addresses = %w[user@foo,com user_at_foo.org example.user@foo.
                       foo@bar_baz.com foo@bar+baz.com]
        addresses.each do |invalid_address|
          @user.email = invalid_address
          @user.should_not be_valid
        end      
      end
    end

    describe "when email format is valid" do
      it "should be valid" do
        addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
        addresses.each do |valid_address|
          @user.email = valid_address
          @user.should be_valid
        end      
      end
    end

  describe "when email address is already taken" do
      before do
        user_with_same_email = @user.dup
        user_with_same_email.email = @user.email.upcase
        user_with_same_email.save
      end

      it { should_not be_valid }
    end

  describe "when password is not present" do
      before { @user.password = @user.password_confirmation = " " }
      it { should_not be_valid }
    end

    describe "when password doesn't match confirmation" do
      before { @user.password_confirmation = "mismatch" }
      it { should_not be_valid }
    end

    describe "when password confirmation is nil" do
      before { @user.password_confirmation = nil }
      it { should_not be_valid }
    end


   describe "with a password that's too short" do
      before { @user.password = @user.password_confirmation = "a" * 5 }
      it { should be_invalid }
    end

    describe "return value of authenticate method" do
      before { @user.save }
      let(:found_user) { User.find_by_email(@user.email) }

      describe "with valid password" do
        it { should == found_user.authenticate(@user.password) }
      end

      describe "with invalid password" do
        let(:user_for_invalid_password) { found_user.authenticate("invalid") }

        it { should_not == user_for_invalid_password }
        specify { user_for_invalid_password.should be_false }
      end
    end

authentication_pages_spec

require '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
end

static_pages_spec

require 'spec_helper'

describe "Static pages" do

  subject { page }

  describe "Home page" do
    before { visit root_path }

    it { should have_selector('h1',    text: 'Sample App') }
    it { should have_selector('title', text: full_title('')) }
    it { should_not have_selector 'title', text: '| Home' }
  end

  describe "Help page" do
    before { visit help_path }

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

  describe "About page" do
    before { visit about_path }

    it { should have_selector('h1',    text: 'About') }
    it { should have_selector('title', text: full_title('About Us')) }
  end

  describe "Contact page" do
    before { visit contact_path }

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

user_pages_spec

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_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

宝石文件

source 'https://rubygems.org'

gem 'rails', '3.2.6'
gem 'bootstrap-sass', '2.0.0'
gem 'bcrypt-ruby', '3.0.1'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

group :development, :test do
  gem 'sqlite3', '1.3.5'
  gem 'rspec-rails', '2.10.0'
end

gem 'annotate', '~> 2.4.1.beta', group: :development


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '3.2.4'
  gem 'coffee-rails', '3.2.2'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '1.2.3'
end

gem 'jquery-rails', '2.0.0'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

group :test do
gem 'capybara', '1.1.2'
gem 'factory_girl_rails', '1.4.0'
end

group :production do
gem 'pg', '0.12.2'
end

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'

在此先感谢各位:)

4

1 回答 1

2

看来您忘记准备测试数据库了。在命令行上运行:

rake db:test:prepare

我绝对建议您阅读测试 Rails 应用程序指南(3.1 准备测试应用程序),以了解在运行测试之前需要发生什么。

于 2012-07-29T03:08:37.547 回答