smart_rspec 0.0.2 → 0.1.0
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 +4 -4
- data/README.md +79 -44
- data/lib/smart_rspec/matchers/be_matchers.rb +46 -0
- data/lib/smart_rspec/matchers/other_matchers.rb +16 -0
- data/lib/smart_rspec/matchers.rb +4 -37
- data/lib/smart_rspec/version.rb +1 -1
- data/lib/smart_rspec.rb +4 -3
- data/spec/smart_rspec/matchers_spec.rb +36 -8
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5ff5716dab4e7adc48ce3d9a68f5ac08335c9447
|
4
|
+
data.tar.gz: e00030e125016a66de204b9d106ae68e60d39ab3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e84dac0dff7b0985a9a7925c61444e34bd26854ad5c354809932bfe74111207534dde371f239ed279af497afed89eb41520e4393cdf26cec96848031983c41b8
|
7
|
+
data.tar.gz: 2ff5953423af3958d6fda0586f1794927024569f1f8114fcfc5587a848685d4345510a43474da70e015e0b6e8c9705c493a59f074b46c66d3acab26f55925ad8
|
data/README.md
CHANGED
@@ -20,13 +20,14 @@ Execute:
|
|
20
20
|
$ bundle
|
21
21
|
|
22
22
|
Require the gem at the top of your `spec/rails_helper.rb` (or equivalent):
|
23
|
-
|
23
|
+
|
24
|
+
``` ruby
|
24
25
|
require 'smart_rspec'
|
25
26
|
```
|
26
27
|
|
27
28
|
Then include the SmartRspec module:
|
28
29
|
|
29
|
-
``` ruby
|
30
|
+
``` ruby
|
30
31
|
RSpec.configure do |config|
|
31
32
|
config.include SmartRspec
|
32
33
|
end
|
@@ -39,17 +40,22 @@ end
|
|
39
40
|
* [belongs_to, has_one, has_many](#belongs_to-has_one-has_many)
|
40
41
|
* [fails_validation_of](#fails_validation_of)
|
41
42
|
* [Matchers](#matchers)
|
42
|
-
* [
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
* [
|
52
|
-
|
43
|
+
* ["Be" matchers](#)
|
44
|
+
* [be_ascending](#be_ascending)
|
45
|
+
* [be_a_bad_request](#be_a_bad_request)
|
46
|
+
* [be_a_list_of](#be_a_list_of)
|
47
|
+
* [be_boolean](#be_boolean)
|
48
|
+
* [be_descending](#be_descending)
|
49
|
+
* [be_email](#be_email)
|
50
|
+
* [be_image_url](#be_image_url)
|
51
|
+
* [be_url](#be_url)
|
52
|
+
* ["Have" matchers](#)
|
53
|
+
* [have](#have)
|
54
|
+
* [have_at_least](#have_at_least)
|
55
|
+
* [have_at_most](#have_at_most)
|
56
|
+
* [have_error_on](#have_error_on)
|
57
|
+
* [Other matchers](#)
|
58
|
+
* [include_items](#include_items)
|
53
59
|
|
54
60
|
### Macros
|
55
61
|
|
@@ -61,7 +67,7 @@ It builds specs for model attributes and test its type, enumerated values and de
|
|
61
67
|
``` ruby
|
62
68
|
RSpec.describe User, type: :model do
|
63
69
|
subject { FactoryGirl.build(:user) }
|
64
|
-
|
70
|
+
|
65
71
|
has_attributes :email, type: :String
|
66
72
|
has_attributes :is_admin, type: :Boolean
|
67
73
|
has_attributes :score, type: :Integer, default: 0
|
@@ -75,25 +81,27 @@ It builds specs and test model associations like `belongs_to`, `has_one` and `ha
|
|
75
81
|
``` ruby
|
76
82
|
RSpec.describe User, type: :model do
|
77
83
|
subject { FactoryGirl.build(:user) }
|
78
|
-
|
84
|
+
|
79
85
|
belongs_to :business
|
80
86
|
has_one :project
|
81
87
|
has_many :tasks
|
82
88
|
end
|
83
89
|
```
|
84
90
|
|
85
|
-
#### fails_validation_of
|
91
|
+
#### fails_validation_of
|
86
92
|
|
87
|
-
It builds specs
|
93
|
+
It builds specs and forces model validations to fail, meaning that you will only turn specs into green when you specify the corresponding validation in the model. In order to get a nice semantics it's recommended to use the `fails_validation_of` macro within a "when invalid" context, like:
|
88
94
|
|
89
95
|
``` ruby
|
90
96
|
RSpec.describe User, type: :model do
|
91
97
|
subject { FactoryGirl.build(:user) }
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
98
|
+
|
99
|
+
context 'when invalid' do
|
100
|
+
fails_validation_of :email, presence: true, email: true
|
101
|
+
fails_validation_of :name, length: { maximum: 80 }, uniqueness: true
|
102
|
+
fails_validation_of :username, length: { minimum: 10 }, exclusion: { in: %w(foo bar) }
|
103
|
+
# Other validations...
|
104
|
+
end
|
97
105
|
end
|
98
106
|
```
|
99
107
|
|
@@ -124,72 +132,97 @@ fails_validation_of :foo, format: { with: /foo/, mock: 'bar' }
|
|
124
132
|
|
125
133
|
SmartRspec gathers a collection of custom useful matchers:
|
126
134
|
|
127
|
-
####
|
135
|
+
#### "Be" matchers
|
136
|
+
|
137
|
+
##### be_ascending
|
128
138
|
|
129
139
|
``` ruby
|
130
140
|
it { expect([1, 2, 3, 4]).to be_ascending }
|
131
141
|
it { expect([1, 4, 2, 3]).not_to be_ascending }
|
132
142
|
```
|
133
143
|
|
134
|
-
|
144
|
+
##### be_a_bad_request
|
135
145
|
``` ruby
|
136
|
-
|
137
|
-
|
146
|
+
context 'unauthenticated' do
|
147
|
+
subject { get :profile }
|
148
|
+
it { is_expected.to be_a_bad_request }
|
149
|
+
end
|
150
|
+
|
151
|
+
context 'authenticated' do
|
152
|
+
before { sign_in user }
|
153
|
+
subject { get :profile }
|
154
|
+
it { is_expected.to_not be_a_bad_request }
|
155
|
+
end
|
138
156
|
```
|
139
157
|
|
140
|
-
|
158
|
+
##### be_a_list_of
|
159
|
+
``` ruby
|
160
|
+
it { expect(Foo.fetch_api).to be_a_list_of(Foo)) }
|
161
|
+
```
|
162
|
+
|
163
|
+
##### be_boolean
|
141
164
|
``` ruby
|
142
165
|
it { expect(true).to be_boolean }
|
143
166
|
it { expect('true').not_to be_boolean }
|
144
167
|
```
|
145
168
|
|
146
|
-
|
169
|
+
##### be_descending
|
147
170
|
``` ruby
|
148
|
-
it { expect(
|
149
|
-
it { expect(
|
171
|
+
it { expect([4, 3, 2, 1]).to be_descending }
|
172
|
+
it { expect([1, 2, 3, 4]).not_to be_descending }
|
150
173
|
```
|
151
174
|
|
152
|
-
|
175
|
+
##### be_email
|
153
176
|
``` ruby
|
154
|
-
it { expect('
|
155
|
-
it { expect('
|
177
|
+
it { expect('tiagopog@gmail.com').to be_email }
|
178
|
+
it { expect('tiagopog@gmail').not_to be_email }
|
156
179
|
```
|
157
180
|
|
158
|
-
|
181
|
+
##### be_image_url
|
159
182
|
``` ruby
|
160
183
|
it { expect('http://adtangerine.com/foobar.png').to be_image_url }
|
161
184
|
it { expect('http://adtangerine.com/foobar.jpg').not_to be_image_url(:gif) }
|
162
185
|
it { expect('http://adtangerine.com/foo/bar').not_to be_image_url }
|
163
186
|
```
|
164
187
|
|
165
|
-
|
188
|
+
##### be_url
|
189
|
+
``` ruby
|
190
|
+
it { expect('http://adtangerine.com').to be_url }
|
191
|
+
it { expect('adtangerine.com').not_to be_url }
|
192
|
+
```
|
193
|
+
|
194
|
+
#### "Have" matchers
|
195
|
+
|
196
|
+
##### have(x).items
|
166
197
|
``` ruby
|
167
198
|
it { expect([1]).to have(1).item }
|
168
199
|
it { expect(%w(foo bar)).to have(2).items }
|
169
200
|
it { expect(%w(foo bar)).not_to have(1).item }
|
170
201
|
```
|
171
202
|
|
172
|
-
|
203
|
+
##### have_at_least
|
173
204
|
``` ruby
|
174
205
|
it { expect(%w(foo bar foobar)).to have_at_least(3).items }
|
175
206
|
```
|
176
207
|
|
177
|
-
|
208
|
+
##### have_at_most
|
178
209
|
``` ruby
|
179
210
|
it { expect(%w(foo bar foobar)).to have_at_most(3).items }
|
180
211
|
```
|
181
|
-
|
212
|
+
|
213
|
+
##### have_error_on
|
182
214
|
``` ruby
|
183
|
-
subject
|
215
|
+
subject { User.new(email: nil, name: Faker::Name.name) }
|
184
216
|
|
185
217
|
it 'has an invalid email' do
|
186
|
-
|
187
|
-
|
188
|
-
expect(user).not_to have_error_on(:email)
|
218
|
+
subject.valid?
|
219
|
+
is_expected.to have_error_on(:email)
|
189
220
|
end
|
190
221
|
```
|
191
222
|
|
192
|
-
####
|
223
|
+
#### Other matchers
|
224
|
+
|
225
|
+
##### include_items
|
193
226
|
``` ruby
|
194
227
|
it { expect(%w(foo bar foobar)).to include_items(%w(foo bar foobar)) }
|
195
228
|
```
|
@@ -198,7 +231,9 @@ it { expect(%w(foo bar foobar)).to include_items(%w(foo bar foobar)) }
|
|
198
231
|
|
199
232
|
- Create macros for model scopes;
|
200
233
|
- Create macros for controllers;
|
201
|
-
- Add more matchers
|
234
|
+
- Add more matchers;
|
235
|
+
- Take groups of matchers into modules;
|
236
|
+
- Turn the whole into "A" in Code Climate.
|
202
237
|
|
203
238
|
## Contributing
|
204
239
|
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module SmartRspec
|
2
|
+
module Matchers
|
3
|
+
module BeMatchers
|
4
|
+
extend RSpec::Matchers::DSL
|
5
|
+
|
6
|
+
matcher :be_ascending do
|
7
|
+
match { |actual| actual == actual.sort }
|
8
|
+
end
|
9
|
+
|
10
|
+
matcher :be_a_bad_request do
|
11
|
+
match do |response|
|
12
|
+
response.code.to_s =~ /^4/
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
matcher :be_a_list_of do |klass|
|
17
|
+
match do |collection|
|
18
|
+
collection.all? { |e| e.is_a?(klass) }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
matcher :be_boolean do
|
23
|
+
match { |actual| [true, false].include?(actual) }
|
24
|
+
end
|
25
|
+
|
26
|
+
matcher :be_descending do
|
27
|
+
match do |actual|
|
28
|
+
actual.each_cons(2).all? { |i, j| i >= j }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
matcher :be_email do
|
33
|
+
match { |actual| actual =~ build_regex(:email) }
|
34
|
+
end
|
35
|
+
|
36
|
+
matcher :be_image_url do |*types|
|
37
|
+
match { |actual| actual =~ build_regex(:image, types) }
|
38
|
+
end
|
39
|
+
|
40
|
+
matcher :be_url do
|
41
|
+
match { |actual| actual =~ build_regex(:uri) }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module SmartRspec
|
2
|
+
module Matchers
|
3
|
+
module OtherMatchers
|
4
|
+
extend RSpec::Matchers::DSL
|
5
|
+
|
6
|
+
matcher :have_error_on do |attr|
|
7
|
+
match { |actual| actual.errors.keys.include?(attr) }
|
8
|
+
end
|
9
|
+
|
10
|
+
matcher :include_items do |items|
|
11
|
+
match { |actual| (items - actual).empty? }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
data/lib/smart_rspec/matchers.rb
CHANGED
@@ -1,45 +1,12 @@
|
|
1
|
-
require 'rspec/matchers'
|
2
|
-
require 'rspec/collection_matchers'
|
3
1
|
require 'smart_rspec/support/regexes'
|
2
|
+
require 'smart_rspec/matchers/be_matchers'
|
3
|
+
require 'smart_rspec/matchers/other_matchers'
|
4
4
|
|
5
5
|
module SmartRspec
|
6
6
|
module Matchers
|
7
|
-
extend RSpec::Matchers::DSL
|
8
7
|
include SmartRspec::Support::Regexes
|
9
|
-
|
10
|
-
|
11
|
-
match { |actual| [true, false].include?(actual) }
|
12
|
-
end
|
13
|
-
|
14
|
-
matcher :be_email do
|
15
|
-
match { |actual| actual =~ build_regex(:email) }
|
16
|
-
end
|
17
|
-
|
18
|
-
matcher :be_url do
|
19
|
-
match { |actual| actual =~ build_regex(:uri) }
|
20
|
-
end
|
21
|
-
|
22
|
-
matcher :be_image_url do |*types|
|
23
|
-
match { |actual| actual =~ build_regex(:image, types) }
|
24
|
-
end
|
25
|
-
|
26
|
-
matcher :have_error_on do |attr|
|
27
|
-
match { |actual| actual.errors.keys.include?(attr) }
|
28
|
-
end
|
29
|
-
|
30
|
-
matcher :include_items do |items|
|
31
|
-
match { |actual| (items - actual).empty? }
|
32
|
-
end
|
33
|
-
|
34
|
-
matcher :be_ascending do
|
35
|
-
match { |actual| actual == actual.sort }
|
36
|
-
end
|
37
|
-
|
38
|
-
matcher :be_descending do
|
39
|
-
match do |actual|
|
40
|
-
actual.each_cons(2).all? { |i, j| i >= j }
|
41
|
-
end
|
42
|
-
end
|
8
|
+
include SmartRspec::Matchers::BeMatchers
|
9
|
+
include SmartRspec::Matchers::OtherMatchers
|
43
10
|
end
|
44
11
|
end
|
45
12
|
|
data/lib/smart_rspec/version.rb
CHANGED
data/lib/smart_rspec.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
+
%w(macros matchers support/expectations).each { |f| require "smart_rspec/#{f}" }
|
2
|
+
|
1
3
|
require 'active_support/concern'
|
2
|
-
require '
|
3
|
-
require '
|
4
|
-
require 'smart_rspec/support/expectations'
|
4
|
+
require 'rspec/collection_matchers'
|
5
|
+
require 'rspec/matchers'
|
5
6
|
|
6
7
|
include SmartRspec::Matchers
|
7
8
|
|
@@ -111,21 +111,19 @@ describe 'SmartRspec Matchers' do
|
|
111
111
|
end
|
112
112
|
|
113
113
|
describe '#have_error_on' do
|
114
|
-
subject
|
114
|
+
subject { User.new(email: nil, name: Faker::Name.name) }
|
115
115
|
|
116
116
|
context 'when valid' do
|
117
117
|
it do
|
118
|
-
|
119
|
-
|
118
|
+
subject.valid?
|
119
|
+
is_expected.to have_error_on(:email)
|
120
120
|
end
|
121
121
|
end
|
122
122
|
|
123
123
|
context 'when invalid' do
|
124
124
|
it do
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
expect(mock).not_to have_error_on(:email)
|
125
|
+
subject.valid?
|
126
|
+
is_expected.not_to have_error_on(:name)
|
129
127
|
end
|
130
128
|
end
|
131
129
|
end
|
@@ -140,5 +138,35 @@ describe 'SmartRspec Matchers' do
|
|
140
138
|
it { expect(%w(foo bar foobar)).not_to include_items(%w(lorem)) }
|
141
139
|
end
|
142
140
|
end
|
143
|
-
end
|
144
141
|
|
142
|
+
describe '#be_a_list_of' do
|
143
|
+
context 'when valid' do
|
144
|
+
subject { Array.new(3, User.new) }
|
145
|
+
it { is_expected.to be_a_list_of(User) }
|
146
|
+
end
|
147
|
+
|
148
|
+
context 'when invalid' do
|
149
|
+
subject { Array.new(3, User.new) << nil }
|
150
|
+
it { is_expected.to_not be_a_list_of(User) }
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
describe '#be_a_bad_request' do
|
155
|
+
let(:response) { double('http response') }
|
156
|
+
|
157
|
+
context 'ok' do
|
158
|
+
before { allow(response).to receive(:code).and_return(200) }
|
159
|
+
it { expect(response).to_not be_a_bad_request }
|
160
|
+
end
|
161
|
+
|
162
|
+
context 'not found' do
|
163
|
+
before { allow(response).to receive(:code).and_return(404) }
|
164
|
+
it { expect(response).to be_a_bad_request }
|
165
|
+
end
|
166
|
+
|
167
|
+
context 'unauthorized' do
|
168
|
+
before { allow(response).to receive(:code).and_return(401) }
|
169
|
+
it { expect(response).to be_a_bad_request }
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_rspec
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tiago Guedes
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -118,6 +118,8 @@ files:
|
|
118
118
|
- lib/smart_rspec.rb
|
119
119
|
- lib/smart_rspec/macros.rb
|
120
120
|
- lib/smart_rspec/matchers.rb
|
121
|
+
- lib/smart_rspec/matchers/be_matchers.rb
|
122
|
+
- lib/smart_rspec/matchers/other_matchers.rb
|
121
123
|
- lib/smart_rspec/support/assertions.rb
|
122
124
|
- lib/smart_rspec/support/expectations.rb
|
123
125
|
- lib/smart_rspec/support/regexes.rb
|