2013-04-13 4 views
0

Я работаю над учебником Rails Rails и застрял на начальном test как часть 9.1.1. Я просмотрел файлы user_pages_spec.rb, users_controller.rb, routes.rb, edit.html.erb и application_controller.rb и не могу найти, что не так.Rails Tutorial RSpec Error (User_Pages_Spec ch. 9.1.1)

Я получаю следующие 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: "[email protected]" 
     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 

routes.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 

ответ

0

Похоже посещенной страницы не правильный

Если вы уроните этот код в describe "page" раздел

it { should == '' }

Сообщение об ошибке должно показать вам, какую фактическую страницу посещает. Это даст вам представление о том, где проблема. Я нахожу это полезным способом только для того, чтобы увидеть html отображаемой страницы.

+0

Thaanks! Я добавил код и получил следующее: Ошибка/ошибка: он должен {должен == ''} ожидается: "" получил: "страница" (используя ==) # ./spec/requests/user_pages_spec.rb:40 : в блоке (4 уровня) в ' –

0

Я нашел ответ здесь: Ruby on Rails Tutorial by Michael Hartl. Failing Test in Chapter 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> 

Пользователь Страницы Spec:

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: "[email protected]" 
     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('[email protected]') } 

     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 
Смежные вопросы