在RoR教程第3版第7章上运行rake测试时出错

时间:2014-10-22 14:29:39

标签: ruby-on-rails

在执行第3版Ruby on Rails教程的第7章时,我无法让我的rake测试运行Green。

受影响文件的代码如下。

ERROR["test_should_get_new", UsersControllerTest, 3.036159469]
 test_should_get_new#UsersControllerTest (3.04s)
ActionView::Template::Error:         ActionView::Template::Error: undefined method `users_path'     for #<#<Class:0x00000007294440>:0x000000072a7f18>
            app/views/users/new.html.erb:6:in     `_app_views_users_new_html_erb__951977753260241405_60205740'
            test/controllers/users_controller_test.rb:5:in `block in <class:UsersControllerTest>'
        app/views/users/new.html.erb:6:in     `_app_views_users_new_html_erb__951977753260241405_60205740'
    test/controllers/users_controller_test.rb:5:in `block in <class:UsersControllerTest>'

ERROR["test_layout_links", SiteLayoutTest, 4.296022857]
 test_layout_links#SiteLayoutTest (4.30s)
ActionView::Template::Error:         ActionView::Template::Error: undefined method `users_path' for #<#<Class:0x00000007294440>:0x00000007aebd78>
        app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb__951977753260241405_60205740'
        test/integration/site_layout_test.rb:12:in `block in <class:SiteLayoutTest>'
    app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb__951977753260241405_60205740'
    test/integration/site_layout_test.rb:12:in `block in <class:SiteLayoutTest>'

ERROR["test_invalid_signup_information", UsersSignupTest, 4.432177815]
 test_invalid_signup_information#UsersSignupTest (4.43s)
