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

编辑.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

看起来访问的页面不是正确的

如果您将此代码放入该describe "page"部分

it { should == '' }

错误消息应显示正在访问的实际页面。这将为您提供有关问题所在的线索。我发现这是一种有用的方法,可以查看正在呈现的页面的 html。

于 2013-04-13T07:54:44.570 回答
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 回答