Permalink
Browse files

I liked my old matchers, don't introduce an extra dependency.

  • Loading branch information...
1 parent 29c5243 commit 4f8038de0a62d1cfea6b74bd85bbf66ae7d95c52 @mathie committed Jul 21, 2012
View
@@ -4,3 +4,5 @@ en:
blank: "can't be empty"
inclusion: 'is not in the list'
invalid: 'is invalid'
+ too_long: 'is invalid'
+ wrong_length: 'is invalid'
@@ -34,7 +34,7 @@ class Address
# verboten otherwise.
validates :state, :presence => {:message => "is required if the country is US"}, :if => :in_us?
- validates :state, :acceptance => { :accept => nil, :message => "is present but the country is not US" }, :unless => :in_us?
+ validates_inclusion_of :state, :in => ['', nil], :message => "is present but the country is not US", :unless => :in_us?
def initialize(attributes = {})
attributes.each do |k, v|
View
@@ -33,7 +33,6 @@ gateway for accepting credit card payments through your web app.
s.add_development_dependency('rspec')
s.add_development_dependency('rake')
- s.add_development_dependency('remarkable_activemodel')
s.files = %w[
.rbenv-version
@@ -84,7 +83,6 @@ gateway for accepting credit card payments through your web app.
spec/spec_helper.rb
spec/support/factories.rb
spec/support/integration.rb
- spec/support/remarkable.rb
spec/support/validation_matchers.rb
]
@@ -1,33 +1,31 @@
require 'spec_helper'
describe SagePay::Server::Address do
- include ValidationMatchers
it "should be valid straight from the factory" do
lambda {
address_factory.should be_valid
}.should_not raise_error
end
describe "validations" do
- it { validate_presence_of(:first_names) }
- it { validate_presence_of(:surname) }
- it { validate_presence_of(:address_1) }
- it { validate_presence_of(:city) }
- it { validate_presence_of(:post_code) }
- it { validate_presence_of(:country) }
-
- should_not_validate_presence_of(:address_2)
- should_not_validate_presence_of(:state)
- should_not_validate_presence_of(:phone)
-
- should_validate_length_of(:first_names, :maximum => 20)
- should_validate_length_of(:surname, :maximum => 20)
- should_validate_length_of(:address_1, :maximum => 100)
- should_validate_length_of(:address_2, :maximum => 100)
- should_validate_length_of(:city, :maximum => 40)
- should_validate_length_of(:post_code, :maximum => 10)
- should_validate_length_of(:phone, :maximum => 20)
-
+ it { validates_the_presence_of(:address, :first_names) }
+ it { validates_the_presence_of(:address, :surname) }
+ it { validates_the_presence_of(:address, :address_1) }
+ it { validates_the_presence_of(:address, :city) }
+ it { validates_the_presence_of(:address, :post_code) }
+ it { validates_the_presence_of(:address, :country) }
+
+ it { does_not_require_the_presence_of(:address, :address_2) }
+ it { does_not_require_the_presence_of(:address, :state) }
+ it { does_not_require_the_presence_of(:address, :phone) }
+
+ it { validates_the_length_of(:address, :first_names, :max => 20) }
+ it { validates_the_length_of(:address, :surname, :max => 20) }
+ it { validates_the_length_of(:address, :address_1, :max => 100) }
+ it { validates_the_length_of(:address, :address_2, :max => 100) }
+ it { validates_the_length_of(:address, :city, :max => 40) }
+ it { validates_the_length_of(:address, :post_code, :max => 10) }
+ it { validates_the_length_of(:address, :phone, :max => 20) }
it "validates the format of first names" do
validates_the_format_of :address, :first_names,
@@ -10,8 +10,8 @@
end
describe "validations" do
- it { should validate_presence_of :status }
- it { should validate_presence_of :redirect_url }
+ it { validates_the_presence_of(:notification_response, :status) }
+ it { validates_the_presence_of(:notification_response, :redirect_url) }
it "validates the presence of the status_detail field only if the status is something other than OK" do
notification_response = notification_response_factory(:status => :ok, :status_detail => nil)
@@ -25,8 +25,8 @@
notification_response.errors[:status_detail].should include("can't be empty")
end
- should_validate_length_of(:redirect_url, :maximum => 255)
- should_validate_length_of(:status_detail, :maximum => 255)
+ it { validates_the_length_of(:notification_response, :redirect_url, :max => 255) }
+ it { validates_the_length_of(:notification_response, :status_detail, :max => 255) }
it "should allow the status to be one of :ok, :invalid or :error" do
notification_response = notification_response_factory(:status => :ok)
@@ -36,34 +36,34 @@
end
describe "validations" do
- should_validate_presence_of(:mode)
- should_validate_presence_of(:tx_type)
- should_validate_presence_of(:vendor)
- should_validate_presence_of(:vendor_tx_code)
- should_validate_presence_of(:amount)
- should_validate_presence_of(:currency)
- should_validate_presence_of(:description)
- should_validate_presence_of(:notification_url)
- should_validate_presence_of(:billing_address)
- should_validate_presence_of(:delivery_address)
-
- should_not_validate_presence_of(:customer_email)
- should_not_validate_presence_of(:basket)
- should_not_validate_presence_of(:allow_gift_aid)
- should_not_validate_presence_of(:apply_avs_cv2)
- should_not_validate_presence_of(:apply_3d_secure)
- should_not_validate_presence_of(:profile)
- should_not_validate_presence_of(:billing_agreement)
- should_not_validate_presence_of(:account_type)
-
- should_validate_length_of(:vendor, :maximum => 15)
- should_validate_length_of(:vendor_tx_code, :maximum => 40)
- should_validate_length_of(:currency, :exactly => 3)
- should_validate_length_of(:description, :maximum => 100)
- should_validate_length_of(:notification_url, :maximum => 255)
- should_validate_length_of(:notification_url, :maximum => 255)
- should_validate_length_of(:customer_email, :maximum => 255)
- should_validate_length_of(:basket, :maximum => 7500)
+ it { validates_the_presence_of(:registration, :mode) }
+ it { validates_the_presence_of(:registration, :tx_type) }
+ it { validates_the_presence_of(:registration, :vendor) }
+ it { validates_the_presence_of(:registration, :vendor_tx_code) }
+ it { validates_the_presence_of(:registration, :amount) }
+ it { validates_the_presence_of(:registration, :currency) }
+ it { validates_the_presence_of(:registration, :description) }
+ it { validates_the_presence_of(:registration, :notification_url) }
+ it { validates_the_presence_of(:registration, :billing_address) }
+ it { validates_the_presence_of(:registration, :delivery_address) }
+
+ it { does_not_require_the_presence_of(:registration, :customer_email) }
+ it { does_not_require_the_presence_of(:registration, :basket) }
+ it { does_not_require_the_presence_of(:registration, :allow_gift_aid) }
+ it { does_not_require_the_presence_of(:registration, :apply_avs_cv2) }
+ it { does_not_require_the_presence_of(:registration, :apply_3d_secure) }
+ it { does_not_require_the_presence_of(:registration, :profile) }
+ it { does_not_require_the_presence_of(:registration, :billing_agreement) }
+ it { does_not_require_the_presence_of(:registration, :account_type) }
+
+ it { validates_the_length_of(:registration, :vendor, :max => 15) }
+ it { validates_the_length_of(:registration, :vendor_tx_code, :max => 40) }
+ it { validates_the_length_of(:registration, :currency, :exactly => 3) }
+ it { validates_the_length_of(:registration, :description, :max => 100) }
+ it { validates_the_length_of(:registration, :notification_url, :max => 255) }
+ it { validates_the_length_of(:registration, :notification_url, :max => 255) }
+ it { validates_the_length_of(:registration, :customer_email, :max => 255) }
+ it { validates_the_length_of(:registration, :basket, :max => 7500) }
it "should allow the amount to be a minimum of 0.01" do
registration = registration_factory(:amount => "0.01")
View
@@ -1,7 +1,7 @@
require 'spec_helper'
describe SagePay do
- it "should be version 0.2.13" do
- SagePay::VERSION.should == '0.2.13'
+ it "should be version 1.0.0" do
+ SagePay::VERSION.should == '1.0.0'
end
end
View
@@ -9,4 +9,5 @@
RSpec.configure do |config|
config.include(Factories)
+ config.include(ValidationMatchers)
end
@@ -1,2 +0,0 @@
-require 'remarkable/active_model'
-Remarkable.include_matchers!(Remarkable::ActiveModel, RSpec::Core::ExampleGroup)
@@ -1,15 +1,71 @@
module ValidationMatchers
+ def validates_the_presence_of(model_name, attribute, message = "can't be empty")
+ model = model_factory(model_name, attribute => "")
+ model.should_not be_valid
+ model.errors[attribute].should include(message)
+ end
+
+ def does_not_require_the_presence_of(model_name, attribute)
+ model = model_factory(model_name, attribute => "")
+ model.should be_valid
+ model.errors[attribute].should be_empty
+ end
+
+ def validates_the_length_of(model_name, attribute, limits, message = "is invalid")
+ if limits[:min]
+ model = model_factory(model_name, attribute => 'a' * (limits[:min] - 1))
+ model.should_not be_valid
+ model.errors[attribute].should include(message)
+
+ model = model_factory(model_name, attribute => 'a' * limits[:min])
+ model.should be_valid
+ model.errors[attribute].should be_empty
+
+ model = model_factory(model_name, attribute => 'a' * (limits[:min] + 1))
+ model.should be_valid
+ model.errors[attribute].should be_empty
+ end
+
+ if limits[:max]
+ model = model_factory(model_name, attribute => 'a' * (limits[:max] - 1))
+ model.should be_valid
+ model.errors[attribute].should be_empty
+
+ model = model_factory(model_name, attribute => 'a' * limits[:max])
+ model.should be_valid
+ model.errors[attribute].should be_empty
+
+ model = model_factory(model_name, attribute => 'a' * (limits[:max] + 1))
+ model.should_not be_valid
+ model.errors[attribute].should include(message)
+ end
+
+ if limits[:exactly]
+ model = model_factory(model_name, attribute => 'a' * (limits[:exactly] - 1))
+ model.should_not be_valid
+ model.errors[attribute].should include(message)
+
+ model = model_factory(model_name, attribute => 'a' * limits[:exactly])
+ model.should be_valid
+ model.errors[attribute].should be_empty
+
+ model = model_factory(model_name, attribute => 'a' * (limits[:exactly] + 1))
+ model.should_not be_valid
+ model.errors[attribute].should include(message)
+ end
+ end
+
def validates_the_format_of(model_name, attribute, examples, message = "is invalid")
(examples[:invalid] || []).each do |invalid|
model = model_factory(model_name, attribute => invalid)
model.should_not be_valid
- model.errors[attribute][0].should == message
+ model.errors[attribute].should include(message)
end
(examples[:valid] || []).each do |valid|
model = model_factory(model_name, attribute => valid)
model.should be_valid
- model.errors[attribute][0].should be_nil
+ model.errors[attribute].should be_empty
end
end
end

0 comments on commit 4f8038d

Please sign in to comment.