article How to test an autocomplete with Rails and Minitest?
29 Oct 2013
Are you eager to elevate your security skills and safeguard your applications
against cyber threats? I created a Rails Security course is designed
specifically for developers like you who aim to build robust, secure Rails
applications!
Buy my course: Security for Rails Developers.
An autocomplete is a nice example for an ajax driven feature and I will demonstrate how to test such a features with Rails 4 and Minitest.
First we will create a sample app and setup Minitest and Capybara for integration testing.
Buy my course: Security for Rails Developers.
rails new rails-autocomplete-test
group :development, :test do
gem "capybara"
gem 'poltergeist'
end
require "capybara/rails"
class ActionDispatch::IntegrationTest
include Capybara::DSL
require 'capybara/poltergeist'
Capybara.javascript_driver = :poltergeist
def teardown
Capybara.current_driver = nil
end
end
class ActiveRecord::Base
mattr_accessor :shared_connection
@@shared_connection = nil
def self.connection
@@shared_connection || retrieve_connection
end
end
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
require 'test_helper'
class AutocompleteTest < ActionDispatch::IntegrationTest
test "autocomplete" do
Capybara.current_driver = Capybara.javascript_driver
visit "/"
fill_in('search_keyword', with: 'Test')
end
end
1) Error:
AutocompleteTest#test_autocomplete:
Capybara::ElementNotFound: Unable to find field "search_keyword"
test/integration/autocomplete_test.rb:8:in `block in <class:AutocompleteTest>'
1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
# config/routes.rb
root 'welcome#index'
rails g controller Welcome index
create app/controllers/welcome_controller.rb
route get "welcome/index"
invoke erb
create app/views/welcome
create app/views/welcome/index.html.erb
invoke test_unit
create test/controllers/welcome_controller_test.rb
invoke helper
create app/helpers/welcome_helper.rb
invoke test_unit
create test/helpers/welcome_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/welcome.js.coffee
invoke scss
create app/assets/stylesheets/welcome.css.scss
# app/views/welcome/index.html.erb
<%= form_for :search do |f| %>
<%= f.label :keyword %>
<%= f.text_field :keyword %>
<% end %>
app/assets/javascripts/application.js
...
//= require typeahead
...
<span class="tt-dropdown-menu">
{{#dataset}}
<div class="tt-dataset-{{name}}">
{{{header}}}
<span class="tt-suggestions">
{{#suggestions}}
<div class="tt-suggestion">{{{html}}}</div>
{{/suggestions}}
</span>
{{{footer}}}
</div>
{{/dataset}}
</span>
require 'test_helper'
class AutocompleteTest < ActionDispatch::IntegrationTest
test "autocomplete" do
Capybara.current_driver = Capybara.javascript_driver
visit "/"
field = 'search_keyword'
fill_in('search_keyword', with: 'Test')
page.execute_script %Q{ $('##{field}').trigger("focus") }
suggestion = page.find('div.tt-suggestion')
assert_equal "Test", suggestion.text
end
end
# config/routes.rb
match 'suggestions' => 'welcome#suggestions', via: :get
# app/controller/welcome_controller.rb
def suggestions
render json: [{name: 'Test'}]
end
$ ->
$('#search_keyword').typeahead [
{
name: 'name'
remote: {
url: '/suggestions.json?q=%QUERY'
}
valueKey: 'name'
}
]
Or follow me on Twitter
I run an indie startup providing vulnerability scanning for your Ruby on Rails app.
It is free to use at the moment, and I am grateful for any feedback about it.If you would like to give it a spin, you can do it here: Vulnerability Scanning for your Ruby on Rails app!