From 604dd25ef54c617fbec9e4ed0b7c3ed3f0e715a6 Mon Sep 17 00:00:00 2001 From: Francisco Martins Date: Tue, 17 Mar 2015 15:04:15 -0300 Subject: [PATCH] Add matcher be_a_list_of --- README.md | 19 ++++++++++++------- lib/smart_rspec/matchers.rb | 7 ++++++- spec/smart_rspec/matchers_spec.rb | 13 ++++++++++++- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 1f8cecb..6b95f90 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 @@ -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 @@ -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; @@ -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. - diff --git a/lib/smart_rspec/matchers.rb b/lib/smart_rspec/matchers.rb index 3e55e2d..b93ef83 100644 --- a/lib/smart_rspec/matchers.rb +++ b/lib/smart_rspec/matchers.rb @@ -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 - diff --git a/spec/smart_rspec/matchers_spec.rb b/spec/smart_rspec/matchers_spec.rb index 194b260..6abbfa7 100644 --- a/spec/smart_rspec/matchers_spec.rb +++ b/spec/smart_rspec/matchers_spec.rb @@ -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