4
我在使用Sinatra和水豚時遇到了一些問題。使用水豚測試純JavaScript應用程序
我想測試一個純JavaScript應用程序。這只是一個簡單的index.html,由Sinatra提供。
require "sinatra"
get "/" do
File.read("public/index.html")
end
比方說,我想測試這個代碼。
$("a.link").click(function(){
$(this).replaceWith("New String");
});
<a href="#link" class="link">Click me!</a>
然後測試看起來像這樣。
describe "requests", js: true do
it "should display a message" do
visit "/"
click_link "Click me!"
page.should have_content("New String")
end
end
問題是沒有任何反應。根據Ryan Bates screencast如果將js: true
添加到describe
塊中,則Firefox應啓動並運行測試。
這是我的spec_helper
文件。
require "rspec"
require "capybara"
require "capybara/dsl"
Capybara.javascript_driver = :selenium
require_relative "./../server"
Capybara.app = Sinatra::Application
Capybara.javascript_driver = :selenium
Capybara.default_wait_time = 10
RSpec.configure do |config|
config.mock_with :rspec
config.include Capybara
end
以下是運行時的輸出rspec rspec/request_spec.rb
。
requests
should display a message (FAILED - 1)
Failures:
1) requests should display a message
Failure/Error: page.should have_content("New String")
expected #has_content?("New String") to return true, got false
# ./spec/request_spec.rb:5:in `block (2 levels) in <top (required)>'
Finished in 4.38 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/request_spec.rb:2 # requests should display a message
我創建了Github上的完整例如項目可以在這裏找到: https://github.com/oleander/capybara-js-fails
任何人都知道失敗的原因?