rspec_controller_helpers 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rspec_controller_helpers/resourceful.rb +76 -25
- 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: bd32a83c2aef3899917f4cd11337cbc64f6e8b9a
|
4
|
+
data.tar.gz: da04fb1ec5fd120de6fe43b3e788521e3ff61fd8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a8464d1920a23fceb2429250f5b7cecfe48c8154107def64754596620bde9524f1a0dbc7432edd4d0e76405a7d02a4c54c33750d0628f377577fe89bd1041d25
|
7
|
+
data.tar.gz: 2cdc8dd41c6eb863ed2c094f530bb8fbfe60d9f1e745a0f1c6879e91198ecdbe77f7dc676ee6ff95e14ab45210ba6f6d58149edc57dac84ea83691bc89856475
|
@@ -1,23 +1,45 @@
|
|
1
1
|
RSpec.shared_examples_for 'a resourceful controller' do |model, raw_options|
|
2
|
-
let(:
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
except = options[:except] || []
|
9
|
-
options[:only] || (all - except)
|
2
|
+
let(:default_options) do
|
3
|
+
{
|
4
|
+
only: [:index, :new, :create, :show, :edit, :update, :delete],
|
5
|
+
except: [],
|
6
|
+
formats: {} # They all default to :html
|
7
|
+
}
|
10
8
|
end
|
9
|
+
let(:options) { default_options.merge(raw_options) }
|
10
|
+
let(:model_name) { options[:model_name] || model.name.downcase.to_sym }
|
11
|
+
let(:factory) { options[:factory] || model_name }
|
12
|
+
let(:endpoints) { options[:only] - options[:except] }
|
13
|
+
|
14
|
+
let(:formats) do
|
15
|
+
endpoints
|
16
|
+
.map { |t| { t => [:html] } }
|
17
|
+
.reduce(:merge)
|
18
|
+
.merge(options[:formats])
|
19
|
+
end
|
20
|
+
|
21
|
+
let(:json_response) { JSON.parse(response.body) }
|
11
22
|
|
12
23
|
describe "GET /<resource>" do
|
13
24
|
let!(:objects) { create_list(factory, 3) }
|
14
25
|
subject { get :index }
|
15
26
|
|
16
|
-
it
|
17
|
-
if
|
27
|
+
it 'is successful' do
|
28
|
+
if endpoints.include? :index
|
18
29
|
subject
|
19
30
|
expect(response).to have_http_status(:ok)
|
20
|
-
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it "renders the index page" do
|
35
|
+
if endpoints.include? :index
|
36
|
+
subject
|
37
|
+
if formats[:index].include? :html
|
38
|
+
expect(response).to render_template(:index)
|
39
|
+
end
|
40
|
+
if formats[:index].include? :json
|
41
|
+
expect(json_response.length).to eq(3)
|
42
|
+
end
|
21
43
|
end
|
22
44
|
end
|
23
45
|
end
|
@@ -25,21 +47,29 @@ RSpec.shared_examples_for 'a resourceful controller' do |model, raw_options|
|
|
25
47
|
describe "GET /<resource>/new" do
|
26
48
|
subject { get :new }
|
27
49
|
|
28
|
-
it
|
29
|
-
if
|
50
|
+
it 'is successful' do
|
51
|
+
if endpoints.include? :new
|
30
52
|
subject
|
31
53
|
expect(response).to have_http_status(:ok)
|
32
|
-
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it "renders the new page" do
|
58
|
+
if endpoints.include? :new
|
59
|
+
subject
|
60
|
+
if formats[:new].include? :html
|
61
|
+
expect(response).to render_template(:new)
|
62
|
+
end
|
33
63
|
end
|
34
64
|
end
|
35
65
|
end
|
36
66
|
|
37
67
|
describe "POST /<resource>" do
|
38
68
|
let(:attributes) { attributes_for(factory) }
|
39
|
-
subject { post :create,
|
69
|
+
subject { post :create, model_name => attributes }
|
40
70
|
|
41
71
|
it "creates a new object" do
|
42
|
-
if
|
72
|
+
if endpoints.include? :create
|
43
73
|
expect { subject }.to change { model.count }.by(1)
|
44
74
|
end
|
45
75
|
end
|
@@ -49,11 +79,24 @@ RSpec.shared_examples_for 'a resourceful controller' do |model, raw_options|
|
|
49
79
|
let(:object) { create(factory) }
|
50
80
|
subject { get :show, id: object.id }
|
51
81
|
|
52
|
-
it
|
53
|
-
if
|
82
|
+
it 'is successful' do
|
83
|
+
if endpoints.include? :show
|
54
84
|
subject
|
55
85
|
expect(response).to have_http_status(:ok)
|
56
|
-
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
it "renders the show page" do
|
90
|
+
if endpoints.include? :show
|
91
|
+
subject
|
92
|
+
if formats[:show].include? :html
|
93
|
+
expect(response).to render_template(:show)
|
94
|
+
end
|
95
|
+
if formats[:show].include? :json
|
96
|
+
desired_attributes = attributes_for(factory).keys
|
97
|
+
attributes = object.attributes.select { |k, _| desired_attributes.include?(k) }
|
98
|
+
expect(json_response).to eq(attributes)
|
99
|
+
end
|
57
100
|
end
|
58
101
|
end
|
59
102
|
end
|
@@ -62,11 +105,19 @@ RSpec.shared_examples_for 'a resourceful controller' do |model, raw_options|
|
|
62
105
|
let(:object) { create(factory) }
|
63
106
|
subject { get :edit, id: object.id }
|
64
107
|
|
65
|
-
it
|
66
|
-
if
|
108
|
+
it 'is successful' do
|
109
|
+
if endpoints.include? :edit
|
67
110
|
subject
|
68
111
|
expect(response).to have_http_status(:ok)
|
69
|
-
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
it "renders the edit page" do
|
116
|
+
if endpoints.include? :edit
|
117
|
+
subject
|
118
|
+
if formats[:edit].include? :html
|
119
|
+
expect(response).to render_template(:edit)
|
120
|
+
end
|
70
121
|
end
|
71
122
|
end
|
72
123
|
end
|
@@ -74,10 +125,10 @@ RSpec.shared_examples_for 'a resourceful controller' do |model, raw_options|
|
|
74
125
|
describe "PATCH /<resource>/:id" do
|
75
126
|
let(:object) { create(factory) }
|
76
127
|
let(:new_attributes) { attributes_for(factory) }
|
77
|
-
subject { patch :update, id: object.id,
|
128
|
+
subject { patch :update, id: object.id, model_name => new_attributes }
|
78
129
|
|
79
130
|
it "updates the object" do
|
80
|
-
if
|
131
|
+
if endpoints.include? :update
|
81
132
|
subject
|
82
133
|
object.reload
|
83
134
|
new_attributes.each do |k, v|
|
@@ -92,7 +143,7 @@ RSpec.shared_examples_for 'a resourceful controller' do |model, raw_options|
|
|
92
143
|
subject { delete :destroy, id: object.id }
|
93
144
|
|
94
145
|
it "deletes the object" do
|
95
|
-
if
|
146
|
+
if endpoints.include? :destroy
|
96
147
|
expect { subject }.to change { model.count }.by(-1)
|
97
148
|
end
|
98
149
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec_controller_helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Derek Kniffin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec-rails
|