Capybara 看到空白页 - 集成测试
Capybara sees blank page - integration tests
我是编写集成测试的新手,所以如果这个问题看起来很初级,请原谅(尽管我花了几个小时在网上寻找答案)。
我正在与 Rails 4、RSpec 和水豚一起工作。
这是我的代码:
RSpec.feature "Sign up" do
it "has a working link to sign in page" do
visit '/users/new'
click_on("Sign in")
expect(page).to have_content "Sign in to continue" <= the problem is here
end
end
错误:
1) Sign up has a working link to sign in page
Failure/Error: expect(page).to have_content "Sign in to continue"
expected to find text "Sign in to continue" in ""
Link 在浏览器中工作正常。
我试过保存并打开页面,它确实显示了一个空白页面。
我的所有 RSpec 单元测试都使用当前配置。
可能是控制器没有呈现我的页面吗?如果是,我该如何解决?此时我没有任何控制器测试。
非常感谢任何想法!
我的规范助手:
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
我的rails助手:
require 'capybara/rspec'
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.include FactoryGirl::Syntax::Methods
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
end
全图:
<header class='header'>
<div class='dog-logo'></div>
<aside>
<a class='blue-button' href='<%= new_session_url %>'>Sign in</a>
</aside>
</header>
<h1 class='create-new-account-header-text'>Create your account!</h1>
<form class='create-account-form group' id='create-account-form'>
<label>Name</label>
<input type='text' for='Name' name='user[first_name]'
placeholder="First" class='sign-up-first-name'>
<input type='text' for='Name' name='user[last_name]'
placeholder="Last" class='sign-up-last-name'>
</label>
<label>Choose your username</label>
<input type='text' name='user[username]'
for='Choose your username'>
<label>Create a password</label>
<input type='password' name='user[password]' class='sign-up-password'
for='Create a password'>
<label>Confirm your password</label>
<input type='password' name='confirmPassword' class='sign-up-confirm-password'
for='Confirm your password'>
<label>Birthday</label>
<select name='birthday-month' for='Birthday'>
<option>January</option>
<option>February</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>August</option>
<option>September</option>
<option>October</option>
<option>November</option>
<option>December</option>
</select>
<input name='birthday-day' type='text' placeholder="Day" for='Birthday'>
<input name='birthday-year' type='text' placeholder="Year" for='Birthday'>
<label>Gender</label>
<select name="user[gender]" for='Gender'>
<option value="" disabled selected style='display:none;'>I am</option>
<option value='M'>Male</option>
<option value='F'>Female</option>
<option value='O'>Other</option>
</select>
<input type='checkbox' for='I agree to be awesome!'
class='sign-up-checkbox'>
<label class='sign-up-awesome-label'>I agree to be awesome!</label>
<button class='blue-button'>Create account</button>
</form>
用户控制器:
class UsersController < ApplicationController
before_action :redirect_current_user
def new
render :new
end
end
会话控制器:
class SessionsController < ApplicationController
before_action :require_user!, only: [:destroy]
before_action :redirect_current_user, only: [:new]
def new
render :new
end
def destroy
current_user.reset_session_token!
session[:session_token] = nil
redirect_to new_session_url
end
end
应用程序控制器:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :current_user, :logged_in?
def current_user
@current_user ||= User.find_by(session_token: session[:session_token])
end
def logged_in?
!!current_user
end
def log_in!(user)
user.reset_session_token!
session[:session_token] = user.session_token
end
def log_out!
current_user.reset_session_token!
session[:session_token] = nil
end
def require_user!
redirect_to new_session_url unless logged_in?
end
def redirect_current_user
redirect_to root_url if current_user
end
end
已解决:
正如@dwenzel 在评论中正确指出的那样,点击后应该出现的内容完全由 Javascript 呈现。
使用水豚时,默认设置是页面上的Javascript不会得到运行。通过安装 capybara-webkit(请参阅 capybara-webkit github 了解更多信息)并包含行
解决了该问题
Capybara.javascript_driver = :webkit
在我的 spec_helper.rb 中。
请注意,您需要在定义示例组时通过编写 js: true 明确告诉 RSpec 测试涉及 JS,例如:
RSpec.feature "Sign up", js: true do
...
// your test examples here
...
end
我是编写集成测试的新手,所以如果这个问题看起来很初级,请原谅(尽管我花了几个小时在网上寻找答案)。
我正在与 Rails 4、RSpec 和水豚一起工作。
这是我的代码:
RSpec.feature "Sign up" do
it "has a working link to sign in page" do
visit '/users/new'
click_on("Sign in")
expect(page).to have_content "Sign in to continue" <= the problem is here
end
end
错误:
1) Sign up has a working link to sign in page
Failure/Error: expect(page).to have_content "Sign in to continue"
expected to find text "Sign in to continue" in ""
Link 在浏览器中工作正常。 我试过保存并打开页面,它确实显示了一个空白页面。 我的所有 RSpec 单元测试都使用当前配置。 可能是控制器没有呈现我的页面吗?如果是,我该如何解决?此时我没有任何控制器测试。
非常感谢任何想法!
我的规范助手:
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
我的rails助手:
require 'capybara/rspec'
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.include FactoryGirl::Syntax::Methods
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
end
全图:
<header class='header'>
<div class='dog-logo'></div>
<aside>
<a class='blue-button' href='<%= new_session_url %>'>Sign in</a>
</aside>
</header>
<h1 class='create-new-account-header-text'>Create your account!</h1>
<form class='create-account-form group' id='create-account-form'>
<label>Name</label>
<input type='text' for='Name' name='user[first_name]'
placeholder="First" class='sign-up-first-name'>
<input type='text' for='Name' name='user[last_name]'
placeholder="Last" class='sign-up-last-name'>
</label>
<label>Choose your username</label>
<input type='text' name='user[username]'
for='Choose your username'>
<label>Create a password</label>
<input type='password' name='user[password]' class='sign-up-password'
for='Create a password'>
<label>Confirm your password</label>
<input type='password' name='confirmPassword' class='sign-up-confirm-password'
for='Confirm your password'>
<label>Birthday</label>
<select name='birthday-month' for='Birthday'>
<option>January</option>
<option>February</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>August</option>
<option>September</option>
<option>October</option>
<option>November</option>
<option>December</option>
</select>
<input name='birthday-day' type='text' placeholder="Day" for='Birthday'>
<input name='birthday-year' type='text' placeholder="Year" for='Birthday'>
<label>Gender</label>
<select name="user[gender]" for='Gender'>
<option value="" disabled selected style='display:none;'>I am</option>
<option value='M'>Male</option>
<option value='F'>Female</option>
<option value='O'>Other</option>
</select>
<input type='checkbox' for='I agree to be awesome!'
class='sign-up-checkbox'>
<label class='sign-up-awesome-label'>I agree to be awesome!</label>
<button class='blue-button'>Create account</button>
</form>
用户控制器:
class UsersController < ApplicationController
before_action :redirect_current_user
def new
render :new
end
end
会话控制器:
class SessionsController < ApplicationController
before_action :require_user!, only: [:destroy]
before_action :redirect_current_user, only: [:new]
def new
render :new
end
def destroy
current_user.reset_session_token!
session[:session_token] = nil
redirect_to new_session_url
end
end
应用程序控制器:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :current_user, :logged_in?
def current_user
@current_user ||= User.find_by(session_token: session[:session_token])
end
def logged_in?
!!current_user
end
def log_in!(user)
user.reset_session_token!
session[:session_token] = user.session_token
end
def log_out!
current_user.reset_session_token!
session[:session_token] = nil
end
def require_user!
redirect_to new_session_url unless logged_in?
end
def redirect_current_user
redirect_to root_url if current_user
end
end
已解决:
正如@dwenzel 在评论中正确指出的那样,点击后应该出现的内容完全由 Javascript 呈现。
使用水豚时,默认设置是页面上的Javascript不会得到运行。通过安装 capybara-webkit(请参阅 capybara-webkit github 了解更多信息)并包含行
解决了该问题Capybara.javascript_driver = :webkit
在我的 spec_helper.rb 中。 请注意,您需要在定义示例组时通过编写 js: true 明确告诉 RSpec 测试涉及 JS,例如:
RSpec.feature "Sign up", js: true do
...
// your test examples here
...
end