ActionView::Template::Error:         ActionView::Template::Error: undefined method `users_path' for #<#<Class:0x00000007294440>:0x00000007bbb1e0>
        app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb__951977753260241405_60205740'
        test/integration/users_signup_test.rb:6:in `block in <class:UsersSignupTest>'
    app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb__951977753260241405_60205740'
    test/integration/users_signup_test.rb:6:in `block in <class:UsersSignupTest>'

ERROR["test_valid_signup_information", UsersSignupTest, 4.510334368]
 test_valid_signup_information#UsersSignupTest (4.51s)
ActionView::Template::Error:         ActionView::Template::Error: undefined method `users_path' for #<#<Class:0x00000007294440>:0x00000005dc3b10>
        app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb__951977753260241405_60205740'
        test/integration/users_signup_test.rb:17:in `block in <class:UsersSignupTest>'
    app/views/users/new.html.erb:6:in `_app_views_users_new_html_erb__951977753260241405_60205740'
    test/integration/users_signup_test.rb:17:in `block in <class:UsersSignupTest>'

18/18: [==================================================] 100% Time: 00:00:04, Time: 00:00:04

Finished in 4.83507s
18 tests, 32 assertions, 0 failures, 4 errors, 0 skips

我的new.html.erb文件

 <% provide(:title, 'Sign up') %>
<h1>Sign up</h1>

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@user) do |f| %>
      <%= render 'shared/error_messages' %>

      <%= f.label :name %>
      <%= f.text_field :name, class: 'form-control' %>

      <%= f.label :email %>
      <%= f.text_field :email, class: 'form-control' %>

      <%= f.label :password %>
      <%= f.password_field :password, class: 'form-control' %>

      <%= f.label :password_confirmation, "Confirmation" %>
      <%= f.password_field :password_confirmation, class: 'form-control' %>

      <%= f.submit "Create my account", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

my users_controllers_test file

require 'test_helper'

class UsersControllerTest < ActionController::TestCase
  test "should get new" do
    get :new
    assert_response :success
  end

end

site_layout_test

require 'test_helper'

class SiteLayoutTest < ActionDispatch::IntegrationTest

  test "layout links" do
    get root_path
    assert_template 'static_pages/home'
    assert_select "a[href=?]", root_path, count: 2
    assert_select "a[href=?]", help_path
    assert_select "a[href=?]", about_path
    assert_select "a[href=?]", contact_path
    get signup_path
    assert_select "title", full_title("Sign up")
  end
end

我的user_signup_test

require 'test_helper'

class UsersSignupTest < ActionDispatch::IntegrationTest

  test "invalid signup information" do
    get signup_path
    assert_no_difference 'User.count' do
      post users_path, user: { name:  "",
                           email: "user@invalid",
                           password:              "foo",
                           password_confirmation: "bar" }
    end
    assert_template 'users/new'
  end

  test "valid signup information" do
    get signup_path
    name     = "Example User"
    email    = "user@example.com"
    password = "password"
    assert_difference 'User.count', 1 do
      post_via_redirect users_path, user: { name:  name,
                                        email: email,
                                        password:              password,
                                        password_confirmation: password }
    end
    assert_template 'users/show'
  end
end

,最后是我的custom.css文件

@import "bootstrap-sprockets";
@import "bootstrap";

/* mixins, variables, etc. */

$gray-medium-light: #eaeaea;

@mixin box_sizing {
  -moz-box-sizing:    border-box;
  -webkit-box-sizing: border-box;
  box-sizing:         border-box;
}

/* universal */

html {
  overflow-y: scroll;
}

body {
  padding-top: 60px;
}

section {
  overflow: auto;
}

textarea {
  resize: vertical;
}

.center {
  text-align: center;
  h1 {
    margin-bottom: 10px;
  }
}

/* typography */

h1, h2, h3, h4, h5, h6 {
  line-height: 1;
}

h1 {
  font-size: 3em;
  letter-spacing: -2px;
  margin-bottom: 30px;
  text-align: center;
}

h2 {
  font-size: 1.2em;
  letter-spacing: -1px;
  margin-bottom: 30px;
  text-align: center;
  font-weight: normal;
  color: $gray-light;
}

p {
  font-size: 1.1em;
  line-height: 1.7em;
}


/* header */

#logo {
  float: left;
  margin-right: 10px;
  font-size: 1.7em;
  color: white;
  text-transform: uppercase;
  letter-spacing: -1px;
  padding-top: 9px;
  font-weight: bold;
  &:hover {
    color: white;
    text-decoration: none;
  }
}

/* footer */

footer {
  margin-top: 45px;
  padding-top: 5px;
  border-top: 1px solid $gray-medium-light;
  color: $gray-light;
  a {
    color: $gray;
    &:hover {
      color: $gray-darker;
    }
  }
  small {
    float: left;
  }
  ul {
    float: right;
    list-style: none;
    li {
      float: left;
      margin-left: 15px;
    }
  }
}

/* miscellaneous */

.debug_dump {
  clear: both;
  float: left;
  width: 100%;
  margin-top: 45px;
 @include box_sizing;
}  


/* sidebar */

aside {
  section.user_info {
    margin-top: 20px;
  }
  section {
    padding: 10px 0;
    margin-top: 20px;
    &:first-child {
      border: 0;
      padding-top: 0;
    }
    span {
      display: block;
      margin-bottom: 3px;
      line-height: 1;
    }
    h1 {
      font-size: 1.4em;
      text-align: left;
      letter-spacing: -1px;
      margin-bottom: 3px;
      margin-top: 0px;
    }
  }
}

.gravatar {
  float: left;
  margin-right: 10px;
}

.gravatar_edit {
  margin-top: 15px;
}

/* forms */

input, textarea, select, .uneditable-input {
  border: 1px solid #bbb;
  width: 100%;
  margin-bottom: 15px;
  @include box_sizing;
}

input {
  height: auto !important;
}

#error_explanation {
  color: red;
  ul {
    color: red;
    margin: 0 0 30px 0;
  }
}

.field_with_errors {
  @extend .has-error;
  .form-control {
    color: $state-danger-text;
  }
}

如果有人能够解释我的错误,我将不胜感激

2 个答案:

答案 0 :(得分:0)

根据Isaffie的建议,问题在于路线文件。

此时文件应如下所示。

Rails.application.routes.draw do
  root             'static_pages#home'
  get 'help'    => 'static_pages#help'
  get 'about'   => 'static_pages#about'
  get 'contact' => 'static_pages#contact'
  get 'signup'  => 'users#new'
  resources :users
end

答案 1 :(得分:0)

检查你的routes.rb文件 - 使用&#39; rake routes&#39;看看你是否能看到users_path