我的spec / features / posts_spec.rb中的测试代码如下所示
require 'spec_helper'
feature 'Posts' do
scenario 'Editing of Micropost', js: true do
visit '/signin'
fill_in 'Email', with: 'user@example.com
...
代码适用于js: true
。但是,如果我取出js: true
,则测试会在fill_in 'Email'
处失败,如果我在此行之前立即使用save_and_open_page,我会看到
Not Found: /signin
我的理解是我不应该放js: true
,除非我需要测试一个javascript函数,默认的rack_test
驱动程序应该可以工作。出了什么问题?我的spec_helper文件如下
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
require 'capybara'
require 'capybara/rails'
require 'capybara/rspec'
require 'capybara-screenshot'
require 'capybara-screenshot/rspec'
require 'capybara/poltergeist'
require 'pp'
require Rails.root.join('app/services/dbg').to_s
require 'database_cleaner_support'
require 'shoulda_matchers_support'
require 'chris_matchers_support'
require 'chris_helpers_support'
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
Capybara.default_host = 'www.example.com'
RSpec.configure do |config|
Capybara.default_driver = :rack_test
Capybara.javascript_driver = :poltergeist
Capybara::Webkit.configure(&:block_unknown_urls)
Capybara::Screenshot.prune_strategy = { keep: 20 }
Capybara::Screenshot.append_timestamp = false
config.include Capybara::UserAgent::DSL
config.include Rails.application.routes.url_helpers
config.include ApplicationHelper
config.include AccountsHelper
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = false
config.filter_run focus: true
config.run_all_when_everything_filtered = true
ActiveRecord::Migration.maintain_test_schema! # dont need db:test:prepare
end
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
end
# config.before(:each, :js => true) do
# DatabaseCleaner.strategy = :truncation
# end
config.before(:each) do |example|
if example.example.metadata[:js]
DatabaseCleaner.strategy = :truncation
else
DatabaseCleaner.strategy = :transaction
end
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
答案 0 :(得分:0)
解决方案是删除行
Capybara.default_host = 'www.example.com'
我不确定为什么会导致这个问题。