Skip to content

Commit

Permalink
wired up a framework for testing with rspec, including 2 POC specs
Browse files Browse the repository at this point in the history
  • Loading branch information
betesh committed Aug 15, 2014
1 parent 58282ec commit 5ba7074
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--order random
22 changes: 22 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'active_model' if Gem.loaded_specs.keys.include?('activemodel')

RSpec::Matchers.define :have_errors_on_email do
match do |actual|
expect(actual).not_to be_nil, "#{actual} should not be nil"
expect(actual).not_to be_empty, "#{actual} should not be empty"
expect(actual.size).to eq(@reasons.size), "#{actual} should have #{@reasons.size} elements"
@reasons.each do |reason|
reason = "#{"Email " if defined?(ActiveModel)}#{reason}"
expect(actual).to include(reason), "#{actual} should contain #{reason}"
end
end
chain :because do |reason|
(@reasons ||= []) << reason
end
chain :and_because do |reason|
(@reasons ||= []) << reason
end
match_when_negated do |actual|
expect(actual).to (defined?(ActiveModel) ? be_empty : be_nil)
end
end
33 changes: 33 additions & 0 deletions spec/validates_email_format_of_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require_relative "spec_helper"
require "validates_email_format_of"

describe ValidatesEmailFormatOf do
before(:all) do
described_class.load_i18n_locales
I18n.enforce_available_locales = false
end
subject do |example|
if defined?(ActiveModel)
user = Class.new do
def initialize(email)
@email = email
end
attr_reader :email
include ActiveModel::Validations
validates_email_format_of :email, example.example_group_instance.options
end
example.example_group_instance.class::User = user
user.new(example.example_group_instance.email).tap(&:valid?).errors.full_messages
else
ValidatesEmailFormatOf::validate_email_format(email, options)
end
end
let(:options) { {} }
let(:email) { |example| example.example_group.description }
describe "no_at_symbol" do
it { should have_errors_on_email.because("does not appear to be a valid e-mail address") }
end
describe "user1@gmail.com" do
it { should_not have_errors_on_email }
end
end
1 change: 1 addition & 0 deletions validates_email_format_of.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ spec = Gem::Specification.new do |s|
s.require_paths = ['lib']

s.add_dependency 'i18n'
s.add_development_dependency 'rspec'
end

0 comments on commit 5ba7074

Please sign in to comment.