rails_anonymous_controller_testing 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0ff3511ad88aa4643e6fe05a29c518683deab8f4cd8d44489a57a606afb1695d
4
+ data.tar.gz: 40d7fd0fd19d8170aaeeddadf8fc2738a591c9410182e5248204229b20862a64
5
+ SHA512:
6
+ metadata.gz: dbc4d3ef652b96729dfe1b4767d1c3167b8f8ca724d7921b764c29d9a6aa663a64230e5e604f00e310e11cb1fb84b1e5d6a887e7219e8e9fcd67419f8e2167a0
7
+ data.tar.gz: 5cea00b2b62b53c67ab3deb7d6e6c3d061d510c1a46ec6e324cdcca6be06821edd25483bbc27cfbe81321de8d626ebf9c5e4322342ed78c2a5157a2734082ba6
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 Zach Ahn
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # Rails Anonymous Controller Testing
2
+
3
+ I sometimes want to test an abstract controller, a controller concern, a view,
4
+ or a helper method.
5
+
6
+ This might be useful for you if you're working on a Rails engine or if you're
7
+ editing your `ApplicationController`.
8
+
9
+ This only works with Minitest, specifically for tests that inherit from
10
+ `ActionDispatch::IntegrationTest`.
11
+
12
+
13
+ ## Usage
14
+
15
+ ```ruby
16
+ class MyControllerTest < ActionDispatch::IntegrationTest
17
+ # 1. Call the `controller` method. You must specify the base controller to
18
+ # inherit from. It'll set up routes with `resources :anonymous`.
19
+ controller(ApplicationController) do
20
+ def index
21
+ end
22
+
23
+ def show
24
+ render plain: params[:id]
25
+ end
26
+ end
27
+
28
+ # 2. Optionally, set views. There are some caveats, see below
29
+ views["layouts/application.html.erb"] = <<~HTML
30
+ <h1>My anonymous test</h1>
31
+ <%= yield %>
32
+ HTML
33
+
34
+ views["index.html.erb"] = <<~HTML
35
+ <h2>Hi</h2>
36
+ HTML
37
+
38
+ # 3. Test like it's a regular controller in your application
39
+ def test_index
40
+ get "/anonymous"
41
+ assert_select "h1", "My anonymous test"
42
+ assert_select "h2", "Hi"
43
+ end
44
+
45
+ def test_show
46
+ get "/anonymous/1234"
47
+ assert_equal "1234", response.body
48
+ end
49
+ end
50
+ ```
51
+
52
+ You can also specify the routes too, but you need to namespace the controller to
53
+ the name of the test class.
54
+
55
+ ```ruby
56
+ class MyControllerTest < ActionDispatch::IntegrationTest
57
+ routes = -> { get "custom", to: "my_controller_test/anonymous#custom" }
58
+ controller(ApplicationController, routes: routes) do
59
+ def custom
60
+ end
61
+ end
62
+ end
63
+ ```
64
+
65
+
66
+ ### Views
67
+
68
+ A few things to note about views:
69
+
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.
78
+
79
+
80
+ ## Installation
81
+
82
+ Add this line to your application's Gemfile:
83
+
84
+ ```ruby
85
+ gem "rails_anonymous_controller_testing", group: :test
86
+ ```
87
+
88
+ And then execute:
89
+
90
+ ```bash
91
+ $ bundle
92
+ ```
93
+
94
+
95
+ ## Contributing
96
+
97
+ Contributions very welcome. Please give me edit access to your branch for a
98
+ faster turnaround.
99
+
100
+
101
+ ## License
102
+
103
+ The gem is available as open source under the terms of the
104
+ [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,5 @@
1
+ require "digest/md5"
2
+ require "thread"
3
+
4
+ require "rails_anonymous_controller_testing/version"
5
+ require "rails_anonymous_controller_testing/railtie"
@@ -0,0 +1,109 @@
1
+ class RailsAnonymousControllerTesting::Railtie < ::Rails::Railtie
2
+ initializer "rails_anonymous_controller_testing" do
3
+ ActiveSupport.on_load(:action_dispatch_integration_test) do
4
+ ANONYMOUS_PATH_MUTEX = Mutex.new
5
+
6
+ def self._anonymous_view_base_path
7
+ @_anonymous_view_base_path ||= Rails.root.join("tmp", "anonymous_controller_views")
8
+ end
9
+
10
+ def self.anonymous_view_base_path=(value)
11
+ @_anonymous_view_base_path = value
12
+ end
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
+ def self._anonymous_controller_name
24
+ @_anonymous_controller_name ||= "AnonymousController"
25
+ end
26
+
27
+ def self._anonymous_view_cache?
28
+ if instance_variable_defined?(:@_anonymous_view_cache)
29
+ @_anonymous_view_cache
30
+ end
31
+
32
+ @_anonymous_view_cache = true
33
+ end
34
+
35
+ def self.disable_anonymous_view_cache!
36
+ @_anonymous_view_cache = false
37
+ end
38
+
39
+ def self.enable_anonymous_view_cache!
40
+ @_anonymous_view_cache = true
41
+ end
42
+
43
+ def self.views
44
+ @_anonymous_views ||= {}
45
+ end
46
+
47
+ def self.views=(value)
48
+ @_anonymous_views = value
49
+ end
50
+
51
+ def self.controller(base_controller, routes: nil, &block)
52
+ anonymous_view_path = _anonymous_view_path
53
+ anonymous_controller_name = _anonymous_controller_name
54
+
55
+ # Define the controller
56
+ anonymous_controller_class = Class.new(base_controller) do
57
+ prepend_view_path(anonymous_view_path)
58
+
59
+ define_singleton_method(:name) do
60
+ anonymous_controller_name
61
+ end
62
+ end
63
+ anonymous_controller_class.class_exec(&block)
64
+
65
+ # Attach the controller to the test class
66
+ const_set(anonymous_controller_name, anonymous_controller_class)
67
+
68
+ setup do
69
+ # Set up the routes
70
+ if !routes
71
+ resource_name = anonymous_controller_class.controller_name.to_sym
72
+ resource_module = self.class.name.underscore
73
+
74
+ routes = proc do
75
+ resources(resource_name, module: resource_module)
76
+ end
77
+ end
78
+
79
+ # Rails.application.routes.draw(&routes)
80
+ Rails.application.routes.send(:eval_block, routes)
81
+
82
+ # Set up the views
83
+ ANONYMOUS_PATH_MUTEX.synchronize do
84
+ next if self.class.views.empty?
85
+ next if self.class._anonymous_view_cache? && anonymous_view_path.exist?
86
+ anonymous_view_path.mkpath
87
+
88
+ self.class.views.each do |basename, contents|
89
+ viewpath = anonymous_view_path.join(basename)
90
+ if viewpath.dirname == anonymous_view_path
91
+ viewpath = anonymous_view_path.join(anonymous_controller_class.controller_name, basename)
92
+ end
93
+
94
+ next if self.class._anonymous_view_cache? && viewpath.exist?
95
+
96
+ viewpath.dirname.mkpath
97
+ viewpath.write(contents)
98
+ end
99
+ end
100
+ end
101
+
102
+ teardown do
103
+ # Reset the routes to its original state
104
+ Rails.application.reload_routes!
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,3 @@
1
+ module RailsAnonymousControllerTesting
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_anonymous_controller_testing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Zach Ahn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-08-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Rails test helpers for testing anonymous controllers (Minitest only!)
56
+ email:
57
+ - engineering@zachahn.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE
63
+ - README.md
64
+ - lib/rails_anonymous_controller_testing.rb
65
+ - lib/rails_anonymous_controller_testing/railtie.rb
66
+ - lib/rails_anonymous_controller_testing/version.rb
67
+ homepage: https://github.com/zachahn/rails_anonymous_controller_testing
68
+ licenses:
69
+ - MIT
70
+ metadata:
71
+ homepage_uri: https://github.com/zachahn/rails_anonymous_controller_testing
72
+ source_code_uri: https://github.com/zachahn/rails_anonymous_controller_testing
73
+ changelog_uri: https://github.com/zachahn/rails_anonymous_controller_testing/blob/main/CHANGELOG.md
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubygems_version: 3.2.15
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Rails test helpers for testing anonymous controllers (Minitest only!)
93
+ test_files: []