Created
May 22, 2010 01:16
-
-
Save kristjan/409652 to your computer and use it in GitHub Desktop.
Some rspec macros to verify Rails model validations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Require fields. | |
it_should_require :wish, :author, :title, :body, :signature | |
# Validate string lengths | |
it_should_validate_strings :body => 1024, :title => 255, :signature => 25 | |
# Strings can be absent. Default length is 255, like Rails. | |
it_should_validate_strings :story => 10000, | |
:blurb => {:optional => true}, | |
:tagline => {:optional => true} | |
# Optional with a non-default length. | |
it_should_validate_strings :note => {:length => 1024, :optional => true} | |
# Specify a specific Fixjour builder. | |
use_builder :video_attachment # => new_video_attachment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Spec::Example::ExampleGroupMethods | |
def use_builder(builder) # Using Fixjour | |
module_eval <<-BUILDER | |
def object_builder | |
#{builder.inspect} | |
end | |
BUILDER | |
end | |
def it_should_require(*fields) | |
@required_fields = fields | |
instance_eval <<-REQUIREMENTS | |
it_should_behave_like "things with required fields" | |
REQUIREMENTS | |
end | |
def it_should_validate_strings(string_lengths) | |
@string_lengths_to_validate = string_lengths | |
instance_eval <<-REQUIREMENTS | |
it_should_behave_like "things with strings" | |
REQUIREMENTS | |
end | |
end | |
shared_examples_for "object builders" do | |
def new_object(*args) | |
builder_name = described_class.name.underscore | |
builder_name = object_builder if respond_to?(:object_builder) | |
send("new_#{builder_name}", *args) | |
end | |
end | |
shared_examples_for "things with required fields" do | |
it_should_behave_like "object builders" | |
@required_fields.each do |field| | |
it "must have a(n) #{field}" do | |
new_object(field => nil).should_not be_valid | |
end | |
end | |
end | |
shared_examples_for "things with strings" do | |
it_should_behave_like "object builders" | |
@string_lengths_to_validate.each do |field, options| | |
if options.is_a?(Fixnum) | |
max_length = options | |
else | |
max_length = options.fetch(:length, 255) | |
allow_blank = options.fetch(:optional, false) | |
end | |
unless allow_blank | |
it "can't have an empty #{field}" do | |
new_object(field => '').should_not be_valid | |
end | |
end | |
it "can't have a loooooong #{field}" do | |
new_object(field => 'a'*(max_length+1)).should_not be_valid | |
end | |
[1, rand(max_length)+1, max_length].each do |length| | |
it "can have a reasonably sized #{field}" do | |
new_object(field => 'a'*length).should be_valid | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment