Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactoring ActiveRecord tests

  • Loading branch information...
commit 2aad0643cd9b2b9f20dceda35c045b5c25ce65bb 1 parent 42eaebc
Peter Brown authored
Showing with 32 additions and 46 deletions.
  1. +32 −13 spec/classy_enum/active_record_spec.rb
  2. +0 −33 spec/spec_helper.rb
45 spec/classy_enum/active_record_spec.rb
View
@@ -1,5 +1,19 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+ActiveRecord::Schema.define(:version => 1) do
+ create_table :dogs, :force => true do |t|
+ t.string :breed
+ t.string :other_breed
+ t.string :color
+ t.string :name
+ t.integer :age
+ end
+
+ create_table :cats, :force => true do |t|
+ t.string :breed
+ end
+end
+
class Breed < ClassyEnum::Base; end
class Breed::GoldenRetriever < Breed; end
class Breed::Snoop < Breed; end
@@ -22,29 +36,31 @@ class CatBreed::Bengal < CatBreed; end
class CatBreed::Birman < CatBreed; end
class CatBreed::Persian < CatBreed; end
-class Dog < ActiveRecord::Base
+class Dog < ActiveRecord::Base; end
+
+class DefaultDog < Dog
classy_enum_attr :breed
end
-class AllowBlankBreedDog < ActiveRecord::Base
+class AllowBlankBreedDog < Dog
classy_enum_attr :breed, :allow_blank => true
end
-class AllowNilBreedDog < ActiveRecord::Base
+class AllowNilBreedDog < Dog
classy_enum_attr :breed, :allow_nil => true
end
-class OtherDog < ActiveRecord::Base
+class OtherDog < Dog
classy_enum_attr :other_breed, :enum => 'Breed'
end
-describe Dog do
- specify { Dog.new(:breed => nil).should_not be_valid }
- specify { Dog.new(:breed => '').should_not be_valid }
+describe DefaultDog do
+ specify { DefaultDog.new(:breed => nil).should_not be_valid }
+ specify { DefaultDog.new(:breed => '').should_not be_valid }
context "with valid breed options" do
[:golden_retriever, 'golden_retriever', Breed::GoldenRetriever.new, Breed::GoldenRetriever].each do |option|
- subject { Dog.new(:breed => option) }
+ subject { DefaultDog.new(:breed => option) }
it { should be_valid }
its(:breed) { should be_a(Breed::GoldenRetriever) }
its('breed.allow_blank') { should be_false }
@@ -58,7 +74,7 @@ class OtherDog < ActiveRecord::Base
end
context "with invalid breed options" do
- subject { Dog.new(:breed => :fake_breed) }
+ subject { DefaultDog.new(:breed => :fake_breed) }
it { should_not be_valid }
it { should have(1).error_on(:breed) }
end
@@ -103,7 +119,7 @@ class OtherDog < ActiveRecord::Base
its(:other_breed) { should be_a(Breed::Snoop) }
end
-class ActiveDog < ActiveRecord::Base
+class ActiveDog < Dog
classy_enum_attr :color
validates_uniqueness_of :name, :scope => :color
scope :goldens, where(:breed => 'golden_retriever')
@@ -142,19 +158,22 @@ class ActiveDog < ActiveRecord::Base
end
class Cat < ActiveRecord::Base
+end
+
+class DefaultCat < Cat
classy_enum_attr :breed, :enum => 'CatBreed'
attr_accessor :color
delegate :breed_color, :to => :breed
end
-class OtherCat < ActiveRecord::Base
+class OtherCat < Cat
classy_enum_attr :breed, :enum => 'CatBreed', :serialize_as_json => true
attr_accessor :color
delegate :breed_color, :to => :breed
end
-describe Cat do
- let(:abyssian) { Cat.new(:breed => :abyssian, :color => 'black') }
+describe DefaultCat do
+ let(:abyssian) { DefaultCat.new(:breed => :abyssian, :color => 'black') }
let(:persian) { OtherCat.new(:breed => :persian, :color => 'white') }
it 'should delegate breed color to breed with an ownership reference' do
33 spec/spec_helper.rb
View
@@ -13,36 +13,3 @@
RSpec.configure do |config|
config.color_enabled = true
end
-
-ActiveRecord::Schema.define(:version => 1) do
- create_table :dogs, :force => true do |t|
- t.string :breed
- end
-
- create_table :other_dogs, :force => true do |t|
- t.string :other_breed
- end
-
- create_table :allow_blank_breed_dogs, :force => true do |t|
- t.string :breed
- end
-
- create_table :allow_nil_breed_dogs, :force => true do |t|
- t.string :breed
- end
-
- create_table :active_dogs, :force => true do |t|
- t.string :breed
- t.string :color
- t.string :name
- t.integer :age
- end
-
- create_table :cats, :force => true do |t|
- t.string :breed
- end
-
- create_table :other_cats, :force => true do |t|
- t.string :breed
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.