这让我发疯了。 Capybara无法找到点击链接!视图是从Rails表单助手创建的,最初在视图代码中如下所示:
<%= f.submit "Go!", id: "submit-button" class: "btn btn-primary btn-large btn-block" %>
请注意,此代码段位于部分_form.html.erb
中,由测试正在访问的requests/new
页面调用(即new_request_path
)。我最初认为这是一个问题,但后来我使用launchy进行了save_and_open_page
,下面的代码来自Inspect Element,它清楚地表明代码正常呈现。
<input class="btn btn-primary btn-large btn-block" name="commit" id="submit-button" type="submit" value="Go!">
但是这个测试仍然会产生错误。
require 'spec_helper' #should have the right config; required capybara/rspec and capybara/rails, and included Capybara::DSL
describe "simple user flow" do
it "should carry form information to redirected edit page" do
visit new_request_path
fill_in('Email we will use to contact you', :with => "fake@gmail.com")
# save_and_open_page # this is what created the view snippet I've shown above
# below are all the variations of clicking that i tried, none of which worked!
click_link('Go!') #value
click_link('submit-button') #id
click_link('commit') #name
click_link('btn btn-primary btn-large btn-block') #class
click_link('submit') #type
click_button('Go!') #value
click_button('submit-button') #id
click_button('commit') #name
click_button('btn btn-primary btn-large btn-block') #class
click_button('submit') #type
expect(page).to have_content "fake@gmail.com"
end
end
我永远坚持以下错误:
Failure/Error: click_link('commit')
Capybara::ElementNotFound:
Unable to find link "commit"
FYI Spec_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'capybara/rails'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
config.include Capybara::DSL
config.include RSpec::Rails::ViewRendering
config.render_views
end