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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b816731a16523117cdac6f32b731ff71bed46b02
4
- data.tar.gz: 4541661168a86e51362be90c193da3f0eeb765d2
3
+ metadata.gz: bd32a83c2aef3899917f4cd11337cbc64f6e8b9a
4
+ data.tar.gz: da04fb1ec5fd120de6fe43b3e788521e3ff61fd8
5
5
  SHA512:
6
- metadata.gz: 2e7316be23023c08cd48b15681708dab43c40d6c8bcdca49075d4cfb447cd4936a1818d607e9dde1e64b2688d1893531d71103d8e36d95cc9bc99014573b8eb3
7
- data.tar.gz: 6b10f3bf98a8dc33731e951e09e1bca23c31f29b0c0e856452964f7a26d46bc7faeeda4f854ee28136ab51913f548b350f03b3b83458315062066d9e8b4dd94b
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(:options) { raw_options || {} }
3
- let(:symbol) { options[:symbol] || model.name.downcase.to_sym }
4
- let(:factory) { options[:factory] || symbol }
5
-
6
- let(:tests) do
7
- all = [:index, :new, :create, :show, :edit, :update, :delete]
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 "renders the index page" do
17
- if tests.include? :index
27
+ it 'is successful' do
28
+ if endpoints.include? :index
18
29
  subject
19
30
  expect(response).to have_http_status(:ok)
20
- expect(response).to render_template(:index)
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 "renders the new page" do
29
- if tests.include? :new
50
+ it 'is successful' do
51
+ if endpoints.include? :new
30
52
  subject
31
53
  expect(response).to have_http_status(:ok)
32
- expect(response).to render_template(:new)
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, symbol => attributes }
69
+ subject { post :create, model_name => attributes }
40
70
 
41
71
  it "creates a new object" do
42
- if tests.include? :create
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 "renders the show page" do
53
- if tests.include? :show
82
+ it 'is successful' do
83
+ if endpoints.include? :show
54
84
  subject
55
85
  expect(response).to have_http_status(:ok)
56
- expect(response).to render_template(:show)
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 "renders the edit page" do
66
- if tests.include? :edit
108
+ it 'is successful' do
109
+ if endpoints.include? :edit
67
110
  subject
68
111
  expect(response).to have_http_status(:ok)
69
- expect(response).to render_template(:edit)
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, symbol => new_attributes }
128
+ subject { patch :update, id: object.id, model_name => new_attributes }
78
129
 
79
130
  it "updates the object" do
80
- if tests.include? :update
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 tests.include? :destroy
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.3
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-19 00:00:00.000000000 Z
11
+ date: 2016-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec-rails