Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit 4f8038de0a62d1cfea6b74bd85bbf66ae7d95c52 1 parent 29c5243
Graeme Mathieson authored July 21, 2012
2  config/locales/en.yml
@@ -4,3 +4,5 @@ en:
4 4
       blank: "can't be empty"
5 5
       inclusion: 'is not in the list'
6 6
       invalid: 'is invalid'
  7
+      too_long: 'is invalid'
  8
+      wrong_length: 'is invalid'
2  lib/sage_pay/server/address.rb
@@ -34,7 +34,7 @@ class Address
34 34
       # verboten otherwise.
35 35
 
36 36
       validates :state, :presence => {:message => "is required if the country is US"}, :if => :in_us?
37  
-      validates :state, :acceptance => { :accept => nil, :message => "is present but the country is not US" }, :unless => :in_us?
  37
+      validates_inclusion_of :state, :in => ['', nil], :message => "is present but the country is not US", :unless => :in_us?
38 38
 
39 39
       def initialize(attributes = {})
40 40
         attributes.each do |k, v|
2  sage_pay.gemspec
@@ -33,7 +33,6 @@ gateway for accepting credit card payments through your web app.
33 33
 
34 34
   s.add_development_dependency('rspec')
35 35
   s.add_development_dependency('rake')
36  
-  s.add_development_dependency('remarkable_activemodel')
37 36
 
38 37
   s.files = %w[
39 38
     .rbenv-version
@@ -84,7 +83,6 @@ gateway for accepting credit card payments through your web app.
84 83
     spec/spec_helper.rb
85 84
     spec/support/factories.rb
86 85
     spec/support/integration.rb
87  
-    spec/support/remarkable.rb
88 86
     spec/support/validation_matchers.rb
89 87
   ]
90 88
 
38  spec/sage_pay/server/address_spec.rb
... ...
@@ -1,7 +1,6 @@
1 1
 require 'spec_helper'
2 2
 
3 3
 describe SagePay::Server::Address do
4  
-  include ValidationMatchers
5 4
   it "should be valid straight from the factory" do
