ctrlspecs 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +95 -0
- data/Rakefile +2 -0
- data/ctrlspecs.gemspec +25 -0
- data/lib/ctrlspecs.rb +29 -0
- data/lib/ctrlspecs/version.rb +3 -0
- data/lib/spec/support/shared/contexts.rb +7 -0
- data/lib/spec/support/shared/examples.rb +21 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f97741e9ed5721be6e3137a3c199dcf9673c4edf
|
4
|
+
data.tar.gz: b7731968b9018bb6b8b0f3f3e15a1adf1614c98d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 85840d0fa5bdd15458a5447267e5f267401ddaa5ee0d1103463718a1421656f39832a2f63903ae8061c42240778a73851327c56d3628a88a20d6cb606942a4e8
|
7
|
+
data.tar.gz: 206b9b0141051d05c4b8e5790bcbc643adc420c8d68ef581ef785c5379197aca8bf637f4d3a3b3a06e7997adee1125e4f5bc417cf01c0ce9def513c6e475862c
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Mateusz Stebnicki
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
# Ctrlspecs
|
2
|
+
What's the responsibility of a controller?
|
3
|
+
|
4
|
+
Render. Redirect. Flash.
|
5
|
+
|
6
|
+
I'd argue that those are, in fact, the only responsibilities controllers have.
|
7
|
+
Now, I realize controllers' actions are often burdened with resource creation, deletion and updating,
|
8
|
+
but let's keep it real here - all this logic belongs to another object, usually a service.
|
9
|
+
And indeed, most of the non-trivial, well designed applications use service objects,
|
10
|
+
instead of cramming everything inside poor controllers.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'ctrlspecs'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install ctrlspecs
|
26
|
+
|
27
|
+
|
28
|
+
In your rails_helper.rb add:
|
29
|
+
```ruby
|
30
|
+
RSpec.configure do |config|
|
31
|
+
config.extend Ctrlspecs
|
32
|
+
...
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
Ctrlspecs gives you a few helpers, which are basically just shared examples testing
|
38
|
+
what your typical controller does:
|
39
|
+
* what template does it render
|
40
|
+
* where does it redirect to
|
41
|
+
* what message does it flash
|
42
|
+
* what service does it call
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
subject { post :create, params } # subject is required
|
46
|
+
|
47
|
+
it_renders_template :create
|
48
|
+
it_redirects_to 'resources_path(assigns(:resource))' # the path argument will be evaled
|
49
|
+
it_flashes notice: I18n.t('shared.created')
|
50
|
+
it_calls_service SuchService
|
51
|
+
```
|
52
|
+
|
53
|
+
Ctrlspecs also provides contexts for testing failures:
|
54
|
+
```ruby
|
55
|
+
mock_save_record_failure
|
56
|
+
mock_service_returning_error, VeryService, Response::Error
|
57
|
+
```
|
58
|
+
|
59
|
+
So, how does your typical controller action spec look like now?
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
require 'rails_helper'
|
63
|
+
|
64
|
+
describe PostsController do
|
65
|
+
describe 'POST #create' do
|
66
|
+
let!(:params) do
|
67
|
+
{ post: { content: 'such content, wow' } }
|
68
|
+
end
|
69
|
+
subject { post :create, params }
|
70
|
+
|
71
|
+
context 'success' do
|
72
|
+
it_redirects_to 'post_path(assigns(:post))'
|
73
|
+
it_flashes notice: I18n.t('shared.created', resource: 'Post')
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'failure' do
|
77
|
+
mock_save_record_failure
|
78
|
+
it_renders_template :new
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe 'GET #index' do
|
83
|
+
subject { get :index }
|
84
|
+
it_calls_service Posts::GetAllTheFrigginPosts
|
85
|
+
end
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
## Contributing
|
90
|
+
|
91
|
+
1. Fork it ( https://github.com/mwsteb/ctrlspecs/fork )
|
92
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
93
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
94
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
95
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/ctrlspecs.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ctrlspecs/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ctrlspecs"
|
8
|
+
spec.version = Ctrlspecs::VERSION
|
9
|
+
spec.authors = ["Mateusz Stebnicki"]
|
10
|
+
spec.email = ["mateusz.stebnicki@gmail.pl"]
|
11
|
+
spec.summary = %q{Helpers for testing controller actions}
|
12
|
+
spec.description = %q{so helpers, wow. amaze.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "rspec-rails", "~> 3.2"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
end
|
data/lib/ctrlspecs.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'ctrlspecs/version'
|
2
|
+
require File.dirname(__FILE__) + "/spec/support/shared/examples.rb"
|
3
|
+
require File.dirname(__FILE__) + "/spec/support/shared/contexts.rb"
|
4
|
+
|
5
|
+
module Ctrlspecs
|
6
|
+
def it_renders_template tmpl_name
|
7
|
+
it_behaves_like 'renders template', tmpl_name
|
8
|
+
end
|
9
|
+
|
10
|
+
def it_redirects_to path
|
11
|
+
it_behaves_like 'redirects to', path
|
12
|
+
end
|
13
|
+
|
14
|
+
def it_flashes hsh
|
15
|
+
it_behaves_like 'flashes', hsh
|
16
|
+
end
|
17
|
+
|
18
|
+
def it_calls_service service
|
19
|
+
it_behaves_like 'calls service', service
|
20
|
+
end
|
21
|
+
|
22
|
+
def mock_save_record_failure
|
23
|
+
include_context 'mock save record failure'
|
24
|
+
end
|
25
|
+
|
26
|
+
def mock_service_returning_error service, error
|
27
|
+
include_context 'mock service returning error', service, error
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
shared_context 'mock save record failure' do
|
2
|
+
before { allow_any_instance_of(ActiveRecord::Base).to receive(:save) { false } }
|
3
|
+
end
|
4
|
+
|
5
|
+
shared_context 'mock service returning error' do |service, error|
|
6
|
+
before { allow_any_instance_of(service).to receive(:call) { error } }
|
7
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
shared_examples 'renders template' do |template|
|
2
|
+
it { is_expected.to render_template template }
|
3
|
+
end
|
4
|
+
|
5
|
+
shared_examples 'redirects to' do |path|
|
6
|
+
it { is_expected.to redirect_to eval(path) }
|
7
|
+
end
|
8
|
+
|
9
|
+
shared_examples 'flashes' do |hsh|
|
10
|
+
specify do
|
11
|
+
subject
|
12
|
+
expect(flash[hsh.keys.first]).to eq hsh.values.first
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
shared_examples 'calls service' do |service|
|
17
|
+
specify do
|
18
|
+
expect_any_instance_of(service).to receive(:call)
|
19
|
+
subject
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ctrlspecs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mateusz Stebnicki
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec-rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.7'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.7'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: so helpers, wow. amaze.
|
56
|
+
email:
|
57
|
+
- mateusz.stebnicki@gmail.pl
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- ctrlspecs.gemspec
|
68
|
+
- lib/ctrlspecs.rb
|
69
|
+
- lib/ctrlspecs/version.rb
|
70
|
+
- lib/spec/support/shared/contexts.rb
|
71
|
+
- lib/spec/support/shared/examples.rb
|
72
|
+
homepage: ''
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.4.3
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Helpers for testing controller actions
|
96
|
+
test_files: []
|