Skip to content

Commit

Permalink
Merge pull request #1 from moneyadviceservice/matcher
Browse files Browse the repository at this point in the history
Matcher
  • Loading branch information
novotnyjakub committed Jul 29, 2013
2 parents de6e9eb + 574a571 commit 6b79166
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ class ValidatedClass
end
```

## Usage with RSpec

With default attribute: :email

```ruby
it { should validate_email_with_postcode_anywhere }
```

With custom attribute.

```ruby
it { should validate_email_with_postcode_anywhere.on_attribute(:email) }
```

## Contributing

1. Fork it
Expand Down
2 changes: 1 addition & 1 deletion lib/postcode_anywhere/email_validation/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module PostcodeAnywhere
module EmailValidation
VERSION = '0.0.2'
VERSION = '0.0.3'
end
end
2 changes: 1 addition & 1 deletion postcodeanywhere-emailvalidation.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
spec.description = %q{Verify the existence of an email address.}
spec.summary = %q{Verifies the existence of an email address using the Email Validation web services from PostcodeAnywhere.}
spec.homepage = 'https://github.com/moneyadviceservice/postcode_anywhere-email_validation'
spec.date = "2013-06-04"
spec.date = '2013-06-04'
spec.files = `git ls-files`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/postcode_anywhere/email_validation/validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ module EmailValidation
end
end

describe 'RSpec Matcher' do
context 'with default attribute' do
it { should validate_email_with_postcode_anywhere }
end
context 'with custom attribute' do
it { should validate_email_with_postcode_anywhere.on_attribute(:email) }
end
end
end
end
end
28 changes: 28 additions & 0 deletions spec/support/matchers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
RSpec::Matchers.define :validate_email_with_postcode_anywhere do

chain :on_attribute do |attribute|
@attribute = attribute
end

match do |model|
model_validators = model.class.validators_on(attribute)
model_validators.any? { |validator| validator.instance_of?(PostcodeAnywhere::EmailValidation::Validator) }
end

def attribute
@attribute || :email
end

failure_message_for_should do |model|
failure_message(model)
end

failure_message_for_should_not do |model|
failure_message(model, 'not')
end

def failure_message(model, option = '')
"#{model.class} should #{option} have 'PostcodeAnywhere::EmailValidation::Validator' on attribute #{attribute}"
end

end

0 comments on commit 6b79166

Please sign in to comment.