Skip to content

Instantly share code, notes, and snippets.

@chrisgaunt
Forked from moklett/address_spec.rb
Created April 26, 2012 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisgaunt/2495790 to your computer and use it in GitHub Desktop.
Save chrisgaunt/2495790 to your computer and use it in GitHub Desktop.
Toying around with an RSpec DSL for permutations. My real use case is more complex than this example - the large numbers of inputs and outputs makes it more concise to "demonstrate" correctness and expectations than to "spec" it out with traditional, one
describe Address do
permute "validations" do
cases [
[:address, :address_2, :city, :state, :zip, :country, :valid, :errors_on],
[ nil, nil, nil, nil, nil, nil, false, ['address', 'city', 'state', 'zip', 'country']],
['123 Main St', 'Apt 100', 'Pleasantville', 'NC', '12345', 'US', true, []],
['123 Main St' 'Apt 100', 'Pleasantville' 'NC' '12345' nil, false ['country']]
['123 Main St' 'Apt 100', 'Pleasantville' 'NC' '12345' 'U', false ['country']]
['123 Main St' 'Apt 100', 'Pleasantville' 'NC' '12345' 'USA', false ['country']]
]
before do
@address = Address.new(
:address => d[:address],
:address_2 => d[:address_2],
:city => d[:city],
:state => d[:state],
:zip => d[:zip]
)
end
it "reports correctly for valid?" do
@address.valid?.should == d[:valid]
end
it "has errors on the correct attributes" do
@address.valid?
@address.errors.keys.sort.should == d[:errors_on].sort
end
conditionally_on(d[:country].blank?) do
it "has only 1 error on 'country', for being blank" do
@address.errors.get(:country).should == "can't be blank"
end
end
conditionally_on(d[:country].size != 2) do
it "has only 1 error on 'country', for not being 2 characters" do
@address.errors.get(:country).should == "must be a valid 2-character country code"
end
end
end
end
# Address validations case 1 (:address => nil, :address_2 => nil, ...) reports correctly for valid?
# Address validations case 1 (:address => nil, :address_2 => nil, ...) has errors on the correct attributes
#
# Address validations case 2 (:address => '123 Main St', :address_2 => 'Apt 100', ...) reports correctly for valid?
# Address validations case 2 (:address => '123 Main St', :address_2 => 'Apt 100', ...) has errors on the correct attributes
#
# Address validations case 3 (:address => '123 Main St', :address_2 => 'Apt 100', ...) reports correctly for valid?
# Address validations case 3 (:address => '123 Main St', :address_2 => 'Apt 100', ...) has errors on the correct attributes
# Address validations case 3 (:address => '123 Main St', :address_2 => 'Apt 100', ...) has only 1 error on 'country', for being blank
#
# Address validations case 4 (:address => '123 Main St', :address_2 => 'Apt 100', ...) reports correctly for valid?
# Address validations case 4 (:address => '123 Main St', :address_2 => 'Apt 100', ...) has errors on the correct attributes
# Address validations case 4 (:address => '123 Main St', :address_2 => 'Apt 100', ...) has only 1 error on 'country', for not being 2 characters
#
# Address validations case 5 (:address => '123 Main St', :address_2 => 'Apt 100', ...) reports correctly for valid?
# Address validations case 5 (:address => '123 Main St', :address_2 => 'Apt 100', ...) has errors on the correct attributes
# Address validations case 5 (:address => '123 Main St', :address_2 => 'Apt 100', ...) has only 1 error on 'country', for not being 2 characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment