我是编写集成测试的新手,所以请原谅我这个问题看起来很简陋(尽管我花了几个小时在网上寻找答案)。
我正在使用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-helper:
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>
UsersController:
class UsersController < ApplicationController
before_action :redirect_current_user
def new
render :new
end
end
SessionsController:
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
的ApplicationController:
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
答案 0 :(得分:1)
已解决:
正如@dwenzel在评论中正确指出的那样,点击后应该出现的内容完全由Javascript呈现。
使用Capybara时,默认设置是页面上的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