Skip to content

Commit 305a299

Browse files
committed
Complete advanced testing setup
1 parent c2e666b commit 305a299

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

Guardfile

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# This is the Guardfile for the 8th edition of the Rails Tutorial.
2+
# For the 7th edition, see here:
3+
# https://github.com/learnenough/sample_app_7th_ed/blob/main/Guardfile
4+
5+
require "active_support/inflector"
6+
# Defines the matching rules for Guard.
7+
guard :minitest, all_on_start: false do
8+
watch(%r{^test/(.*)/?(.*)_test\.rb$})
9+
watch("test/test_helper.rb") { "test" }
10+
watch("config/routes.rb") { interface_tests }
11+
watch(%r{app/views/layouts/*}) { interface_tests }
12+
watch(%r{^app/models/(.*?)\.rb$}) do |matches|
13+
["test/models/#{matches[1]}_test.rb",
14+
"test/integration/microposts_interface_test.rb"]
15+
end
16+
watch(%r{^test/fixtures/(.*?)\.yml$}) do |matches|
17+
"test/models/#{matches[1].singularize}_test.rb"
18+
end
19+
watch(%r{^app/mailers/(.*?)\.rb$}) do |matches|
20+
"test/mailers/#{matches[1]}_test.rb"
21+
end
22+
watch(%r{^app/views/(.*)_mailer/.*$}) do |matches|
23+
"test/mailers/#{matches[1]}_mailer_test.rb"
24+
end
25+
watch(%r{^app/controllers/(.*?)_controller\.rb$}) do |matches|
26+
resource_tests(matches[1])
27+
end
28+
watch(%r{^app/views/([^/]*?)/.*\.html\.erb$}) do |matches|
29+
["test/controllers/#{matches[1]}_controller_test.rb"] +
30+
integration_tests(matches[1])
31+
end
32+
watch(%r{^app/helpers/(.*?)_helper\.rb$}) do |matches|
33+
integration_tests(matches[1])
34+
end
35+
watch("app/views/layouts/application.html.erb") do
36+
"test/integration/site_layout_test.rb"
37+
end
38+
watch("app/helpers/sessions_helper.rb") do
39+
integration_tests << "test/helpers/sessions_helper_test.rb"
40+
end
41+
watch("app/controllers/sessions_controller.rb") do
42+
["test/controllers/sessions_controller_test.rb",
43+
"test/integration/users_login_test.rb"]
44+
end
45+
watch("app/controllers/account_activations_controller.rb") do
46+
"test/integration/users_signup_test.rb"
47+
end
48+
watch(%r{app/views/users/*}) do
49+
resource_tests("users") +
50+
["test/integration/microposts_interface_test.rb"]
51+
end
52+
watch("app/controllers/relationships_controller.rb") do
53+
["test/controllers/relationships_controller_test.rb",
54+
"test/integration/following_test.rb"]
55+
end
56+
end
57+
58+
# Returns the integration tests corresponding to the given resource.
59+
def integration_tests(resource = :all)
60+
if resource == :all
61+
Dir["test/integration/*"]
62+
else
63+
Dir["test/integration/#{resource}_*.rb"]
64+
end
65+
end
66+
67+
# Returns all tests that hit the interface.
68+
def interface_tests
69+
integration_tests << "test/controllers"
70+
end
71+
72+
# Returns the controller tests corresponding to the given resource.
73+
def controller_test(resource)
74+
"test/controllers/#{resource}_controller_test.rb"
75+
end
76+
77+
# Returns all tests for the given resource.
78+
def resource_tests(resource)
79+
integration_tests(resource) << controller_test(resource)
80+
end

test/test_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
ENV["RAILS_ENV"] ||= "test"
22
require_relative "../config/environment"
33
require "rails/test_help"
4+
require "minitest/reporters"
5+
Minitest::Reporters.use!
46

57
module ActiveSupport
68
class TestCase

0 commit comments

Comments
 (0)