2015-10-28 63 views
0

請幫忙解決問題。如何通過capybara DSL登錄?

我使用寶石: '水豚', '撬', 'rspec的', '色器件'

我需要輸出登錄後仿真安慰標記。

application.html.erb:

<!DOCTYPE html> 
<html> 
<head> ..................</head> 
<body> 
    <% if user_signed_in? %> 
     <span>Здравствуйте, <span id="emailUser"><%= current_user.email %></span></span> 
     <%= link_to 'Выйти', destroy_user_session_path, :method => :delete %> 
    <% else %> 
     <%= link_to 'Войти', new_user_session_path %> или <%= link_to 'Зарегистрироваться', new_user_registration_path %> 
    <% end %> 
    <%= yield %> 
</body> 
</html> 

spec_helper.rb:

ENV["RAILS_ENV"] ||= 'test' 
require File.expand_path("../../config/environment", __FILE__) 
require 'rspec/rails' 
#require 'rspec/autorun' 

require 'capybara/rails' 
require 'capybara/rspec' 
include Capybara::DSL 
............ 
.......... 
RSpec.configure do |config| 
    config.include Devise::TestHelpers, type: :controller 
    ............ 
    .......... 

規格/工廠/ users.rb的:

FactoryGirl.define do 
    factory :user do 
    sequence(:email){ |i| "us#{i}@ad.ad" } 
    password 'qwertyui' 
    password_confirmation{ |u| u.password }  
    end 
end 

規格/控制器/ messages_controller_spec.rb :

it "login via capybara" do 
    @user = FactoryGirl.create(:user) 
    visit new_user_session_path 

    binding.pry 

    fill_in "user_email", :with => @user.email 
    fill_in "user_password", :with => "qwertyui" 
    click_button "commitSignIn" 
    visit '/' 

    expect(response).to render_template("index") 
end 

運行RSpec的測試控制檯顯示後如下:

[email protected] ~/rails/resource_test $ rspec spec/controllers 
From: /home/kalinin/rails/resource_test/spec/controllers/messages_controller_spec.rb @ line 44 : 

    39: describe "GET #index" do 
    40:  it "login via capybara" do 
    41:  @user = FactoryGirl.create(:user) 
    42:  visit new_user_session_path 
    43: 
=> 44:  binding.pry 
    45: 
    46:  fill_in "user_email", :with => @user.email 
    47:  fill_in "user_password", :with => "qwertyui" 
    48:  click_button "commitSignIn" 
    49:  visit '/' 

[1] pry(#<RSpec::ExampleGroups::MessagesController::GETIndex>)> puts page.html 
<!DOCTYPE html> 
<html> 
.......... 
......... 
... 

但運行後另一個RSpec的測試控制檯顯示如下:

[email protected] ~/rails/resource_test $ rspec spec/controllers 
From: /home/kalinin/rails/resource_test/spec/controllers/messages_controller_spec.rb @ line 48 : 

    43: 
    44:  fill_in "user_email", :with => @user.email 
    45:  fill_in "user_password", :with => "qwertyui" 
    46:  click_button "commitSignIn" 
    47:  visit '/' 
=> 48:  binding.pry 
    49: 
    50:  expect(response).to render_template("index") 
    51:  #expect(page).to have_selector('#emailUser', :text => @user.email) 
    52:  end 
    53: 

[1] pry(#<RSpec::ExampleGroups::MessagesController::GETIndex>)> puts page.html 

=> nil 

我不明白爲什麼命令將顯示page.html中無。我需要獲取標記元素

回答

0

您需要在訪問新位置之前驗證頁面上的內容,否則您的訪問可能會中斷由click_button觸發的表單的提交。

click_button "commitSignIn" 
expect(page).to have_content("Some text that appears on page when sign in is successful") 
# Now you can visit other pages if needed - or follow links on the current page 

你看到零這個頁面的原因很可能是因爲瀏覽器已要求您要訪問的頁面之前binding.pry被中斷,再次爲什麼你應該等待可見的內容在頁面上出現。

而且 - 你真的不應該在控制器的規格使用水豚,它集成/功能規格與響應對象。要render_template(「指數」不工作,所以你不能指望做(響應) ),同時使用水豚。