2013-11-14 107 views
1

我正在通過Hartl衆所周知的Ruby on Rails 4教程開展工作。我對Rails並不陌生,但我對測試很陌生:RSpec,Capybara,FactoryGirl等。我在編輯第9章(http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users#sec-successful_edits)中的用戶編輯時遇到了一些問題。我會發布我已經嘗試的第一個補救措施,然後是相關的代碼:Hartl的Ruby on Rails教程測試失敗(第9章)

  1. 與本教程相比,三重檢查了我的代碼。一切看起來都不錯,但我可能會錯過一些東西。
  2. 重新啓動我的服務器並重置,重新填充並重新設置測試數據庫。
  3. 在FF中使用SQLite管理器擴展檢查測試和開發SQLite dbs中的用戶。

下面是錯誤:

1) Authentication Signin page with valid information followed by signout 
Failure/Error: before { click_link "Sign Out" } 
Capybara::ElementNotFound: 
    Unable to find link "Sign Out" 
# ./spec/requests/authentication_pages_spec.rb:41:in `block (5 levels) in <top (required)>' 

2) UserPages edit with invalid information 
Failure/Error: before { click_button "Save changes" } 
Capybara::ElementNotFound: 
    Unable to find button "Save changes" 
# ./spec/requests/user_pages_spec.rb:130:in `block (4 levels) in <top (required)>' 

3) UserPages edit page 
Failure/Error: it { should have_title("Edit user") } 
    expected #has_title?("Edit user") to return true, got false 
# ./spec/requests/user_pages_spec.rb:125:in `block (4 levels) in <top (required)>' 

4) UserPages edit page 
Failure/Error: it { should have_content("Update your profile") } 
    expected #has_content?("Update your profile") to return true, got false 
# ./spec/requests/user_pages_spec.rb:124:in `block (4 levels) in <top (required)>' 

5) UserPages edit page 
Failure/Error: it { should have_link('change', href: 'http://gravatar.com/emails') } 
    expected #has_link?("change", {:href=>"http://gravatar.com/emails"}) to return true, got false 
# ./spec/requests/user_pages_spec.rb:126:in `block (4 levels) in <top (required)>' 

6) UserPages edit with valid information 
Failure/Error: fill_in "Name",    with: new_name 
Capybara::ElementNotFound: 
    Unable to find field "Name" 
# ./spec/requests/user_pages_spec.rb:138:in `block (4 levels) in <top (required)>' 

7) UserPages edit with valid information 
Failure/Error: fill_in "Name",    with: new_name 
Capybara::ElementNotFound: 
    Unable to find field "Name" 
# ./spec/requests/user_pages_spec.rb:138:in `block (4 levels) in <top (required)>' 

8) UserPages edit with valid information 
Failure/Error: fill_in "Name",    with: new_name 
Capybara::ElementNotFound: 
    Unable to find field "Name" 
# ./spec/requests/user_pages_spec.rb:138:in `block (4 levels) in <top (required)>' 

9) UserPages edit with valid information 
Failure/Error: fill_in "Name",    with: new_name 
Capybara::ElementNotFound: 
    Unable to find field "Name" 
# ./spec/requests/user_pages_spec.rb:138:in `block (4 levels) in <top (required)>' 

10) UserPages edit with valid information 
Failure/Error: fill_in "Name",    with: new_name 
Capybara::ElementNotFound: 
    Unable to find field "Name" 
# ./spec/requests/user_pages_spec.rb:138:in `block (4 levels) in <top (required)>' 

起初,我認爲這是一個水豚的問題,但我不知道這一點。下面是測試代碼(在./spec/requests/user_pages_spec.rb找到):

