简体   繁体   English

Michael Hartl撰写的Rails教程-第5.3章

[英]Rails Tutorial by Michael Hartl - Chapter 5.3

When I run $ bundle exec rake test I am getting the following failures: 当我运行$ 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 

My /static_pages_controller_test.rb is: 我的/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

my routes.rb is: 我的routes.rb是:

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

  get 'static_pages/help'

  get 'static_pages/about'

  get 'static_pages/contact'
end

Everything is working fine though, I think. 我认为一切都很好。 I am getting every link that I press but my tests fail. 我得到了我按下的每个链接,但是测试失败。 Does anyone have any idea why? 有谁知道为什么吗?

The first line is stating you need to build the schema first. 第一行说明您需要先构建架构。 Try running rake db:migrate 尝试运行rake db:migrate

Make sure the following files are filled in exactly. 确保以下文件正确填写。

application_helper.rb : 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 : 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 : 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 : 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>

Then to fix the first error, (as also suggested by @0r4cl3 ) in the command line run: rake db:migrate . 然后,要解决第一个错误,(在@ 0r4cl3中也建议)在命令行中运行: rake db:migrate

If all the above steps still do not fix all your errors, check the following file: 如果上述所有步骤仍不能解决所有错误,请检查以下文件:

application.html.erb : 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM