rails_anonymous_controller_testing 0.0.1 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -11
- data/lib/rails_anonymous_controller_testing/railtie.rb +29 -13
- data/lib/rails_anonymous_controller_testing/version.rb +1 -1
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 806ed4f101b16a4baade5b23b7af947809fa5f7e2d070001437ae44b5b2d4dc7
|
4
|
+
data.tar.gz: 5461ffdf99fd003cb1e5e345e6a127cfaa0f96c4f7f655dcf7518a1934c584ba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 148a834923806eeb61ffb38fb561f3a0f0596b94aacdc9d4910541b92fcd18b0fa9fdb383c031305dfce4123ffcdc4d3195bfb8a99ee6cd6dcda691a67724615
|
7
|
+
data.tar.gz: 6989348f767f3497142d64df8de6b56f159b050f6899b9ad7d9b3ea7348831dea4bfbbba0923f671c8d20f3a5309a485689513b2b8fd17096c01bc6e0be56270
|
data/README.md
CHANGED
@@ -12,6 +12,9 @@ This only works with Minitest, specifically for tests that inherit from
|
|
12
12
|
|
13
13
|
## Usage
|
14
14
|
|
15
|
+
Here's a relatively straightforward example. There are a few caveats you might
|
16
|
+
need to keep in mind for more complicated cases; see the Caveats section below.
|
17
|
+
|
15
18
|
```ruby
|
16
19
|
class MyControllerTest < ActionDispatch::IntegrationTest
|
17
20
|
# 1. Call the `controller` method. You must specify the base controller to
|
@@ -25,7 +28,8 @@ class MyControllerTest < ActionDispatch::IntegrationTest
|
|
25
28
|
end
|
26
29
|
end
|
27
30
|
|
28
|
-
# 2. Optionally, set views.
|
31
|
+
# 2. Optionally, set views. You can set the layout for your anonymous
|
32
|
+
# controller too.
|
29
33
|
views["layouts/application.html.erb"] = <<~HTML
|
30
34
|
<h1>My anonymous test</h1>
|
31
35
|
<%= yield %>
|
@@ -63,18 +67,23 @@ end
|
|
63
67
|
```
|
64
68
|
|
65
69
|
|
66
|
-
###
|
70
|
+
### Caveats
|
67
71
|
|
68
|
-
A few things to note
|
72
|
+
A few things to note
|
69
73
|
|
70
|
-
*
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
74
|
+
* You can only define **one** `controller do` per test class. But you can get
|
75
|
+
around this limitation by defining another test class in one file.
|
76
|
+
* Views are generated only once and cached. The cache key is determined by MD5
|
77
|
+
hashing the test file contents, and test file path, and the line number of the
|
78
|
+
`controller do`.
|
79
|
+
* You can disable the view cache by calling `disable_anonymous_view_cache!` on
|
80
|
+
your test class. This doesn't get around the limitation of having just one
|
81
|
+
`controller do` definition in your test class.
|
82
|
+
* The view cache is located in `tmp/anonymous_controller_views` and can be
|
83
|
+
cleared anytime
|
84
|
+
* Setting a custom layout will only affect your anonymous controller. This gem
|
85
|
+
does not provide any way to affect the behavior of existing controllers
|
86
|
+
defined by your Rails application or engine
|
78
87
|
|
79
88
|
|
80
89
|
## Installation
|
@@ -1,5 +1,5 @@
|
|
1
1
|
class RailsAnonymousControllerTesting::Railtie < ::Rails::Railtie
|
2
|
-
|
2
|
+
config.before_initialize do
|
3
3
|
ActiveSupport.on_load(:action_dispatch_integration_test) do
|
4
4
|
ANONYMOUS_PATH_MUTEX = Mutex.new
|
5
5
|
|
@@ -11,19 +11,14 @@ class RailsAnonymousControllerTesting::Railtie < ::Rails::Railtie
|
|
11
11
|
@_anonymous_view_base_path = value
|
12
12
|
end
|
13
13
|
|
14
|
-
def self._anonymous_view_path
|
15
|
-
if instance_variable_defined?(:@_anonymous_view_path)
|
16
|
-
return @_anonymous_view_path
|
17
|
-
end
|
18
|
-
|
19
|
-
caller_path = caller_locations(1, 10).find { |location| location.absolute_path != __FILE__ }.absolute_path
|
20
|
-
@_anonymous_view_path = _anonymous_view_base_path.join(Digest::MD5.file(caller_path).hexdigest)
|
21
|
-
end
|
22
|
-
|
23
14
|
def self._anonymous_controller_name
|
24
15
|
@_anonymous_controller_name ||= "AnonymousController"
|
25
16
|
end
|
26
17
|
|
18
|
+
def self._anonymous_controller_name=(value)
|
19
|
+
@_anonymous_controller_name = value
|
20
|
+
end
|
21
|
+
|
27
22
|
def self._anonymous_view_cache?
|
28
23
|
if instance_variable_defined?(:@_anonymous_view_cache)
|
29
24
|
@_anonymous_view_cache
|
@@ -48,8 +43,28 @@ class RailsAnonymousControllerTesting::Railtie < ::Rails::Railtie
|
|
48
43
|
@_anonymous_views = value
|
49
44
|
end
|
50
45
|
|
51
|
-
def self.controller(base_controller, routes: nil, &block)
|
52
|
-
|
46
|
+
def self.controller(base_controller, routes: nil, controller_name: "AnonymousController", &block)
|
47
|
+
caller_location = caller_locations(1, 10).find { |location| location.absolute_path != __FILE__ }
|
48
|
+
|
49
|
+
display_name =
|
50
|
+
if caller_location
|
51
|
+
"#{caller_location.absolute_path.split("/").last.split(".").first}_#{caller_location.lineno}"
|
52
|
+
else
|
53
|
+
"path_unknown"
|
54
|
+
end
|
55
|
+
|
56
|
+
unique_identifier =
|
57
|
+
if caller_location
|
58
|
+
Digest::MD5
|
59
|
+
.file(caller_location.absolute_path)
|
60
|
+
.tap { |d| d << caller_location.absolute_path }
|
61
|
+
.tap { |d| d << caller_location.lineno.to_s }
|
62
|
+
else
|
63
|
+
SecureRandom.hex
|
64
|
+
end
|
65
|
+
|
66
|
+
anonymous_view_path = _anonymous_view_base_path.join("#{display_name}_#{unique_identifier}")
|
67
|
+
self._anonymous_controller_name = controller_name
|
53
68
|
anonymous_controller_name = _anonymous_controller_name
|
54
69
|
|
55
70
|
# Define the controller
|
@@ -66,6 +81,8 @@ class RailsAnonymousControllerTesting::Railtie < ::Rails::Railtie
|
|
66
81
|
const_set(anonymous_controller_name, anonymous_controller_class)
|
67
82
|
|
68
83
|
setup do
|
84
|
+
Rails.application.reload_routes!
|
85
|
+
|
69
86
|
# Set up the routes
|
70
87
|
if !routes
|
71
88
|
resource_name = anonymous_controller_class.controller_name.to_sym
|
@@ -76,7 +93,6 @@ class RailsAnonymousControllerTesting::Railtie < ::Rails::Railtie
|
|
76
93
|
end
|
77
94
|
end
|
78
95
|
|
79
|
-
# Rails.application.routes.draw(&routes)
|
80
96
|
Rails.application.routes.send(:eval_block, routes)
|
81
97
|
|
82
98
|
# Set up the views
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_anonymous_controller_testing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zach Ahn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: actionpack
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 5.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 5.0.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 5.0.0
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: sqlite3
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|