Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSpec matcher (validate_email_with_postcode_anywhere) #2

Merged
merged 4 commits into from
Jul 31, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Gemfile.lock
pkg
.idea
.DS_Store
coverage
1 change: 1 addition & 0 deletions lib/postcode_anywhere/email_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'postcode_anywhere/email_validation/validation_error'
require 'postcode_anywhere/email_validation/validator'
require 'postcode_anywhere/email_validation/version'
require 'postcode_anywhere/matchers/email_validator' if defined?(RSpec)

require 'rest_client'

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.3'
VERSION = '0.0.4'
end
end
30 changes: 30 additions & 0 deletions lib/postcode_anywhere/matchers/email_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module PostcodeAnywhere
module Matchers
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, 'should')
end

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

def failure_message(model, should_or_should_not)
"#{model.class} #{should_or_should_not} have 'PostcodeAnywhere::EmailValidation::Validator' on attribute #{attribute}"
end
end
end
end
24 changes: 24 additions & 0 deletions spec/lib/postcode_anywhere/matchers/email_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'spec_helper'

module PostcodeAnywhere::EmailValidation

describe 'Validator Matcher' do

context 'when Validator is defined' do
subject { ::ValidatedClass.new }

it { should validate_email_with_postcode_anywhere }

it { should validate_email_with_postcode_anywhere.on_attribute(:email) }

it { should_not validate_email_with_postcode_anywhere.on_attribute(:email_address) }
end

context 'when Validator is not defined' do
subject { ::NotValidatedClass.new }

it { should_not validate_email_with_postcode_anywhere }
end

end
end
28 changes: 0 additions & 28 deletions spec/support/matchers.rb

This file was deleted.

4 changes: 4 additions & 0 deletions spec/support/not_validated_class.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class NotValidatedClass
include ActiveModel::Validations
end