我正在完成 Hartl 的 Rails 教程,并坚持作为 9.1.1 的一部分进行初始测试。我浏览了我的 user_pages_spec.rb、users_controller.rb、routes.rb、edit.html.erb 和 application_controller.rb 文件,但找不到问题所在。
我在终端中收到以下 3 个错误:
Ryans-MacBook-Air-3:tutorialapp rlhinchey$ bundle exec rspec spec/requests/user_pages_spec.rb -e "edit page"
Run options: include {:full_description=>/edit\ page/}
FFF
Failures:
  1) signup edit page 
     Failure/Error: it { should have_selector('h1',    text: "Update your profile") }
       expected css "h1" with text "Update your profile" to return something
     # ./spec/requests/user_pages_spec.rb:37:in `block (4 levels) in <top (required)>'
  2) signup edit page 
     Failure/Error: it { should have_link('change', href: 'http://gravatar.com/emails') }
       expected link "change" to return something
     # ./spec/requests/user_pages_spec.rb:39:in `block (4 levels) in <top (required)>'
  3) signup edit page 
     Failure/Error: it { should have_selector('title', text: "Edit user") }
       expected css "title" with text "Edit user" to return something
     # ./spec/requests/user_pages_spec.rb:38:in `block (4 levels) in <top (required)>'
Finished in 0.31167 seconds
3 examples, 3 failures
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:37 # signup edit page 
rspec ./spec/requests/user_pages_spec.rb:39 # signup edit page 
rspec ./spec/requests/user_pages_spec.rb:38 # signup edit page 
Randomized with seed 20756
在我看来,测试没有在 edit.html.erb 文件上进行?我是新手,所以我可能完全错了。任何帮助将非常感激!
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_selector('h1',    text: user.name) }
    it { should have_selector('title', text: user.name) }
end
  describe "signup page" do
    before { visit signup_path }
    it { should have_selector('h1',    text: 'Sign up') }
    it { should have_selector('title', text: full_title('Sign up')) }
  end
end
  describe "signup" do
    before { visit signup_path }
    let(:submit) { "Create my account" }
    describe "with invalid information" do
      it "should not create a user" do
        expect { click_button submit }.not_to change(User, :count)
      end 
    end
  describe "edit" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit edit_user_path(user) }
  describe "page" do
    it { should have_selector('h1',    text: "Update your profile") }
    it { should have_selector('title', text: "Edit user") }
    it { should have_link('change', href: 'http://gravatar.com/emails') }
  end
  describe "with invalid information" do
    before { click_button "Save changes" }
    it { should have_content('error') }
  end
end  
  describe "with valid information" do
    before do
        fill_in "Name",         with: "Example User"
        fill_in "Email",        with: "user@example.com"
        fill_in "Password",     with: "foobar"
        fill_in "Confirmation", with: "foobar"
       it "should create a user" do
        expect { click_button submit }.to change(User, :count).by(1)
  end
    describe "after saving the user" do
        it { should have_link("Sign out") }
      end
    end
  end
end
users_controller.rb
class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
  end
  def new
    @user = User.new
  end
  def create
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
        flash[:success] = "Welcome to the Sample App!"
        redirect_to @user
    else
        render 'new'    
       end
    end
  def edit
    @user = User.find(params[:id])
  end
end
编辑.html.erb:
<% provide(:title, "Edit user") %> 
<h1>Update your profile</h1>
<div class="row">
  <div class="span6 offset3">
    <%= form_for(@user) do |f| %>
      <%= render 'shared/error_messages' %>
      <%= f.label :name %>
      <%= f.text_field :name %>
      <%= f.label :email %>
      <%= f.text_field :email %>
      <%= f.label :password %>
      <%= f.password_field :password %>
      <%= f.label :password_confirmation, "Confirm Password" %>
      <%= f.password_field :password_confirmation %>
      <%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
    <% end %>
    <%= gravatar_for @user %>
    <a href="http://gravatar.com/emails">change</a>
  </div>
</div>
application_controller.rb:
class ApplicationController < ActionController::Base
  protect_from_forgery
  include SessionsHelper
  #force signout to prevent CSRF attacks
  def handle_unverified_request
    sign_out
    super
  end   
end
路线.rb
Tutorialapp::Application.routes.draw do
  resources :users
  resources :sessions, only: [:new, :create, :destroy]
  root to: 'static_pages#home'
  match '/help',    to: 'static_pages#help'
  match '/about',   to: 'static_pages#about'
  match '/contact', to: 'static_pages#contact'
  match '/signup',  to: 'users#new'
  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete