Permalink
Browse files

More testing and validation improvements

  • Loading branch information...
1 parent 2f64a24 commit 699e60cbb3571c75d0913b4734c88cf0f29fefe5 @ddemaree committed Nov 6, 2011
Showing with 17 additions and 9 deletions.
  1. +10 −5 spec/party_spec.rb
  2. +3 −0 spec/spec_helper.rb
  3. +4 −4 thirty_one.rb
View
@@ -1,21 +1,26 @@
require 'spec_helper'
describe ThirtyOne::Party do
-
- before { ThirtyOne::Party.delete_all; ThirtyOne::PartyBit.delete_all }
+ before { DatabaseCleaner.start }
+ after { DatabaseCleaner.clean }
it "generates a unique code on create" do
ThirtyOne::RandomPhrase.should_receive(:generate).and_return("3-saucy-calamari")
- new_party = ThirtyOne::Party.new
- new_party.name = "David Demaree"
- new_party.email = "david@demaree.me"
+ new_party = FactoryGirl.build(:party)
+ new_party.unique_phrase.should be_blank
new_party.save
new_party.unique_phrase.should be_present
new_party.unique_phrase.should == "3-saucy-calamari"
end
+ it "validates at least one bit is a datetime" do
+ new_party = FactoryGirl.build(:party, bits: ["dinner.longman_and_eagle"])
+ new_party.should_not be_valid
+ new_party.errors[:base].should include("You have to choose at least one time when you're available to par-tay")
+ end
+
it "uses the unique phrase as its URL param" do
party = ThirtyOne::Party.create(name: "David", email: "david@demaree.me")
party.to_param.should == party.unique_phrase
View
@@ -4,6 +4,9 @@
require "rack/test"
require 'active_support/core_ext/kernel'
+require 'database_cleaner'
+DatabaseCleaner.strategy = :transaction
+
schema_file = ENV['SCHEMA'] || "#{APP_ROOT}/db/schema.rb"
if File.exists?(schema_file)
load(schema_file)
View
@@ -105,8 +105,9 @@ class Model < ::ActiveRecord::Base
class Party < Model
has_many :party_bits
- validate :name, presence: true
- validate :email, presence: true
+ validates :name, presence: true
+ validates :email, presence: true
+ validate :ensure_bits_include_time
def bits
@bits ||= get_bits!
@@ -126,8 +127,7 @@ def to_param
unique_phrase || id
end
- before_validation :ensure_datetime_bits
- def ensure_datetime_bits
+ def ensure_bits_include_time
if !bits.any? { |b| b =~ /^datetime/ }
errors[:base] << "You have to choose at least one time when you're available to par-tay"
end

0 comments on commit 699e60c

Please sign in to comment.