Skip to content

Commit 7a88d3d

Browse files
author
Emmanuel Delgado
committed
Refactorizing tests
1 parent b0cabea commit 7a88d3d

File tree

7 files changed

+119
-214
lines changed

7 files changed

+119
-214
lines changed

test/active_record_controller_test.rb

Lines changed: 0 additions & 106 deletions
This file was deleted.

test/active_record_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require "test_helper"
2+
require 'test_controller'
3+
require 'support/active_record'
4+
5+
class ActiveRecordControllerTest < ActionController::TestCase
6+
include Rails3JQueryAutocomplete::Test::Setup
7+
include Rails3JQueryAutocomplete::Test
8+
end

test/mongoid_controller_test.rb

Lines changed: 0 additions & 106 deletions
This file was deleted.

test/mongoid_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require "test_helper"
2+
require 'test_controller'
3+
require 'support/mongoid'
4+
5+
class MonogidControllerTest < ActionController::TestCase
6+
include Rails3JQueryAutocomplete::Test::Setup
7+
include Rails3JQueryAutocomplete::Test
8+
end

test/support/active_record.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Rails3JQueryAutocomplete
22
module Test
3-
module ActiveRecord
3+
module Setup
44
def setup
55
::ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
66
::ActiveRecord::Schema.define(:version => 1) do

test/support/mongoid.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Rails3JQueryAutocomplete
22
module Test
3-
module Mongoid
3+
class ActorsControllerTest
44
def setup
55
::Mongoid.configure do |config|
66
name = "rails3_jquery_autocomplete_test"

test/test_controller.rb

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
module Rails3JQueryAutocomplete
2+
module Test
3+
4+
include Shoulda::InstanceMethods
5+
extend Shoulda::ClassMethods
6+
include Shoulda::Assertions
7+
extend Shoulda::Macros
8+
include Shoulda::Helpers
9+
10+
context "the autocomplete gem" do
11+
12+
should "be able to access the autocomplete action regardless of the quality of param[:term]" do
13+
get :autocomplete_movie_name
14+
assert_response :success
15+
16+
get :autocomplete_movie_name, :term => ''
17+
assert_response :success
18+
19+
get :autocomplete_movie_name, :term => nil
20+
assert_response :success
21+
22+
get :autocomplete_movie_name, :term => 'Al'
23+
assert_response :success
24+
end
25+
26+
should "respond with expected json" do
27+
get :autocomplete_movie_name, :term => 'Al'
28+
json_response = JSON.parse(@response.body)
29+
assert_equal(json_response.first["label"], @movie1.name)
30+
assert_equal(json_response.first["value"], @movie1.name)
31+
assert_equal(json_response.first["id"].to_s, @movie1.id.to_s)
32+
end
33+
34+
should "return results in alphabetical order by default" do
35+
get :autocomplete_movie_name, :term => 'Al'
36+
json_response = JSON.parse(@response.body)
37+
assert_equal(json_response.first["label"], "Alpha")
38+
assert_equal(json_response.last["label"], "Alzpha")
39+
end
40+
41+
should "be able to sort in other ways if desired" do
42+
@controller.class_eval do
43+
autocomplete :movie, :name, {:order => "name DESC"}
44+
end
45+
46+
get :autocomplete_movie_name, :term => 'Al'
47+
json_response = JSON.parse(@response.body)
48+
assert_equal(json_response.first["label"], "Alzpha")
49+
assert_equal(json_response.last["label"], "Alpha")
50+
end
51+
52+
should "be able to limit the results" do
53+
@controller.class_eval do
54+
autocomplete :movie, :name, {:limit => 1}
55+
end
56+
57+
get :autocomplete_movie_name, :term => 'Al'
58+
json_response = JSON.parse(@response.body)
59+
assert_equal(json_response.length, 1)
60+
end
61+
62+
should "ignore case of search term and results" do
63+
@movie = @movie_class.create(:name => 'aLpHa')
64+
65+
@controller.class_eval do
66+
autocomplete :movie, :name
67+
end
68+
69+
get :autocomplete_movie_name, :term => 'Al'
70+
json_response = JSON.parse(@response.body)
71+
assert_equal(json_response.length, @movie_class.count)
72+
assert_equal(json_response.last["label"], 'aLpHa')
73+
end
74+
75+
should "match term to letters in middle of words when full-text search is on" do
76+
@controller.class_eval do
77+
autocomplete :movie, :name, {:full => true}
78+
end
79+
80+
get :autocomplete_movie_name, :term => 'ph'
81+
json_response = JSON.parse(@response.body)
82+
assert_equal(json_response.length, @movie_class.count)
83+
assert_equal(json_response.first["label"], @movie1.name)
84+
end
85+
86+
should "be able to customize what is displayed" do
87+
@controller.class_eval do
88+
autocomplete :movie, :name, {:display_value => :display_name}
89+
end
90+
91+
get :autocomplete_movie_name, :term => 'Al'
92+
93+
json_response = JSON.parse(@response.body)
94+
95+
assert_equal(@movie1.display_name, json_response.first["label"])
96+
assert_equal(@movie1.display_name, json_response.first["value"])
97+
assert_equal(@movie1.id.to_s, json_response.first["id"].to_s)
98+
end
99+
end
100+
end
101+
end

0 commit comments

Comments
 (0)