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中無。我需要獲取標記元素