Skip to content

Commit

Permalink
Refactoring ActiveRecord tests
Browse files Browse the repository at this point in the history
  • Loading branch information
beerlington committed Feb 8, 2013
1 parent 42eaebc commit 2aad064
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 46 deletions.
45 changes: 32 additions & 13 deletions spec/classy_enum/active_record_spec.rb
@@ -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
Expand All @@ -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 }
Expand All @@ -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
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down
33 changes: 0 additions & 33 deletions spec/spec_helper.rb
Expand Up @@ -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

0 comments on commit 2aad064

Please sign in to comment.