【发布时间】:2015-10-25 21:42:35
【问题描述】:
我对编写集成测试还很陌生,所以如果这个问题看起来很初级,请原谅(尽管我花了几个小时在网上寻找答案)。
我正在使用 Rails 4、RSpec 和 Capybara。
这是我的代码:
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 ""
链接在浏览器中运行良好。 我尝试了保存并打开页面,它确实显示了一个空白页面。 我所有的 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
【问题讨论】:
-
您还有其他包含“登录”的链接或按钮吗?
-
不,它是唯一一个带有“登录”文本的
-
您能否分享您的观点,以便我们看到标记?
-
刚刚更新了问题。
-
我查看了您的新会话视图。如果我正确阅读了您的代码,那么该视图的所有渲染都来自 Backbone 视图。你可能需要做一些额外的配置来让你的 RSpec 测试使用 JavaScript 运行。这篇文章可能会有所帮助:stackoverflow.com/questions/15312572/…
标签: ruby-on-rails ruby rspec capybara integration-testing