rails_anonymous_controller_testing 0.0.2 → 0.0.3

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: cc85a760430231b13db61ed3f20e5aa47c30323cc84c5409344e4c7594c96a1c
4
- data.tar.gz: 4b14dd564d41bef6f77597f3ce2f9a3da1873513a7a5e10c18f21ec3459df0f5
3
+ metadata.gz: ae4209c78f5fbe04812401242ebc41f448005547b3a99f95e4100a6c86ebc33d
4
+ data.tar.gz: 3ccbd4f88dc6ebd29ac0ab69f3875467949896df125941d386e9ceaa373be7f1
5
5
  SHA512:
6
- metadata.gz: 9abece243a2654e95fcf67ef6df0f806bbced2764077048850f8e53586eb9deb7cef735f5aef3c1c31c24b0c3e9cac5ef62d778394a65e7a28556e28d2ed8274
7
- data.tar.gz: 53a7682c51375d2d172fb591c87d100cd967a3179a69d7c983cc9f029e7ed3db7d327585b5d36c7fc2fc31e793b3aca6785a484a0426a144edb5ae347b34f3ce
6
+ metadata.gz: 0530f6b5890971a25ce0c2f5ffc709542bc99bb02c7e754853f8b16ba1a4df0af46736a392664c19a5d129cacd36bfbdce3970da055ea8634e1b1d2bc08306c5
7
+ data.tar.gz: 7c99f8c676676c219d6e730bcb640967b27da28bbd086ba86dff4833d47c14c16d5c6403274a0bb3b4a40bd1e13787d67b2cde1fda7034b30e8b037864fc7074
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
@@ -11,15 +11,6 @@ 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
@@ -49,7 +40,26 @@ class RailsAnonymousControllerTesting::Railtie < ::Rails::Railtie
49
40
  end
50
41
 
51
42
  def self.controller(base_controller, routes: nil, &block)
52
- anonymous_view_path = _anonymous_view_path
43
+ caller_location = caller_locations(1, 10).find { |location| location.absolute_path != __FILE__ }
44
+
45
+ display_name =
46
+ if caller_location
47
+ "#{caller_location.absolute_path.split("/").last.split(".").first}_#{caller_location.lineno}"
48
+ else
49
+ "path_unknown"
50
+ end
51
+
52
+ unique_identifier =
53
+ if caller_location
54
+ Digest::MD5
55
+ .file(caller_location.absolute_path)
56
+ .tap { |d| d << caller_location.absolute_path }
57
+ .tap { |d| d << caller_location.lineno.to_s }
58
+ else
59
+ SecureRandom.hex
60
+ end
61
+
62
+ anonymous_view_path = _anonymous_view_base_path.join("#{display_name}_#{unique_identifier}")
53
63
  anonymous_controller_name = _anonymous_controller_name
54
64
 
55
65
  # Define the controller
@@ -1,3 +1,3 @@
1
1
  module RailsAnonymousControllerTesting
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
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.2
4
+ version: 0.0.3
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-08-06 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