Update assert_select for Nokogiri compatibility

assert_select has been moved into the rails-dom-testing gem and is now
based on Nokogiri.

* Remove assert_tag and assert_xml_select
* Quote CSS attribute selector values
* Use decoded versions of HTML in assert_select. Nokogiri decodes
  elements prior to matching.
* Add a test helper for entities such as ` ` for which it's
  difficult or confusing to include the decoded version directly in the
  assertion.
* Assert directly on Atom feeds' XML namespace because it isn't
  selectable as an attribute
This commit is contained in:
Dan Rice 2015-08-09 17:43:40 -04:00
parent d7d86446c0
commit 689db4c9c3
9 changed files with 66 additions and 64 deletions

View file

@ -1,6 +1,9 @@
require 'test_helper'
require 'support/html_entity_helper'
class ContextsControllerTest < ActionController::TestCase
include HTMLEntityHelper
fixtures :users, :preferences, :contexts
def test_contexts_list
@ -96,7 +99,7 @@ class ContextsControllerTest < ActionController::TestCase
login_as :admin_user
get :show, { :id => "0", :format => 'xml' }
assert_response 404
assert_xml_select 'error', 'Context not found'
assert_select 'error', 'Context not found'
end
# RSS
@ -107,7 +110,7 @@ class ContextsControllerTest < ActionController::TestCase
assert_equal 'application/rss+xml', @response.content_type
#puts @response.body
assert_xml_select 'rss[version="2.0"]' do
assert_select 'rss[version="2.0"]' do
assert_select 'channel' do
assert_select '>title', 'Tracks Contexts'
assert_select '>description', "Lists all the contexts for #{users(:admin_user).display_name}"
@ -118,7 +121,7 @@ class ContextsControllerTest < ActionController::TestCase
assert_select 'title', /.+/
assert_select 'description' do
assert_select_encoded do
assert_select 'p', /\d+&nbsp;actions. Context is (Active|Hidden)./
assert_select 'p', /\d+#{nbsp}actions. Context is (Active|Hidden)./
end
end
%w(guid link).each do |node|
@ -153,16 +156,15 @@ class ContextsControllerTest < ActionController::TestCase
login_as :admin_user
get :index, { :format => "atom" }
assert_equal 'application/atom+xml', @response.content_type
#puts @response.body
assert_xml_select 'feed[xmlns="http://www.w3.org/2005/Atom"]' do
assert_equal 'http://www.w3.org/2005/Atom', html_document.children[0].namespace.href
assert_select 'feed' do
assert_select '>title', 'Tracks Contexts'
assert_select '>subtitle', "Lists all the contexts for #{users(:admin_user).display_name}"
assert_select 'entry', 10 do
assert_select 'title', /.+/
assert_select 'content[type="html"]' do
assert_select_encoded do
assert_select 'p', /\d+&nbsp;actions. Context is (Active|Hidden)./
assert_select 'p', /\d+#{nbsp}actions. Context is (Active|Hidden)./
end
end
assert_select 'published', /(#{Regexp.escape(contexts(:agenda).created_at.xmlschema)}|#{Regexp.escape(contexts(:library).created_at.xmlschema)})/

View file

@ -26,4 +26,4 @@ class FeedlistControllerTest < ActionController::TestCase
assert_response 200
end
end
end

View file

@ -1,6 +1,8 @@
require 'test_helper'
require 'support/html_entity_helper'
class ProjectsControllerTest < ActionController::TestCase
include HTMLEntityHelper
def setup
end
@ -83,7 +85,7 @@ class ProjectsControllerTest < ActionController::TestCase
assert_equal 'application/rss+xml', @response.content_type
#puts @response.body
assert_xml_select 'rss[version="2.0"]' do
assert_select 'rss[version="2.0"]' do
assert_select 'channel' do
assert_select '>title', 'Tracks Projects'
assert_select '>description', "Lists all the projects for #{users(:admin_user).display_name}"
@ -94,7 +96,7 @@ class ProjectsControllerTest < ActionController::TestCase
assert_select 'title', /.+/
assert_select 'description' do
assert_select_encoded do
assert_select 'p', /^\d+&nbsp;actions\. Project is (active|hidden|completed)\.$/
assert_select 'p', /^\d+#{nbsp}actions\. Project is (active|hidden|completed)\.$/
end
end
%w(guid link).each do |node|
@ -127,16 +129,15 @@ class ProjectsControllerTest < ActionController::TestCase
login_as :admin_user
get :index, { :format => "atom" }
assert_equal 'application/atom+xml', @response.content_type
# puts @response.body
assert_xml_select 'feed[xmlns="http://www.w3.org/2005/Atom"]' do
assert_equal 'http://www.w3.org/2005/Atom', html_document.children[0].namespace.href
assert_select 'feed' do
assert_select '>title', 'Tracks Projects'
assert_select '>subtitle', "Lists all the projects for #{users(:admin_user).display_name}"
assert_select 'entry', 3 do
assert_select 'title', /.+/
assert_select 'content[type="html"]' do
assert_select_encoded do
assert_select 'p', /\d+&nbsp;actions. Project is (active|hidden|completed)./
assert_select 'p', /\d+#{nbsp}actions. Project is (active|hidden|completed)./
end
end
assert_select 'published', /(#{Regexp.escape(projects(:timemachine).updated_at.xmlschema)}|#{Regexp.escape(projects(:moremoney).updated_at.xmlschema)})/
@ -239,7 +240,7 @@ class ProjectsControllerTest < ActionController::TestCase
get :index, { :format => "xml" }
assert_equal 'application/xml', @response.content_type
assert_xml_select 'projects' do
assert_select 'projects' do
assert_select 'project', 3 do
assert_select 'name', /.+/
assert_select 'state', 'active'

View file

@ -1,7 +1,6 @@
require 'test_helper'
class TodosControllerTest < ActionController::TestCase
def test_get_index_when_not_logged_in
get :index
assert_redirected_to login_url
@ -235,8 +234,8 @@ class TodosControllerTest < ActionController::TestCase
"project_name"=>"Build a working time machine",
"todo"=>{"notes"=>"", "description"=>"Call Warren Buffet to find out how much he makes per day", "due"=>"30/11/2006"}, "tag_list"=>"foo bar" }
assert_response 409
assert_xml_select "errors" do
assert_xml_select "error", "Context can't be blank"
assert_select "errors" do
assert_select "error", "Context can't be blank"
end
end
@ -467,7 +466,7 @@ class TodosControllerTest < ActionController::TestCase
assert_equal 'application/rss+xml', @response.content_type
# puts @response.body
assert_xml_select 'rss[version="2.0"]' do
assert_select 'rss[version="2.0"]' do
assert_select 'channel' do
assert_select '>title', 'Tracks Actions'
assert_select '>description', "Actions for #{users(:admin_user).display_name}"
@ -488,7 +487,7 @@ class TodosControllerTest < ActionController::TestCase
login_as(:admin_user)
get :index, { :format => "rss", :limit => '5' }
assert_xml_select 'rss[version="2.0"]' do
assert_select 'rss[version="2.0"]' do
assert_select 'channel' do
assert_select '>title', 'Tracks Actions'
assert_select '>description', "Actions for #{users(:admin_user).display_name}"
@ -522,15 +521,14 @@ class TodosControllerTest < ActionController::TestCase
login_as :admin_user
get :index, { :format => "atom" }
assert_equal 'application/atom+xml', @response.content_type
# #puts @response.body
assert_xml_select 'feed[xmlns="http://www.w3.org/2005/Atom"]' do
assert_xml_select '>title', 'Tracks Actions'
assert_xml_select '>subtitle', "Actions for #{users(:admin_user).display_name}"
assert_xml_select 'entry', 17 do
assert_xml_select 'title', /.+/
assert_xml_select 'content[type="html"]', /.*/
assert_xml_select 'published', /(#{Regexp.escape(todos(:book).updated_at.xmlschema)}|#{Regexp.escape(projects(:moremoney).updated_at.xmlschema)})/
assert_equal 'http://www.w3.org/2005/Atom', html_document.children[0].namespace.href
assert_select 'feed' do
assert_select '>title', 'Tracks Actions'
assert_select '>subtitle', "Actions for #{users(:admin_user).display_name}"
assert_select 'entry', 17 do
assert_select 'title', /.+/
assert_select 'content[type="html"]', /.*/
assert_select 'published', /(#{Regexp.escape(todos(:book).updated_at.xmlschema)}|#{Regexp.escape(projects(:moremoney).updated_at.xmlschema)})/
end
end
end
@ -846,7 +844,7 @@ class TodosControllerTest < ActionController::TestCase
todo.notes = "foo #{url} bar"
todo.save!
get :index
assert_select "a[href=#{url}]"
assert_select "a[href='#{url}']"
end
def test_link_opened_in_new_window
@ -886,7 +884,7 @@ class TodosControllerTest < ActionController::TestCase
todo.save!
get :index
assert_select("div#notes_todo_#{todo.id}", 'A link to http://github.com/.')
assert_select("div#notes_todo_#{todo.id} a[href=http://github.com/]", 'http://github.com/')
assert_select("div#notes_todo_#{todo.id} a[href='http://github.com/']", 'http://github.com/')
end
def test_format_note_link_message
@ -895,9 +893,9 @@ class TodosControllerTest < ActionController::TestCase
todo.raw_notes = "A Mail.app message://<ABCDEF-GHADB-123455-FOO-BAR@example.com> link"
todo.save!
get :index
assert_select("div#notes_todo_#{todo.id}", 'A Mail.app message://&lt;ABCDEF-GHADB-123455-FOO-BAR@example.com&gt; link')
assert_select("div#notes_todo_#{todo.id} a", 'message://&lt;ABCDEF-GHADB-123455-FOO-BAR@example.com&gt;')
assert_select("div#notes_todo_#{todo.id} a[href=message://&lt;ABCDEF-GHADB-123455-FOO-BAR@example.com&gt;]", 'message://&lt;ABCDEF-GHADB-123455-FOO-BAR@example.com&gt;')
assert_select("div#notes_todo_#{todo.id}", 'A Mail.app message://<ABCDEF-GHADB-123455-FOO-BAR@example.com> link')
assert_select("div#notes_todo_#{todo.id} a", 'message://<ABCDEF-GHADB-123455-FOO-BAR@example.com>')
assert_select("div#notes_todo_#{todo.id} a[href='message://<ABCDEF-GHADB-123455-FOO-BAR@example.com>']", "message://<ABCDEF-GHADB-123455-FOO-BAR@example.com>")
end
def test_format_note_link_onenote
@ -908,7 +906,7 @@ class TodosControllerTest < ActionController::TestCase
get :index
assert_select("div#notes_todo_#{todo.id}", 'link me to onenote')
assert_select("div#notes_todo_#{todo.id} a", 'link me to onenote')
assert_select("div#notes_todo_#{todo.id} a[href=onenote:///E:%5COneNote%5Cdir%5Cnotes.one#PAGE&amp;section-id=%7BFD597D3A-3793-495F-8345-23D34A00DD3B%7D&amp;page-id=%7B1C95A1C7-6408-4804-B3B5-96C28426022B%7D&amp;end]", 'link me to onenote')
assert_select("div#notes_todo_#{todo.id} a[href='onenote:///E:%5COneNote%5Cdir%5Cnotes.one#PAGE&section-id=%7BFD597D3A-3793-495F-8345-23D34A00DD3B%7D&page-id=%7B1C95A1C7-6408-4804-B3B5-96C28426022B%7D&end']", 'link me to onenote')
end
##############

View file

@ -6,11 +6,12 @@ class ProjectXmlApiTest < ActionDispatch::IntegrationTest
def test_retrieve_project
authenticated_get_xml "/projects/1.xml", users(:admin_user).login, 'abracadabra', {}
assert_tag :tag => "project"
assert_tag :tag => "project", :child => {:tag => "not_done" }
assert_tag :tag => "project", :child => {:tag => "deferred" }
assert_tag :tag => "project", :child => {:tag => "pending" }
assert_tag :tag => "project", :child => {:tag => "done" }
assert_select 'project' do
assert_select 'not_done'
assert_select 'deferred'
assert_select 'pending'
assert_select 'done'
end
assert_response 200
end

View file

@ -24,12 +24,14 @@ class TodoXmlApiTest < ActionDispatch::IntegrationTest
def test_get_tickler_returns_all_deferred_and_pending_todos
number = @user.todos.deferred.count + @user.todos.pending.count
authenticated_get_xml "/tickler.xml", @user.login, @password, {}
assert_tag :tag => "todos", :children => { :count => number }
assert_select 'todos' do
assert_select 'todo', count: number
end
end
def test_get_tickler_omits_user_id
authenticated_get_xml "/tickler.xml", @user.login, @password, {}
assert_no_tag :tag => "user_id"
assert_select 'user_id', false
end
def test_get_index_with_only_active_todos
@ -245,7 +247,7 @@ class TodoXmlApiTest < ActionDispatch::IntegrationTest
<project_id type='integer'>-11</project_id>
</todo>"
assert_response 409
assert_xml_select 'errors' do
assert_select 'errors' do
assert_select 'error', 2
end
end
@ -261,4 +263,4 @@ class TodoXmlApiTest < ActionDispatch::IntegrationTest
authenticated_post_xml "/todos.xml", user, password, postdata
end
end
end

View file

@ -69,16 +69,17 @@ class UsersXmlApiTest < ActionDispatch::IntegrationTest
def test_get_users_as_xml
get '/users.xml', {}, basic_auth_headers()
assert_response :success
assert_tag :tag => "users",
:children => { :count => 4, :only => { :tag => "user" } }
assert_no_tag :tag => "password"
assert_select 'users' do
assert_select 'user', count: 4
end
assert_select 'password', false
end
def test_get_user_as_xml
get "/users/#{users(:other_user).id}.xml", {}, basic_auth_headers()
assert_response :success
assert_tag :tag => "user"
assert_no_tag :tag => "password"
assert_select 'user'
assert_select 'password', false
end
private

View file

@ -0,0 +1,11 @@
require 'htmlentities'
module HTMLEntityHelper
def entity(name)
HTMLEntities.new.decode("&#{name};")
end
def nbsp
entity('nbsp')
end
end

View file

@ -56,15 +56,6 @@ class ActiveSupport::TestCase
def assert_equal_dmy(date1, date2)
assert_equal date1.strftime("%d-%m-%y"), date2.strftime("%d-%m-%y")
end
def xml_document
@xml_document ||= HTML::Document.new(@response.body, false, true)
end
def assert_xml_select(*args, &block)
@html_document = xml_document
assert_select(*args, &block)
end
end
class ActionController::TestCase
@ -90,11 +81,6 @@ class ActionController::TestCase
xhr :post, :create, get_model_class.downcase => {:name => name}
end
def assert_xml_select(*args, &block)
@html_document = xml_document
assert_select(*args, &block)
end
private
def get_model_class
@ -145,7 +131,7 @@ class ActionDispatch::IntegrationTest
def assert_responses_with_error(error_msg)
assert_response 409
assert_xml_select 'errors' do
assert_select 'errors' do
assert_select 'error', 1, error_msg
end
end