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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0ff3511ad88aa4643e6fe05a29c518683deab8f4cd8d44489a57a606afb1695d
4
- data.tar.gz: 40d7fd0fd19d8170aaeeddadf8fc2738a591c9410182e5248204229b20862a64
3
+ metadata.gz: 806ed4f101b16a4baade5b23b7af947809fa5f7e2d070001437ae44b5b2d4dc7
4
+ data.tar.gz: 5461ffdf99fd003cb1e5e345e6a127cfaa0f96c4f7f655dcf7518a1934c584ba
5
5
  SHA512:
6
- metadata.gz: dbc4d3ef652b96729dfe1b4767d1c3167b8f8ca724d7921b764c29d9a6aa663a64230e5e604f00e310e11cb1fb84b1e5d6a887e7219e8e9fcd67419f8e2167a0
7
- data.tar.gz: 5cea00b2b62b53c67ab3deb7d6e6c3d061d510c1a46ec6e324cdcca6be06821edd25483bbc27cfbe81321de8d626ebf9c5e4322342ed78c2a5157a2734082ba6
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. There are some caveats, see below
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
- ### Views
70
+ ### Caveats
67
71
 
68
- A few things to note about views:
72
+ A few things to note
69
73
 
70
- * Views are generated only once and cached. This is determined by the MD5 hash
71
- of the test **file** you defined the **controller**. You can disable caching
72
- by calling `disable_anonymous_view_cache!` in your Rails test. The view cache
73
- is located in `tmp/anonymous_controller_views`.
74
- * If you override the layout for your anonymous controller, you'll also override
75
- the layout for your other controllers. This is because this library uses
76
- `prepend_view_path`, so the views defined in the test take precedence over the
77
- other view directories.
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
- initializer "rails_anonymous_controller_testing" do
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
- anonymous_view_path = _anonymous_view_path
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
@@ -1,3 +1,3 @@
1
1
  module RailsAnonymousControllerTesting
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.5"
3
3
  end
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.1
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-08-01 00:00:00.000000000 Z
11
+ date: 2021-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rails
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