resource_spec 0.1.0 → 0.2.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 +22 -3
- data/lib/resource_spec/all.rb +1 -0
- data/lib/resource_spec/context.rb +19 -8
- data/lib/resource_spec/create.rb +22 -12
- data/lib/resource_spec/destroy.rb +14 -7
- data/lib/resource_spec/edit.rb +6 -3
- data/lib/resource_spec/new.rb +9 -4
- data/lib/resource_spec/shared.rb +35 -0
- data/lib/resource_spec/update.rb +22 -11
- data/lib/resource_spec/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9bb861e58b40cf18f77d6ce576d3d88f2d43e526
|
4
|
+
data.tar.gz: 12e5208fed9ff5cfd9be32d10429f51990da34e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 459e7463835bb1d49b42316ddb43780dc71e7590cf3ee013b4e7f11a2a2e774f6e16c8bfa497acaac9bfeeedab6b184769cd8b4499f3f2d95e828d486e7dc274
|
7
|
+
data.tar.gz: cc0b200afaa66ae59bb5a0340783c74d03e9d222343807c201f768b0c810ceda91dbf30ef5f682f61a1d5136e0611f6b894b25776d9847ea7d6f9d70a63ca939
|
data/README.md
CHANGED
@@ -1,10 +1,16 @@
|
|
1
1
|
# ResourceSpec
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.org/gzigzigzeo/resource_spec.svg)](http://travis-ci.org/gzigzigzeo/resource_spec/fias)
|
4
|
+
|
5
|
+
<a href="https://evilmartians.com/?utm_source=resource_spec-gem">
|
6
|
+
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54">
|
7
|
+
</a>
|
8
|
+
|
3
9
|
Test your RESTful Rails controllers with ease.
|
4
10
|
|
5
11
|
### Motivation
|
6
12
|
|
7
|
-
In every
|
13
|
+
In every Rails app we have a set of controllers which encapsulates simple CRUD logic, especially in the admin area. Most of such controllers stays uncovered: it often seems excessive to test typical things. In other hand, in such typical places there always exists some unusual logic, which had to be tested. Furthermore, 100% test coverage rocks.
|
8
14
|
|
9
15
|
## Simple Use Case
|
10
16
|
|
@@ -23,7 +29,7 @@ include_context "ResourceSpec", User do
|
|
23
29
|
end
|
24
30
|
```
|
25
31
|
|
26
|
-
`FactoryGirl` factory for `User` must be defined. Calls to `FactoryGirl` or other factory engine can be
|
32
|
+
`FactoryGirl` factory for `User` must be defined. Calls to `FactoryGirl` or other factory engine can be overridden with settings.
|
27
33
|
|
28
34
|
## Customization
|
29
35
|
|
@@ -59,7 +65,7 @@ This will make nested resources like `/category/:category_id/groups` work:
|
|
59
65
|
let(:category) { create(:category) }
|
60
66
|
|
61
67
|
include_context "ResourceSpec", Group do
|
62
|
-
let(:
|
68
|
+
let(:base_url_args) { { category_id: category.id } }
|
63
69
|
|
64
70
|
...
|
65
71
|
end
|
@@ -90,6 +96,19 @@ around { |example| travel_to(Time.now, &example) }
|
|
90
96
|
|
91
97
|
If you have Date columns - use `#to_date` in factory.
|
92
98
|
|
99
|
+
### Additional action expectations
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
include_context "ResourceSpec", User do
|
103
|
+
it_behaves_like "GET :new" do
|
104
|
+
it "includes form tag" do
|
105
|
+
expect(response.body).to include("form")
|
106
|
+
end
|
107
|
+
end
|
108
|
+
...
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
93
112
|
## Installation
|
94
113
|
|
95
114
|
Add this line to your application's Gemfile:
|
data/lib/resource_spec/all.rb
CHANGED
@@ -2,35 +2,46 @@ RSpec.shared_context "ResourceSpec" do |model|
|
|
2
2
|
let(:model) { model }
|
3
3
|
|
4
4
|
let(:param_name) { model.model_name.param_key }
|
5
|
-
let(:primary_key_param_name) { :id }
|
6
|
-
let(:factory_name) { model }
|
5
|
+
let(:primary_key_param_name) { :id } # /resource/:id/edit
|
6
|
+
let(:factory_name) { model } # FactoryGirl(Namespaced::Model) fails
|
7
7
|
|
8
8
|
let(:attributes) { FactoryGirl.attributes_for(factory_name).except(:id) }
|
9
|
-
let(:not_expected_params) { [] }
|
10
9
|
let(:params) { attributes }
|
11
|
-
|
10
|
+
|
11
|
+
# Used to skip values changing on record save.
|
12
|
+
#
|
13
|
+
# POST /users/create?user[name]=John&user[password]=5
|
14
|
+
# expect(user).to have_attributes({name: "John"})
|
15
|
+
#
|
16
|
+
# No need to check password: it's encrypted
|
17
|
+
let(:not_expected_params) { [] } # Usually :password, :image, etc.
|
12
18
|
let(:params_to_expect) { params.except(*not_expected_params) }
|
13
19
|
|
20
|
+
# Valid params with blank values by default
|
21
|
+
let(:invalid_params) { Hash[params.keys.zip([""] * params.keys.size)] }
|
22
|
+
|
23
|
+
# Returns active object instance from controller. Could be `controller.resource` if you use decent_exposure.
|
14
24
|
let(:resource) { assigns[param_name] }
|
15
25
|
let(:instance) { FactoryGirl.create(factory_name) }
|
16
26
|
|
27
|
+
# Returns collection, same as :resource
|
17
28
|
let(:collection) { assigns[param_name.to_s.pluralize.to_sym] }
|
18
29
|
|
19
|
-
let(:
|
30
|
+
let(:default_url_args) { {} }
|
20
31
|
|
21
32
|
let(:index_url_args) { {} }
|
22
33
|
|
23
|
-
let(:new_url_args) {
|
34
|
+
let(:new_url_args) { default_url_args.merge(param_name => params) }
|
24
35
|
|
25
36
|
let(:success_resource_url) do
|
26
37
|
controller.url_for(action: :show, primary_key_param_name => resource.id)
|
27
38
|
end
|
28
39
|
|
29
40
|
let(:create_url_args) { new_url_args }
|
30
|
-
let(:invalid_create_url_args) {
|
41
|
+
let(:invalid_create_url_args) { default_url_args.merge(param_name => invalid_params) }
|
31
42
|
let(:success_create_url) { success_resource_url }
|
32
43
|
|
33
|
-
let(:edit_url_args) {
|
44
|
+
let(:edit_url_args) { default_url_args.merge(primary_key_param_name => instance.id) }
|
34
45
|
|
35
46
|
let(:update_url_args) { edit_url_args.merge(new_url_args) }
|
36
47
|
let(:invalid_update_url_args) do
|
data/lib/resource_spec/create.rb
CHANGED
@@ -1,20 +1,30 @@
|
|
1
1
|
RSpec.shared_examples "POST :create" do
|
2
2
|
describe "POST :create" do
|
3
|
-
|
4
|
-
|
3
|
+
include_examples "POST :create, success"
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
RSpec.shared_examples "POST :create, success" do
|
8
|
+
context "success" do
|
9
|
+
let(:url_args) { create_url_args }
|
10
|
+
let(:redirect_url) { success_create_url }
|
5
11
|
|
6
|
-
|
7
|
-
|
8
|
-
|
12
|
+
before { post :create, url_args }
|
13
|
+
|
14
|
+
include_examples "responds 301"
|
15
|
+
include_examples "resource has no errors"
|
16
|
+
include_examples "resource persisted"
|
17
|
+
include_examples "resource has expected attributes"
|
18
|
+
end
|
19
|
+
end
|
9
20
|
|
10
|
-
|
11
|
-
|
21
|
+
RSpec.shared_examples "POST :create, fail" do
|
22
|
+
context "fail" do
|
23
|
+
let(:url_args) { invalid_create_url_args }
|
12
24
|
|
13
|
-
|
14
|
-
post :create, invalid_create_url_args
|
25
|
+
before { post :create, url_args }
|
15
26
|
|
16
|
-
|
17
|
-
|
18
|
-
end
|
27
|
+
include_examples "responds 200"
|
28
|
+
include_examples "resource has errors"
|
19
29
|
end
|
20
30
|
end
|
@@ -1,14 +1,21 @@
|
|
1
1
|
RSpec.shared_examples "DELETE :destroy" do |**kwargs|
|
2
2
|
describe "DELETE :destroy" do
|
3
|
-
|
4
|
-
|
3
|
+
context "success" do
|
4
|
+
let(:url_args) { destroy_url_args }
|
5
|
+
let(:redirect_url) { success_destroy_url }
|
5
6
|
|
6
|
-
|
7
|
+
before { delete :destroy, url_args }
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
include_examples "responds 301"
|
10
|
+
|
11
|
+
it "resource destroyed" do
|
12
|
+
if kwargs[:paranoid]
|
13
|
+
expect(resource.deleted_at).to be_present
|
14
|
+
else
|
15
|
+
expect { resource.reload }.to raise_error(
|
16
|
+
ActiveRecord::RecordNotFound
|
17
|
+
)
|
18
|
+
end
|
12
19
|
end
|
13
20
|
end
|
14
21
|
end
|
data/lib/resource_spec/edit.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
RSpec.shared_examples "GET :edit" do
|
2
2
|
describe "GET :edit" do
|
3
|
-
|
4
|
-
get :edit, edit_url_args
|
3
|
+
let(:url_args) { edit_url_args }
|
5
4
|
|
6
|
-
|
5
|
+
before { get :edit, url_args }
|
6
|
+
|
7
|
+
include_examples "responds 200"
|
8
|
+
|
9
|
+
it "resource loaded" do
|
7
10
|
expect(resource).to eq(instance)
|
8
11
|
end
|
9
12
|
end
|
data/lib/resource_spec/new.rb
CHANGED
@@ -1,11 +1,16 @@
|
|
1
1
|
RSpec.shared_examples "GET :new" do
|
2
2
|
describe "GET :new" do
|
3
|
-
|
4
|
-
get :new, new_url_args
|
3
|
+
let(:url_args) { new_url_args }
|
5
4
|
|
6
|
-
|
5
|
+
before { get :new, url_args }
|
7
6
|
|
8
|
-
|
7
|
+
include_examples "responds 200"
|
8
|
+
|
9
|
+
it "resource exists" do
|
10
|
+
expect(resource).to be_present
|
11
|
+
end
|
12
|
+
|
13
|
+
it "resource is new record" do
|
9
14
|
expect(resource).to be_new_record
|
10
15
|
end
|
11
16
|
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
RSpec.shared_examples "responds 200" do
|
2
|
+
it "responds 200" do
|
3
|
+
expect(response).to be_success
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
RSpec.shared_examples "responds 301" do
|
8
|
+
it "responds 301" do
|
9
|
+
expect(response).to redirect_to(redirect_url)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
RSpec.shared_examples "resource has no errors" do
|
14
|
+
it "resource has no errors" do
|
15
|
+
expect(resource.errors).to be_blank, "Given params: #{url_args} failed with errors: #{resource.errors.full_messages}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
RSpec.shared_examples "resource has errors" do
|
20
|
+
it "resource has errors" do
|
21
|
+
expect(resource.errors).to be_present
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
RSpec.shared_examples "resource persisted" do
|
26
|
+
it "resource persisted" do
|
27
|
+
expect(resource).to be_persisted
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
RSpec.shared_examples "resource has expected attributes" do
|
32
|
+
it "resource has expected attributes" do
|
33
|
+
expect(resource.reload).to have_attributes(params_to_expect)
|
34
|
+
end
|
35
|
+
end
|
data/lib/resource_spec/update.rb
CHANGED
@@ -1,19 +1,30 @@
|
|
1
1
|
RSpec.shared_examples "PUT :update" do
|
2
2
|
describe "PUT :update" do
|
3
|
-
|
4
|
-
|
3
|
+
include_examples "PUT :update, success"
|
4
|
+
include_examples "PUT :update, fail"
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
RSpec.shared_examples "PUT :update, success" do
|
9
|
+
context "success" do
|
10
|
+
let(:url_args) { update_url_args }
|
11
|
+
let(:redirect_url) { success_update_url }
|
5
12
|
|
6
|
-
|
7
|
-
|
13
|
+
before { put :update, url_args }
|
14
|
+
|
15
|
+
include_examples "responds 301"
|
16
|
+
include_examples "resource has no errors"
|
17
|
+
include_examples "resource has expected attributes"
|
18
|
+
end
|
19
|
+
end
|
8
20
|
|
9
|
-
|
10
|
-
|
21
|
+
RSpec.shared_examples "PUT :update, fail" do
|
22
|
+
context "fail" do
|
23
|
+
let(:url_args) { invalid_update_url_args }
|
11
24
|
|
12
|
-
|
13
|
-
put :update, invalid_update_url_args
|
25
|
+
before { post :update, url_args }
|
14
26
|
|
15
|
-
|
16
|
-
|
17
|
-
end
|
27
|
+
include_examples "responds 200"
|
28
|
+
include_examples "resource has errors"
|
18
29
|
end
|
19
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resource_spec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Victor Sokolov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -174,6 +174,7 @@ files:
|
|
174
174
|
- lib/resource_spec/edit.rb
|
175
175
|
- lib/resource_spec/index.rb
|
176
176
|
- lib/resource_spec/new.rb
|
177
|
+
- lib/resource_spec/shared.rb
|
177
178
|
- lib/resource_spec/update.rb
|
178
179
|
- lib/resource_spec/version.rb
|
179
180
|
- resource_spec.gemspec
|