Skip to content

Commit

Permalink
Add matcher be_a_list_of
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscomxs committed Mar 17, 2015
1 parent c4e07ba commit 604dd25
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ Execute:
$ bundle

Require the gem at the top of your `spec/rails_helper.rb` (or equivalent):
``` ruby
``` ruby
require 'smart_rspec'
```

Then include the SmartRspec module:

``` ruby
``` ruby
RSpec.configure do |config|
config.include SmartRspec
end
Expand All @@ -50,6 +50,7 @@ end
* [have_at_most](#have_at_most)
* [have_error_on](#have_error_on)
* [include_items](#include_items)
* [be_a_list_of](#be_a_list_of)

### Macros

Expand All @@ -61,7 +62,7 @@ It builds specs for model attributes and test its type, enumerated values and de
``` ruby
RSpec.describe User, type: :model do
subject { FactoryGirl.build(:user) }

has_attributes :email, type: :String
has_attributes :is_admin, type: :Boolean
has_attributes :score, type: :Integer, default: 0
Expand All @@ -75,21 +76,21 @@ It builds specs and test model associations like `belongs_to`, `has_one` and `ha
``` ruby
RSpec.describe User, type: :model do
subject { FactoryGirl.build(:user) }

belongs_to :business
has_one :project
has_many :tasks
end
```

#### fails_validation_of
#### fails_validation_of

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:

``` ruby
RSpec.describe User, type: :model do
subject { FactoryGirl.build(:user) }

context 'when invalid' do
fails_validation_of :email, presence: true, email: true
fails_validation_of :name, length: { maximum: 80 }, uniqueness: true
Expand Down Expand Up @@ -196,6 +197,11 @@ end
it { expect(%w(foo bar foobar)).to include_items(%w(foo bar foobar)) }
```

#### be_a_list_of
``` ruby
it { expect(Foo.fetch_api).to be_a_list_of(Foo)) }
```

# TODO

- Create macros for model scopes;
Expand All @@ -211,4 +217,3 @@ it { expect(%w(foo bar foobar)).to include_items(%w(foo bar foobar)) }
4. Commit your changes (`git commit -am 'Add some feature'`);
5. Push to the branch (`git push origin my-new-feature`);
6. Create new Pull Request.

7 changes: 6 additions & 1 deletion lib/smart_rspec/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ module Matchers
actual.each_cons(2).all? { |i, j| i >= j }
end
end

matcher :be_a_list_of do |klass|
match do |collection|
!collection.map { |object| object.is_a?(klass) }.include?(false)
end
end
end
end

13 changes: 12 additions & 1 deletion spec/smart_rspec/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,16 @@
it { expect(%w(foo bar foobar)).not_to include_items(%w(lorem)) }
end
end
end

describe '#be_a_list_of' do
context 'when valid' do
subject { [User.new, User.new, User.new] }
it { is_expected.to be_a_list_of(User) }
end

context 'when invalid' do
subject { [User.new, User.new, String.new] }
it { is_expected.to_not be_a_list_of(User) }
end
end
end

0 comments on commit 604dd25

Please sign in to comment.