Michael Hartl的Rails教程 - 第5.3章

时间:2016-01-29 14:04:36

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

当我运行$ bundle exec rake test时,我遇到以下故障:

$ bundle exec rake test
/home/ubuntu/workspace/sample_app/db/schema.rb doesn't exist yet. Run `rake db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter /home/ubuntu/workspace/sample_app/config/application.rb to limit the frameworks that will be loaded.
Run options: --seed 7295

# Running:

FF.F

Finished in 0.664525s, 6.0193 runs/s, 12.0387 assertions/s.

  1) Failure:
StaticPagesControllerTest#test_should_get_contact [/home/ubuntu/workspace/sample_app/test/controllers/static_pages_controller_test.rb:25]:
<Contact | Ruby on Rails Tutorial Sample App> expected but was
<Contact  |  Ruby on Rails Tutorial Sample App>..
Expected 0 to be >= 1.


  2) Failure:
StaticPagesControllerTest#test_should_get_about [/home/ubuntu/workspace/sample_app/test/controllers/static_pages_controller_test.rb:19]:
<About | Ruby on Rails Tutorial Sample App> expected but was
<Ruby on Rails Tutorial Sample App>..
Expected 0 to be >= 1.


  3) Failure:
StaticPagesControllerTest#test_should_get_help [/home/ubuntu/workspace/sample_app/test/controllers/static_pages_controller_test.rb:13]:
<Help | Ruby on Rails Tutorial Sample App> expected but was
<Ruby on Rails Tutorial Sample App>..
Expected 0 to be >= 1.

4 runs, 8 assertions, 3 failures, 0 errors, 0 skips 

我的/static_pages_controller_test.rb是:

require 'test_helper'

class StaticPagesControllerTest < ActionController::TestCase
  test "should get home" do
    get :home
    assert_response :success
    assert_select "title", "Ruby on Rails Tutorial Sample App" 
  end

  test "should get help" do
    get :help
    assert_response :success
    assert_select "title", "Help | Ruby on Rails Tutorial Sample App" 
  end

  test "should get about" do
    get :about
    assert_response :success
    assert_select "title", "About | Ruby on Rails Tutorial Sample App" 
  end

  test "should get contact" do
    get :contact
    assert_response :success
    assert_select "title", "Contact | Ruby on Rails Tutorial Sample App"
  end

end

我的routes.rb是:

Rails.application.routes.draw do
  root 'static_pages#home'

  get 'static_pages/help'

  get 'static_pages/about'

  get 'static_pages/contact'
end

我认为一切都很好。我收到了我按的所有链接,但我的测试失败了。有谁知道为什么?

2 个答案:

答案 0 :(得分:0)

第一行是说明您需要先构建架构。尝试运行rake db:migrate

答案 1 :(得分:0)

确保准确填写以下文件。

application_helper.rb

module ApplicationHelper

  # Returns the full title on a per-page basis.
  def full_title(page_title = '')
    base_title = "Ruby on Rails Tutorial Sample App"
    if page_title.empty?
      base_title
    else
      page_title + " | " + base_title
    end
  end
end

contact.html.erb

<% provide(:title, 'Contact') %>
<h1>Contact</h1>
<p>
  Contact the Ruby on Rails Tutorial about the sample app at the
  <a href="http://www.railstutorial.org/#contact">contact page</a>.
</p>

about.html.erb

<% provide(:title, "About") %>
<h1>About</h1>
<p>
  The <a href="http://www.railstutorial.org/"><em>Ruby on Rails
  Tutorial</em></a> is a
  <a href="http://www.railstutorial.org/book">book</a> and
  <a href="http://screencasts.railstutorial.org/">screencast series</a>
  to teach web development with
  <a href="http://rubyonrails.org/">Ruby on Rails</a>.
  This is the sample application for the tutorial.
</p>

help.html.erb

<% provide(:title, "Help") %>
<h1>Help</h1>
<p>
  Get help on the Ruby on Rails Tutorial at the
  <a href="http://www.railstutorial.org/#help">Rails Tutorial help section</a>.
  To get help on this sample app, see the
  <a href="http://www.railstutorial.org/book"><em>Ruby on Rails Tutorial</em>
  book</a>.
</p>

然后在命令行中修复第一个错误(也由 @ 0r4cl3 建议):rake db:migrate

如果上述所有步骤仍无法修复所有错误,请检查以下文件:

application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title><%= full_title(yield(:title)) %></title>
    <%= stylesheet_link_tag 'application', media: 'all',
                                           'data-turbolinks-track' => true %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
    <%= csrf_meta_tags %>
    <%= render 'layouts/shim' %>
  </head>
  <body>
    <%= render 'layouts/header' %>
    <div class="container">
      <% flash.each do |message_type, message| %>
        <div class="alert alert-<%= message_type %>"><%= message %></div>
      <% end %>    
      <%= yield %>
      <%= render 'layouts/footer' %>
      <%= debug(params) if Rails.env.development? %>
    </div>
  </body>
</html>
相关问题