6 5
     lambda {
7 6
       address_factory.should be_valid
@@ -9,25 +8,24 @@
9 8
   end
10 9
 
11 10
   describe "validations" do
12  
-    it { validate_presence_of(:first_names) }
13  
-    it { validate_presence_of(:surname)     }
14  
-    it { validate_presence_of(:address_1)   }
15  
-    it { validate_presence_of(:city)        }
16  
-    it { validate_presence_of(:post_code)   }
17  
-    it { validate_presence_of(:country)     }
18  
-
19  
-    should_not_validate_presence_of(:address_2)
20  
-    should_not_validate_presence_of(:state)
21  
-    should_not_validate_presence_of(:phone)
22  
-
23  
-    should_validate_length_of(:first_names, :maximum => 20)
24  
-    should_validate_length_of(:surname,     :maximum => 20)
25  
-    should_validate_length_of(:address_1,   :maximum => 100)
26  
-    should_validate_length_of(:address_2,   :maximum => 100)
27  
-    should_validate_length_of(:city,        :maximum => 40)
28  
-    should_validate_length_of(:post_code,   :maximum => 10)
29  
-    should_validate_length_of(:phone,       :maximum => 20)
30  
-
  11
+    it { validates_the_presence_of(:address, :first_names) }
  12
+    it { validates_the_presence_of(:address, :surname)     }
  13
+    it { validates_the_presence_of(:address, :address_1)   }
  14
+    it { validates_the_presence_of(:address, :city)        }
  15
+    it { validates_the_presence_of(:address, :post_code)   }
  16
+    it { validates_the_presence_of(:address, :country)     }
  17
+
  18
+    it { does_not_require_the_presence_of(:address, :address_2) }
  19
+    it { does_not_require_the_presence_of(:address, :state)     }
  20
+    it { does_not_require_the_presence_of(:address, :phone)     }
  21
+
  22
+    it { validates_the_length_of(:address, :first_names, :max => 20)  }
  23
+    it { validates_the_length_of(:address, :surname,     :max => 20)  }
  24
+    it { validates_the_length_of(:address, :address_1,   :max => 100) }
  25
+    it { validates_the_length_of(:address, :address_2,   :max => 100) }
  26
+    it { validates_the_length_of(:address, :city,        :max => 40)  }
  27
+    it { validates_the_length_of(:address, :post_code,   :max => 10)  }
  28
+    it { validates_the_length_of(:address, :phone,       :max => 20)  }
31 29
 
32 30
     it "validates the format of first names" do
33 31
       validates_the_format_of :address, :first_names,
8  spec/sage_pay/server/notification_response_spec.rb
@@ -10,8 +10,8 @@
10 10
   end
11 11
 
12 12
   describe "validations" do
13  
-    it { should validate_presence_of :status }
14  
-    it { should validate_presence_of :redirect_url }
  13
+    it { validates_the_presence_of(:notification_response, :status) }
  14
+    it { validates_the_presence_of(:notification_response, :redirect_url) }
15 15
 
16 16
     it "validates the presence of the status_detail field only if the status is something other than OK" do
17 17
       notification_response = notification_response_factory(:status => :ok, :status_detail => nil)
@@ -25,8 +25,8 @@
25 25
       notification_response.errors[:status_detail].should include("can't be empty")
26 26
     end
27 27
 
28  
-    should_validate_length_of(:redirect_url,  :maximum => 255)
29  
-    should_validate_length_of(:status_detail, :maximum => 255)
  28
+    it { validates_the_length_of(:notification_response, :redirect_url,  :max => 255) }
  29
+    it { validates_the_length_of(:notification_response, :status_detail, :max => 255) }
30 30
 
31 31
     it "should allow the status to be one of :ok, :invalid or :error" do
32 32
       notification_response = notification_response_factory(:status => :ok)
56  spec/sage_pay/server/registration_spec.rb
@@ -36,34 +36,34 @@
36 36
   end
37 37
 
38 38
   describe "validations" do
39  
-    should_validate_presence_of(:mode)
40  
-    should_validate_presence_of(:tx_type)
41  
-    should_validate_presence_of(:vendor)
42  
-    should_validate_presence_of(:vendor_tx_code)
43  
-    should_validate_presence_of(:amount)
44  
-    should_validate_presence_of(:currency)
45  
-    should_validate_presence_of(:description)
46  
-    should_validate_presence_of(:notification_url)
47  
-    should_validate_presence_of(:billing_address)
48  
-    should_validate_presence_of(:delivery_address)
49  
-
50  
-    should_not_validate_presence_of(:customer_email)
51  
-    should_not_validate_presence_of(:basket)
52  
-    should_not_validate_presence_of(:allow_gift_aid)
53  
-    should_not_validate_presence_of(:apply_avs_cv2)
54  
-    should_not_validate_presence_of(:apply_3d_secure)
55  
-    should_not_validate_presence_of(:profile)
56  
-    should_not_validate_presence_of(:billing_agreement)
57  
-    should_not_validate_presence_of(:account_type)
58  
-
59  
-    should_validate_length_of(:vendor, :maximum => 15)
60  
-    should_validate_length_of(:vendor_tx_code, :maximum => 40)
61  
-    should_validate_length_of(:currency, :exactly => 3)
62  
-    should_validate_length_of(:description, :maximum => 100)
63  
-    should_validate_length_of(:notification_url, :maximum => 255)
64  
-    should_validate_length_of(:notification_url, :maximum => 255)
65  
-    should_validate_length_of(:customer_email, :maximum => 255)
66  
-    should_validate_length_of(:basket, :maximum => 7500)
  39
+    it { validates_the_presence_of(:registration, :mode)             }
  40
+    it { validates_the_presence_of(:registration, :tx_type)          }
  41
+    it { validates_the_presence_of(:registration, :vendor)           }
  42
+    it { validates_the_presence_of(:registration, :vendor_tx_code)   }
  43
+    it { validates_the_presence_of(:registration, :amount)           }
  44
+    it { validates_the_presence_of(:registration, :currency)         }
  45
+    it { validates_the_presence_of(:registration, :description)      }
  46
+    it { validates_the_presence_of(:registration, :notification_url) }
  47
+    it { validates_the_presence_of(:registration, :billing_address)  }
  48
+    it { validates_the_presence_of(:registration, :delivery_address) }
  49
+
  50
+    it { does_not_require_the_presence_of(:registration, :customer_email)    }
  51
+    it { does_not_require_the_presence_of(:registration, :basket)            }
  52
+    it { does_not_require_the_presence_of(:registration, :allow_gift_aid)    }
  53
+    it { does_not_require_the_presence_of(:registration, :apply_avs_cv2)     }
  54
+    it { does_not_require_the_presence_of(:registration, :apply_3d_secure)   }
  55
+    it { does_not_require_the_presence_of(:registration, :profile)           }
  56
+    it { does_not_require_the_presence_of(:registration, :billing_agreement) }
  57
+    it { does_not_require_the_presence_of(:registration, :account_type)      }
  58
+
  59
+    it { validates_the_length_of(:registration, :vendor,           :max => 15)    }
  60
+    it { validates_the_length_of(:registration, :vendor_tx_code,   :max => 40)    }
  61
+    it { validates_the_length_of(:registration, :currency,         :exactly => 3) }
  62
+    it { validates_the_length_of(:registration, :description,      :max => 100)   }
  63
+    it { validates_the_length_of(:registration, :notification_url, :max => 255)   }
  64
+    it { validates_the_length_of(:registration, :notification_url, :max => 255)   }
  65
+    it { validates_the_length_of(:registration, :customer_email,   :max => 255)   }
  66
+    it { validates_the_length_of(:registration, :basket,           :max => 7500)  }
67 67
 
68 68
     it "should allow the amount to be a minimum of 0.01" do
69 69
       registration = registration_factory(:amount => "0.01")
4  spec/sage_pay_spec.rb
... ...
@@ -1,7 +1,7 @@
1 1
 require 'spec_helper'
2 2
 
3 3
 describe SagePay do
4  
-  it "should be version 0.2.13" do
5  
-    SagePay::VERSION.should == '0.2.13'
  4
+  it "should be version 1.0.0" do
  5
+    SagePay::VERSION.should == '1.0.0'
6 6
   end
7 7
 end
1  spec/spec_helper.rb
@@ -9,4 +9,5 @@
9 9
 
10 10
 RSpec.configure do |config|
11 11
   config.include(Factories)
  12
+  config.include(ValidationMatchers)
12 13
 end
2  spec/support/remarkable.rb
... ...
@@ -1,2 +0,0 @@
1  
-require 'remarkable/active_model'
2  
-Remarkable.include_matchers!(Remarkable::ActiveModel, RSpec::Core::ExampleGroup)
60  spec/support/validation_matchers.rb
... ...
@@ -1,15 +1,71 @@
1 1
 module ValidationMatchers
  2
+  def validates_the_presence_of(model_name, attribute, message = "can't be empty")
  3
+    model = model_factory(model_name, attribute => "")
  4
+    model.should_not be_valid
  5
+    model.errors[attribute].should include(message)
  6
+  end
  7
+
  8
+  def does_not_require_the_presence_of(model_name, attribute)
  9
+    model = model_factory(model_name, attribute => "")
  10
+    model.should be_valid
  11
+    model.errors[attribute].should be_empty
  12
+  end
  13
+
  14
+  def validates_the_length_of(model_name, attribute, limits, message = "is invalid")
  15
+    if limits[:min]
  16
+      model = model_factory(model_name, attribute => 'a' * (limits[:min] - 1))
  17
+      model.should_not be_valid
  18
+      model.errors[attribute].should include(message)
  19
+
  20
+      model = model_factory(model_name, attribute => 'a' * limits[:min])
  21
+      model.should be_valid
  22
+      model.errors[attribute].should be_empty
  23
+
  24
+      model = model_factory(model_name, attribute => 'a' * (limits[:min] + 1))
  25
+      model.should be_valid
  26
+      model.errors[attribute].should be_empty
  27
+    end
  28
+
  29
+    if limits[:max]
  30
+      model = model_factory(model_name, attribute => 'a' * (limits[:max] - 1))
  31
+      model.should be_valid
  32
+      model.errors[attribute].should be_empty
  33
+
  34
+      model = model_factory(model_name, attribute => 'a' * limits[:max])
  35
+      model.should be_valid
  36
+      model.errors[attribute].should be_empty
  37
+
  38
+      model = model_factory(model_name, attribute => 'a' * (limits[:max] + 1))
  39
+      model.should_not be_valid
  40
+      model.errors[attribute].should include(message)
  41
+    end
  42
+
  43
+    if limits[:exactly]
  44
+      model = model_factory(model_name, attribute => 'a' * (limits[:exactly] - 1))
  45
+      model.should_not be_valid
  46
+      model.errors[attribute].should include(message)
  47
+
  48
+      model = model_factory(model_name, attribute => 'a' * limits[:exactly])
  49
+      model.should be_valid
  50
+      model.errors[attribute].should be_empty
  51
+
  52
+      model = model_factory(model_name, attribute => 'a' * (limits[:exactly] + 1))
  53
+      model.should_not be_valid
  54
+      model.errors[attribute].should include(message)
  55
+    end
  56
+  end
  57
+
2 58
   def validates_the_format_of(model_name, attribute, examples, message = "is invalid")
3 59
     (examples[:invalid] || []).each do |invalid|
4 60
       model = model_factory(model_name, attribute => invalid)
5 61
       model.should_not be_valid
6  
-      model.errors[attribute][0].should == message
  62
+      model.errors[attribute].should include(message)
7 63
     end
8 64
 
9 65
     (examples[:valid] || []).each do |valid|
10 66
       model = model_factory(model_name, attribute => valid)
11 67
       model.should be_valid
12  
-      model.errors[attribute][0].should be_nil
  68
+      model.errors[attribute].should be_empty
13 69
     end
14 70
   end
15 71
 end

0 notes on commit 4f8038d

Please sign in to comment.
Something went wrong with that request. Please try again.