首页 > 解决方案 > Rails 测试错误,同时学习 rails 教程

问题描述

我目前正在学习 Rails 教程。在 Rails 测试后,我有错误消息,TDD。还有更多消息,但首先我想调试这些。尽管 Rails 测试运行不佳,但应用程序本身现在运行没有问题。

我看了一下这个示例代码。 https://github.com/yasslab/sample_apps

错误信息

 11) Error:
StaticPagesControllerTest#test_should_get_about:
NameError: undefined local variable or method `static_pages_about_url' for #<StaticPagesControllerTest:0x00000006f89228>
StaticPagesControllerTest#test_should_get_home:NameError: undefined local variable or method `static_pages_home_url' for #<StaticPagesControllerTest:0x00000006f964a0>
    test/controllers/static_pages_controller_test.rb:7:in `block in <class:StaticPagesControllerTest>' 13) Error:
StaticPagesControllerTest#test_should_get_contact:NameError: undefined local variable or method `static_pages_contact_url' for #<StaticPagesControllerTest:0x0000000623d600>
    test/controllers/static_pages_controller_test.rb:27:in `block in <class:StaticPagesControllerTest>' 14) Error:StaticPagesControllerTest#test_should_get_help:NameError: undefined local variable or method `static_pages_help_url' for #<StaticPagesControllerTest:0x00000006f9d6d8>    test/controllers/static_pages_controller_test.rb:13:in `block in <class:StaticPagesControllerTest>'

static_pages_controller.rb

class StaticPagesController < ApplicationController
  def home
  end

  def help
  end

  def about
  end

  def contact
  end
end

static_pages_controller_test.rb

class StaticPagesControllerTest < ActionController::TestCase


  test "should get home" do
    get static_pages_home_url
    assert_response :success
    assert_select "title", "Ruby on Rails Tutorial Sample App"
  end

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

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

  end


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

路线.rb

Rails.application.routes.draw do
  get 'sessions/new'

  root 'static_pages#home'
  get  '/help',    to: 'static_pages#help'
  get  '/about',   to: 'static_pages#about'
  get  '/contact', to: 'static_pages#contact'
  get  '/signup',  to: 'users#new'
  post '/signup', to: 'users#create'

  get '/login', to: 'sessions#new'
  post '/login', to: 'sessions#create'
  delete '/logout', to: 'sessions#destroy'
  resources :users
end

我想把这些错误变成断言。你能帮帮我吗?

标签: ruby-on-railsruby-on-rails-5

解决方案


推荐阅读