2016-01-29 97 views
0

当我运行$ bundle exec rake test我得到以下故障:Rails的教程由迈克尔·哈特尔 - 第5.3章

$ 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 

尽管如此,我认为一切正常。我收到我按下的每个链接,但我的测试失败。有谁知道为什么?

+0

我得到的一切像static_pages /帮助static_pages /约等,但没有按”通过测试。 – clair13

回答

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> 

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

如果以上步骤仍不能解决你所有的错误,请检查以下文件:

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> 
+0

谢谢你的时间。我发现解决方案是在“#{page_title} |#{base_title}”之间的模块上的空间。问题解决了。 – clair13

0

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

相关问题