Ruby onl的Ruby on Rails教程第7.1.3章 - 失败测试

时间:2013-05-21 02:27:22

标签: ruby-on-rails rspec-rails ruby-on-rails-4

我花了几个小时试图弄清楚这个小小的拼图,然后再继续教程,这样它就不会在我以后爆炸了。因此,我知道测试失败的问题就像这些是一打一打,所以我道歉。在本教程的“7.1.3测试用户显示页面(带工厂)”部分(下面提供的链接)中,我无法通过注册页面测试。这似乎是如此微不足道,但我在任何看似无穷无尽的文件中都找不到任何错误,这些文件可能存在问题(控制器,路由,测试本身,用户模型等)。似乎没有人在网上遇到这个问题,所以我认为它必须是一些随机设置或我的rails项目。

无论如何,我认为这是相关的代码:
在sublime中运行user_pages_spec测试时出错:

Failures:

  1) User pages signup page 
     Failure/Error: it { should have_content('Sign up') }
       expected #has_content?("Sign up") to return true, got false
     # ./spec/requests/user_pages_spec.rb:18:in `block (3 levels) in <top (required)>'

  2) User pages signup page 
     Failure/Error: it { should have_title(full_title('Sign up')) }
       expected #has_title?("Ruby on Rails Tutorial Sample App | Sign up") to return true, got false
     # ./spec/requests/user_pages_spec.rb:19:in `block (3 levels) in <top (required)>'

Finished in 0.16194 seconds
4 examples, 2 failures

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:18 # User pages signup page 
rspec ./spec/requests/user_pages_spec.rb:19 # User pages signup page 

Randomized with seed 46255

[Finished in 2.7s with exit code 1]

spec / requests / user_pages_spec.rb

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit user_path(user) }

    it { should have_content(user.name) }
    it { should have_title(user.name) }
  end

  describe "signup page" do
    before { visit signup_path }

    it { should have_content('Sign up') }
    it { should have_title(full_title('Sign up')) }
  end
end

的应用程序/视图/用户/ new.html.erb

<% provide(:title, 'Sign Up') %>
<h1>Sign Up</h1>
<p>Find me in app/views/users/new.html.erb</p>

app / controller / users_controller.rb

class UsersController < ApplicationController

  def show
    @user = User.find(params[:id])
  end

  def new
  end
end

config / routes.rb

SampleApp::Application.routes.draw do
  resources :users
  root to: 'static_pages#home'
  match '/signup',  to: 'users#new',            via: 'get'
  match '/help',    to: 'static_pages#help',    via: 'get'
  match '/about',   to: 'static_pages#about',   via: 'get'
  match '/contact', to: 'static_pages#contact', via: 'get'
end

如果您能想到您可能需要查看的任何其他代码,请告诉我。我包含了new.html.erb,因为我认为注册测试正在该页面上查找,但我一定是错的。如果有人能让我知道我做错了什么导致了这次失败,我将永远感激不尽。

如果你可以说服我整个测试驱动的开发概念并不是后方的完全痛苦,那么你可以获得一些额外的布朗尼点。我发现这个教程非常有趣,但是每次进入测试部分我都会失去继续的动力,特别是当它不起作用时。从长远来看,我知道你的代码会更好,但是刚刚开始,我开始认为我应该忽略它有可能会放弃。

http://ruby.railstutorial.org/book/ruby-on-rails-tutorial?version=4.0#code-name_title_and_heading

1 个答案:

答案 0 :(得分:2)

您是否尝试将规范/ requests / user_pages_spec.rb 中的“注册”替换为“注册”?