Skip to content

Instantly share code, notes, and snippets.

@mattetti
Created September 7, 2008 14:31
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 mattetti/9264 to your computer and use it in GitHub Desktop.
Save mattetti/9264 to your computer and use it in GitHub Desktop.
require File.join( File.dirname(__FILE__), '..', "spec_helper" )
describe Client do
before(:each) do
Client.all.destroy!
end
it "should have a valid factory" do
Factory.build(:client).should be_valid
end
it "should not be valid without a name" do
client = Factory.build(:client, :name => nil)
client.should_not be_valid
client.errors.on(:name).first.should == "Name must not be blank"
end
it "should not be valid with a name smaller than 3 letters" do
client = Factory.build(:client, :name => 'ab')
client.should_not be_valid
client.errors.length.should == 1
client.errors.on(:name).first.should == "Name must be between 3 and 150 characters long"
end
it "should validate the uniqueness of a client name" do
client_1 = Factory(:client)
client_2 = Factory.build(:client, :email => 'test@test.com')
client_2.should_not be_valid
client_2.errors.length.should == 1
client_2.errors.on(:name).first.should == "Name is already taken"
end
it "should not be valid with a bad email address" do
client = Factory.build(:client, :email => 'test')
client.should_not be_valid
client.errors.length.should == 1
client.errors.on(:email).first.should == "Email has an invalid format"
Factory.build(:client, :email => "dude@ example dot com").should_not be_valid
Factory.build(:client, :email => "dude@ example . com").should_not be_valid
Factory.build(:client, :email => "dude@example.com ;)").should_not be_valid
end
it "should validate the uniqueness of an email address" do
client_1 = Factory(:client)
client_2 = Factory.build(:client, :name => 'client_2', :email => 'john.doe@email.com')
client_1.email.should == client_2.email
client_2.should_not be_valid
client_2.errors.length.should == 1
client_2.errors.on(:email).first.should == "Email is already taken"
end
it "should auto timestamp a saved model instance" do
client = Factory(:client, :created_at => nil)
client.created_at.should_not be_nil
client.created_at.should be_an_instance_of(DateTime)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment