我有一个设置为:remote => true的表单,它有两个提交按钮(一个用于“测试连接”,一个用于创建/更新)。我的控制器正确地处理这个问题,并根据单击的按钮呈现正确的视图。
我有以下集成测试,以确保如果数据源能够连接,它将向用户显示正确的消息:
describe "Data Source Validation", :js => true do
before (:each) do
@user = create_logged_in_user
end
it "returns true when data source is valid" do
DataSource.any_instance.stub(:can_connect).and_return(true)
visit new_data_source_path
fill_in "Name", :with => "Example 123"
fill_in "Host", :with => "myip.example.com"
select "SQL Server", :from => "Database type"
fill_in "Database name", :with => "Example"
fill_in "Username", :with => "user"
fill_in "Password", :with => "password"
click_button "Test Connection"
expect(page).to have_content "Successfully connected to database"
end
end我正在使用gem "capybara-webkit",并且在spec_helper.rb中定义了Capybara.javascript_driver = :webkit。
当测试运行时,我得到以下结果:
Failure/Error: expect(page).to have_content "Successfully connected to database"
expected to find text "Successfully connected to database" in ...当我在Chrome中查看它时,它的工作方式与我所期望的完全一样,并带有正确的错误消息.
我怎样才能通过这个测试条件?
为“测试连接”方法执行的data_source_controller.rb代码
begin
if @data_source.valid? && @data_source.can_connect?
format.js {render "valid_connection" }
else
format.js {render "invalid_connection" }
end
rescue Exception => e
format.js {render "invalid_connection", locals: {error_msg: e.message} }
end编辑#1
我将javascript驱动程序切换到:selenium,并遇到了同样的问题。我还试图添加"wait_for_ajax“方法助手,并收到了一个错误:
Failure/Error: wait_for_ajax
Capybara::Webkit::InvalidResponseError:
Javascript failed to execute只有正常的webkit驱动程序和没有等待/睡眠的完整错误消息:
Failure/Error: expect(page).to have_content "Successfully connected to database"
expected to find text "Successfully connected to database" in "Dashboard Reports Data Sources Account Create a New Data Source * Name * Host Port * Database type * Database name * Username * Password We encrypt all information in the database. Nothing can be retrieved without the proper credentials and encryption key. Copyright 2013"我期待的是“成功连接到数据库”的文本,在“密码”之后和“我们加密数据库中的所有信息”之前动态显示。
我希望这能让我有更多的洞察力,我可以尝试把一个github项目放在一起来测试它,但是这是令人沮丧的尝试。
发布于 2013-11-16 17:56:43
我也遇到了一个类似的问题: rspec/capybara在浏览器看起来很好的情况下,没有检测到ajax的变化。这就是对我有用的东西:
添加到GemFile中
gem 'capybara-webkit'添加到主机文件
127.0.0.1 testhost.com添加到spec_helper.rb中
DEFAULT_HOST = "testhost.com"
DEFAULT_PORT = 7171
RSpec.configure do |config|
config.include Capybara::DSL
Capybara.javascript_driver = :webkit
Capybara.default_host = "http://#{DEFAULT_HOST}"
Capybara.server_port = DEFAULT_PORT
Capybara.app_host = "http://#{DEFAULT_HOST}:#{Capybara.server_port}"
#fixes issues with capybara not detecting db changes made during tests
config.use_transactional_fixtures = false
config.before :each do
if Capybara.current_driver == :rack_test
DatabaseCleaner.strategy = :transaction
else
DatabaseCleaner.strategy = :truncation
end
DatabaseCleaner.start
end
config.after do
DatabaseCleaner.clean
end
end在给出问题的测试中,在描述标题中添加a :js =>真标志,如下所示:
describe 'my test', :js => true do我也不能将描述块与:js => true嵌套在其他描述块中,但它必须独立工作,我认为这与从rack_test切换到webkit进行测试有关。
发布于 2013-10-04 06:28:49
尝试创建助手方法:
def wait_for_ajax
counter = 0
while page.execute_script("return $.active").to_i > 0
counter += 1
sleep(0.1)
raise "AJAX request took longer than 5 seconds." if counter >= 50
end
end然后:
click_button "Test Connection"
wait_for_ajax
expect(page).to have_content "Successfully connected to database"发布于 2013-10-03 11:42:19
我认为这可能是铬版的问题,请尝试更新驱动程序或,您甚至可以参考这。
https://stackoverflow.com/questions/19083321
复制相似问题