[rspec-users] How much test data to use in specs

Zach Dennis zach.dennis at gmail.com
Mon Aug 25 13:29:53 EDT 2008


On Mon, Aug 25, 2008 at 12:53 PM, Nick Hoffman <nick at deadorange.com> wrote:
> I'm not sure how much test data I should be using in my specs. I'm writing
> specs for the Property model in my Rails app. Its "address" attribute is
> going to be validated with this regex:
> /\A\d+[a-z]? [-', a-z]{2,128}\Z/i
>
> At the moment, my plan is to spec out the following possibilities. A
> property is invalid if its address:
> 1) doesn't begin with a digit;
> 2) is shorter than 2 characters;
> 3) is longer than 128 characters;
>
> I should also test that certain characters, such as ! @ # $ etc, invalidate
> a property.
>
> However, all of that seems like blacklisting, and achieves poor coverage of
> the regular expression.
>
> Should I create a list of valid and invalid addresses to test against the
> regex? That seems like a decent idea, but seems synonymous with fixtures.
> What would you guys recommend?
>

I might do something like the following...

describe Property, "email validations" do

  ["1invalid.email at example.com", "can't start with a digit",
    "invalid.email2 at example.com", "can't end with a digit"
  ].in_groups_of(2) do |email, description|
    it description do
      prop =  Property.new :email => email
      prop.should_not be_valid
      prop.should have(1).error_on(:email)
    end
  end
end


-- 
Zach Dennis
http://www.continuousthinking.com
http://www.mutuallyhuman.com


More information about the rspec-users mailing list