controller-testing-kwargs 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +27 -1
- data/lib/controller/testing/kwargs.rb +55 -2
- data/lib/controller/testing/kwargs/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d73c4de37761656efc91f3e08bb894ed62a009da
|
4
|
+
data.tar.gz: f9a4c5810fba29f702bd104c094090f7be4cda49
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61329363599e362de4b82e85e47aea47029e6fc9eef263e074c973e6c4f85f5352724cf09ecb3eab32bbc9aac224ca08d02b4dfa132581aeada636b49429a0c5
|
7
|
+
data.tar.gz: 1bd91c023b29ec0d0da0e41222866dd44a5e343b75015115239601c7bd96026b651c576d7346bc4e161d0a52eea6d7d57f55d1386f9a5a4cbce4da3708112db8
|
data/README.md
CHANGED
@@ -7,7 +7,7 @@ Backport Rails 5 style controller/integration testing syntax using kwargs to Rai
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem 'controller-testing-kwargs'
|
10
|
+
gem 'controller-testing-kwargs', require: false
|
11
11
|
```
|
12
12
|
|
13
13
|
And then execute:
|
@@ -18,6 +18,12 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
$ gem install controller-testing-kwargs
|
20
20
|
|
21
|
+
At the appropriate spot in your `test_helper.rb`, `spec_helper.rb`, or similar file add the following line:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'controller/testing/kwargs'
|
25
|
+
```
|
26
|
+
|
21
27
|
## Usage
|
22
28
|
|
23
29
|
Allows you to simultaneously use the old and new syntax while making HTTP calls to your controllers
|
@@ -37,6 +43,26 @@ get #{url_or_action}, xhr: true, params: params, headers: headers
|
|
37
43
|
|
38
44
|
should work while you transition your test suite.
|
39
45
|
|
46
|
+
## Modes of Operation
|
47
|
+
|
48
|
+
Deprecation warnings will appear by default when executing statements that
|
49
|
+
utilize the old method. Deprecation warnings can be disabled by adding the
|
50
|
+
following to your appropriate test helper:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
Controller::Testing::Kwargs.ignore
|
54
|
+
```
|
55
|
+
|
56
|
+
The above is useful if you simply want to support the Rails 5 syntax. If
|
57
|
+
instead you want to prevent new uses of the old syntax, add the following:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
Controller::Testing::Kwargs.raise_exception
|
61
|
+
```
|
62
|
+
|
63
|
+
The above is useful when you're done coverting the syntax but are not yet ready
|
64
|
+
to make the switch to Rails 5.
|
65
|
+
|
40
66
|
## Development
|
41
67
|
|
42
68
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -4,12 +4,43 @@ require "rails/test_help"
|
|
4
4
|
module Controller
|
5
5
|
module Testing
|
6
6
|
module Kwargs
|
7
|
+
ERROR_MESSAGE = 'Please use Rails 5 syntax. See: https://github.com/appfolio/controller-testing-kwargs'
|
8
|
+
|
9
|
+
class <<self
|
10
|
+
@on_old = :deprecate
|
11
|
+
|
12
|
+
attr_reader :on_old
|
13
|
+
|
14
|
+
def deprecate
|
15
|
+
@on_old = :warn
|
16
|
+
end
|
17
|
+
|
18
|
+
def deprecated?
|
19
|
+
@on_old == :warn
|
20
|
+
end
|
21
|
+
|
22
|
+
def ignore
|
23
|
+
@on_old = :ignore
|
24
|
+
end
|
25
|
+
|
26
|
+
def raise_exception
|
27
|
+
@on_old = :raise
|
28
|
+
end
|
29
|
+
|
30
|
+
def raise_exception?
|
31
|
+
@on_old == :raise
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
7
35
|
%i[get post patch put head delete].each do |method|
|
8
36
|
define_method(method) do |action, *args|
|
9
37
|
request_params = args[0]&.dup
|
10
38
|
request_headers = args[1]&.dup
|
39
|
+
|
40
|
+
old_method = false
|
41
|
+
xhr = false
|
11
42
|
if request_params && request_params.is_a?(Hash)
|
12
|
-
xhr = request_params.delete(:xhr)
|
43
|
+
xhr = request_params.delete(:xhr)
|
13
44
|
if request_params[:params].is_a?(Hash)
|
14
45
|
request_params.merge!(request_params.delete(:params) || {})
|
15
46
|
request_headers = request_params.delete(:headers) || request_headers
|
@@ -19,12 +50,34 @@ module Controller
|
|
19
50
|
elsif request_params[:headers]
|
20
51
|
request_headers = request_params[:headers]
|
21
52
|
request_params = nil
|
53
|
+
else
|
54
|
+
old_method = true
|
55
|
+
end
|
56
|
+
elsif request_headers.is_a?(Hash)
|
57
|
+
old_method = true
|
58
|
+
end
|
59
|
+
|
60
|
+
raise Exception, ERROR_MESSAGE if Controller::Testing::Kwargs.raise_exception? && old_method
|
61
|
+
|
62
|
+
if xhr
|
63
|
+
previous_value = Controller::Testing::Kwargs.on_old
|
64
|
+
Controller::Testing::Kwargs.ignore
|
65
|
+
begin
|
66
|
+
return xhr(method, action, request_params, request_headers)
|
67
|
+
ensure
|
68
|
+
Controller::Testing::Kwargs.instance_variable_set(:@on_old, previous_value)
|
22
69
|
end
|
23
|
-
return xhr(method, action, request_params, request_headers) if xhr
|
24
70
|
end
|
71
|
+
ActiveSupport::Deprecation.warn(ERROR_MESSAGE) if Controller::Testing::Kwargs.deprecated?
|
25
72
|
super(action, request_params, request_headers)
|
26
73
|
end
|
27
74
|
end
|
75
|
+
|
76
|
+
def xhr(request_method, action, parameters = nil, *args)
|
77
|
+
raise Exception ERROR_MESSAGE if Controller::Testing::Kwargs.raise_exception?
|
78
|
+
ActiveSupport::Deprecation.warn(ERROR_MESSAGE) if Controller::Testing::Kwargs.deprecated?
|
79
|
+
super(request_method, action, parameters, *args)
|
80
|
+
end
|
28
81
|
end
|
29
82
|
end
|
30
83
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: controller-testing-kwargs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chad Shaffer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-08-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|