1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
require 'isolation/abstract_unit'
class LoadingTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def setup
build_app
boot_rails
end
def teardown
teardown_app
end
def app
@app ||= Rails.application
end
test "constants in app are autoloaded" do
app_file "app/models/post.rb", <<-MODEL
class Post < ActiveRecord::Base
validates_acceptance_of :title, :accept => "omg"
end
MODEL
require "#{rails_root}/config/environment"
setup_ar!
p = Post.create(:title => 'omg')
assert_equal 1, Post.count
assert_equal 'omg', p.title
p = Post.first
assert_equal 'omg', p.title
end
test "models without table do not panic on scope definitions when loaded" do
app_file "app/models/user.rb", <<-MODEL
class User < ActiveRecord::Base
default_scope where(:published => true)
end
MODEL
require "#{rails_root}/config/environment"
setup_ar!
User
end
test "load config/environments/environment before Bootstrap initializers" do
app_file "config/environments/development.rb", <<-RUBY
AppTemplate::Application.configure do
config.development_environment_loaded = true
end
RUBY
add_to_config <<-RUBY
config.before_initialize do
config.loaded = config.development_environment_loaded
end
RUBY
require "#{app_path}/config/environment"
assert ::AppTemplate::Application.config.loaded
end
test "descendants are cleaned on each request without cache classes" do
add_to_config <<-RUBY
config.cache_classes = false
config.reload_classes_only_on_change = false
RUBY
app_file "app/models/post.rb", <<-MODEL
class Post < ActiveRecord::Base
end
MODEL
app_file 'config/routes.rb', <<-RUBY
AppTemplate::Application.routes.draw do
match '/load', :to => lambda { |env| [200, {}, Post.all] }
match '/unload', :to => lambda { |env| [200, {}, []] }
end
RUBY
require 'rack/test'
extend Rack::Test::Methods
require "#{rails_root}/config/environment"
setup_ar!
assert_equal [], ActiveRecord::Base.descendants
get "/load"
assert_equal [Post], ActiveRecord::Base.descendants
get "/unload"
assert_equal [], ActiveRecord::Base.descendants
end
test "initialize_cant_be_called_twice" do
require "#{app_path}/config/environment"
assert_raise(RuntimeError) { ::AppTemplate::Application.initialize! }
end
test "reload constants on development" do
add_to_config <<-RUBY
config.cache_classes = false
RUBY
app_file 'config/routes.rb', <<-RUBY
AppTemplate::Application.routes.draw do
match '/c', :to => lambda { |env| [200, {"Content-Type" => "text/plain"}, [User.counter.to_s]] }
end
RUBY
app_file "app/models/user.rb", <<-MODEL
class User
def self.counter; 1; end
end
MODEL
require 'rack/test'
extend Rack::Test::Methods
require "#{rails_root}/config/environment"
sleep(1)
get "/c"
assert_equal "1", last_response.body
app_file "app/models/user.rb", <<-MODEL
class User
def self.counter; 2; end
end
MODEL
get "/c"
assert_equal "2", last_response.body
end
test "does not reload constants on development if custom file watcher always returns false" do
add_to_config <<-RUBY
config.cache_classes = false
config.file_watcher = Class.new do
def initialize(*); end
def updated?; false; end
end
RUBY
app_file 'config/routes.rb', <<-RUBY
AppTemplate::Application.routes.draw do
match '/c', :to => lambda { |env| [200, {"Content-Type" => "text/plain"}, [User.counter.to_s]] }
end
RUBY
app_file "app/models/user.rb", <<-MODEL
class User
def self.counter; 1; end
end
MODEL
require 'rack/test'
extend Rack::Test::Methods
require "#{rails_root}/config/environment"
sleep(1)
get "/c"
assert_equal "1", last_response.body
app_file "app/models/user.rb", <<-MODEL
class User
def self.counter; 2; end
end
MODEL
get "/c"
assert_equal "1", last_response.body
end
protected
def setup_ar!
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define(:version => 1) do
create_table :posts do |t|
t.string :title
end
end
end
end
|