describe "edit", :js => true do 
    let(:user) { FactoryGirl.create(:user) } 
    before do 
    sign_in user 
    visit edit_user_path(user) 
    end 

    describe "page" do 
    it { should have_content("Update your profile") } 
    it { should have_title("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 

    describe "with valid information" do 
    let(:new_name) { "New Name" } 
    let(:new_email) { "[email protected]" } 
    before do 
     fill_in "Name",    with: new_name 
     fill_in "Email",   with: new_email 
     fill_in "Password",   with: user.password 
     fill_in "Confirm Password", with: user.password 
     click_button "Save changes" 
    end 

    it { should have_title(new_name) } 
    it { should have_selector('div.alert.alert-success') } 
    it { should have_link('Sign Out', href: signout_path) } 
    specify { expect(user.reload.name).to eq new_name } 
    specify { expect(user.reload.email).to eq new_email } 
    end 
end 

我加入最初的描述塊:js => true,因爲我認爲這是一個水豚的問題,該頁面被加載太快,但,並補充說,哈希導致FF瀏覽器打開,我可以觀察到發生了什麼...... FactoryGirl成功地創建用戶,但它嘗試登錄時得到一個無效的電子郵件或密碼錯誤。所以它無法登錄,因此所有編輯表單的測試失敗。下面是相關的FactoryGirl代碼:在./spec/utilities.rb

sign_in()助手:

def sign_in(user, options={}) 
    if options[:no_capybara] 
    # Sign in when not using Capybara. 
    remember_token = User.new_remember_token 
    cookies[:remember_token] = remember_token 
    user.update_attribute(:remember_token, User.encrypt(remember_token)) 
    else 
    visit signin_path 
    fill_in "Email", with: user.email 
    fill_in "Password", with: user.password 
    click_button "Sign In" 
    end 
end 

./spec/factories.rb:

FactoryGirl.define do 
    factory :user do 
    sequence(:name) { |n| "Person #{n}" } 
    sequence(:email) { |n| "person_#{n}@example.com"} 
    password "foobar" 
    password_confirmation "foobar" 

    factory :admin do 
     admin true 
    end 
    end 
end 

我有正確的寶石安裝和FactoryGirl似乎與其他測試工作正常,這裏是gemfile:

group :test do 
    gem 'selenium-webdriver', '2.35.1' 
    gem 'capybara', '2.1.0' 
    gem 'growl', '1.0.3' 
    gem 'factory_girl_rails', '4.2.1' 
end 

就像我說的,我看了一下在SQLite開發和測試數據庫,我沒有看到測試數據庫中的任何用戶。 FactoryGirl是否僅臨時創建用戶進行測試,然後在完成時回滾?什麼會導致像我看到的失敗跡象?還是有什麼我失蹤?

萬一有人問,這裏有相關的觀點:

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

和在application.html呈現_header.html.erb .erb

<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> 
+0

#1 spec錯誤在哪裏?我認爲那是登錄時遇到問題的那個?錯誤2-11可能是由此引起的,所以如果我們能夠看到/解決這個問題,其他人可能會落到位。 – CDub

+0

是的,FactoryGirl爲用戶創建特定的測試或測試塊,如果在'context' /'describe'塊內的'before'塊中完成的話。一旦完成,工廠就會被銷燬。 – CDub

+0

對不起,我應該說,錯誤1與同一測試無關,但可能是由同一問題引起的: 1)身份驗證登錄頁面,其中包含有效信息,然後登出 失敗/錯誤:在{click_link「之前登錄Out「} Capybara :: ElementNotFound: 無法找到鏈接」退出「 #./spec/requests/authentication_pages_spec.rb:41:in ' – user2993894

回答

1

您所得到的第一個錯誤很可能是因爲您需要有y我們​​嵌套在describe "after saving the user"像這樣:

describe "after saving the user" do 
     before { click_button submit } 
     let(:user) { User.find_by(email: '[email protected]') } 

     it { should have_link('Sign out') } 
     it { should have_title(user.name) } 
     it { should have_selector('div.alert.alert-success', text: 'Welcome') } 
     describe "followed by signout" do #Notice that this describe is inside the other one 
      before { click_link "Sign out" } 
      it { should have_link('Sign in') } 
     end 
end 

我知道,這只是部分的答案,但它很難解決所有的問題一次。一旦你解決了這個問題,再次運行測試並告訴我什麼錯誤代碼保持。我會盡力幫助你。

(至少如果這解決了你的第一個問題,那可能是別的東西)

+0

爲我修復 - 謝謝! – Cleverlemming

相關問題