0

私は 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

edit.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
4

2 に答える 2

0

ここでこの回答を見つけました: Michael Hartl による Ruby on Rails チュートリアル。第 9.1 章の失敗したテスト

_header.html.erb ファイルを変更して、「#」を「users_path」に更新すると、全体が緑色に点灯しました。

Header.html ファイル:

<header class="navbar navbar-fixed-top navbar-inverse">
  <div class="navbar-inner">
    <div class="container">
      <%= link_to "sample app", root_path, id: "logo" %>
      <nav>
        <ul class="nav pull-right">
          <li><%= link_to "Home", root_path %></li>
          <li><%= link_to "Help", help_path %></li>
          <% if signed_in? %>
            <li><%= link_to "Users", 'users_path' %></li>
            <li id="fat-menu" class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                Account <b class="caret"></b>
              </a>
              <ul class="dropdown-menu">
                <li><%= link_to "Profile", current_user %></li>
                <li><%= link_to "Settings", edit_user_path(current_user) %></li>
                <li class="divider"></li>
                <li>
                  <%= link_to "Sign out", signout_path, method: "delete" %>
                </li>
              </ul>
            </li>
          <% else %>
            <li><%= link_to "Sign in", signin_path %></li>
          <% end %>
        </ul>
      </nav>
    </div>
  </div>
</header>

ユーザーページの仕様:

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

describe "signup" do

    before { visit signup_path }

    describe "with invalid information" do
        it "should not create a user" do
            expect { click_button "Create my account"}.not_to change(User, :count)
        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"
      end

it "should create a user" do
        expect do
          click_button "Create my account"
         end.to change(User, :count).by(1)
      end

describe "after saving the user" do
        before { click_button "Create my account" }
        let(:user) { User.find_by_email('user@example.com') }

        it { should have_selector('title', text: user.name) }
        it { should have_selector('div.alert.alert-success', text: 'Welcome') }
        it { should have_link('Sign out') }
        end
     end
end

describe "edit" do
    let(:user) { FactoryGirl.create(:user) }
    before do
      sign_in user
      visit edit_user_path(user)
    end

    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
end
end
于 2013-04-13T18:21:49.413 に答える
0

訪問したページが正しくないようです

このコードをdescribe "page"セクションにドロップすると

it { should == '' }

エラー メッセージには、実際にアクセスされているページが表示されます。これにより、問題がどこにあるかの手がかりが得られます。これは、レンダリングされているページの html を確認するだけの便利な方法だと思います。

于 2013-04-13T07:54:44.570 に答える