Skip to content

Commit

Permalink
Added specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Hunt committed Aug 19, 2008
1 parent 6373b94 commit 14dd301
Show file tree
Hide file tree
Showing 7 changed files with 322 additions and 16 deletions.
20 changes: 12 additions & 8 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# check for RSpec plugin
rspec_path = File.join(%W(#{File.dirname(__FILE__)} .. rspec lib))
$:.unshift(rspec_path) if File.exists?(rspec_path) && !$:.include?(rspec_path)

require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'spec/rake/spectask'

desc 'Default: run unit tests.'
task :default => :test
desc 'Default: run the specs.'
task :default => :spec

desc 'Test the germinate plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
desc 'Run the specs for the germinate plugin.'
Spec::Rake::SpecTask.new(:spec) do |t|
options_path = File.join(%W(#{File.dirname(__FILE__)} spec spec.opts))
t.spec_opts = ['--options', %("#{options_path}")]
t.spec_files = FileList['spec/**/*_spec.rb']
end

desc 'Generate documentation for the germinate plugin.'
Expand Down
35 changes: 35 additions & 0 deletions spec/germinate/factory_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require File.join(%W(#{File.dirname(__FILE__)} .. spec_helper))

describe Germinate::Factory do
before(:each) do
@methods = [:id, :name, :city_and_state]
@seed = mock('seed')
@seed.stub!(:records).and_return([])
@factory = Germinate::Factory.new(@seed, :create)
@record = mock('record')
Germinate::Record.stub!(:new).and_return(@record)
end

it "should respond to any arbitrary method call with a new record" do
@methods.each do |method|
@factory.send(method).should == @record
end
end

it "should create new records using the invoked method as the key" do
@methods.each do |method|
Germinate::Record.should_receive(:new).with(@seed, :create, method).and_return(@record)
@factory.send(method)
end
end

it "should add each generated record to the records set on the seed" do
records = mock('records')
records.should_receive(:<<).exactly(@methods.length).with(@record)
@seed.should_receive(:records).exactly(@methods.length).and_return(records)

@methods.each do |method|
@factory.send(method)
end
end
end
188 changes: 188 additions & 0 deletions spec/germinate/record_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
require File.join(%W(#{File.dirname(__FILE__)} .. spec_helper))

describe Germinate::Record do
before(:each) do
@record = Germinate::Record.new(UserSeed, :create, :id, 1)
end

it "should define the available action methods" do
[:create, :update, :create_or_update].each do |method|
Germinate::Record::METHODS.should include(method)
end
end

it "should privately respond to all of the specified action methods" do
Germinate::Record::METHODS.each do |method|
@record.private_methods.should include(method.to_s)
end
end

it "should allow additional attributes to be specified and return self" do
@record.instance_variable_get('@attributes').should be_nil
@record.with(attributes = { :name => 'Test' }).should == @record
@record.instance_variable_get('@attributes').should == attributes
end

it "should have an execute method that invokes the record's method" do
@record.should_receive(:send).with(:create)
@record.execute
end

describe "actions" do
before(:each) do
@user = mock('user')
@user.stub!(:id=)
@user.stub!(:attributes=)
@user.stub!(:save!)
end

describe "an action in general", :shared => true do
it "should save the record" do
@user.should_receive(:save!)
end

it "should set the key on the new record" do
@user.should_receive(:id=).with(1)
end

it "should set the attributes on the new record" do
@user.should_receive(:attributes=).with(nil)
end
end

describe "create" do
describe "with success" do
before(:each) do
User.stub!(:find_by_id).and_return(nil)
User.stub!(:new).and_return(@user)
end

it_should_behave_like "an action in general"

it "should check for an existing record" do
User.should_receive(:find_by_id).with(1).and_return(nil)
end

it "should create a new record" do
User.should_receive(:new).and_return(@user)
end

it "should save the new record" do
@user.should_receive(:save!)
end

it "should return the new record" do
@after = lambda { |record| record.should == @user }
end

it "should set the status to :created" do
@after = lambda { @record.status.should == :created }
end

after(:each) do
record = @record.send(:create)
@after.call(record) if @after
end
end

it "should raise ActiveRecord::RecordNotSaved if the record exists" do
User.should_receive(:find_by_id).with(1).and_return(@user)
lambda { @record.send(:create) }.should raise_error(ActiveRecord::RecordNotSaved)
end
end

describe "update" do
describe "with success" do
before(:each) do
User.stub!(:find_by_id).and_return(@user)
end

it_should_behave_like "an action in general"

it "should find an existing record" do
User.should_receive(:find_by_id).with(1).and_return(@user)
end

it "should not create a new record" do
User.should_not_receive(:new)
end

it "should save the updated record" do
@user.should_receive(:save!)
end

it "should return the updated record" do
@after = lambda { |record| record.should == @user }
end

it "should set the status to :update" do
@after = lambda { @record.status.should == :updated }
end

after(:each) do
record = @record.send(:update)
@after.call(record) if @after
end
end

it "should raise ActiveRecord::RecordNotFound if the record doesn't exist" do
User.should_receive(:find_by_id).with(1).and_return(nil)
lambda { @record.send(:update) }.should raise_error(ActiveRecord::RecordNotFound)
end
end

describe "create or update" do
before(:each) do
User.stub!(:find_by_id).and_return(@user)
end

it_should_behave_like "an action in general"

describe "with successful update" do
it "should try to update the record" do
@record.should_receive(:update)
@record.should_not_receive(:create)
end

it "should return the updated record" do
@after = lambda { |record| record.should == @user }
end

it "should set the status to :updated" do
@after = lambda { @record.status.should == :updated }
end
end

describe "with failed update" do
before(:each) do
User.stub!(:find_by_id).and_return(nil)
User.stub!(:new).and_return(@user)
@record.should_receive(:update).and_raise(ActiveRecord::RecordNotFound)
end

it "should try to create the record" do
@record.should_receive(:create)
end

it "should return the created record" do
@after = lambda { |record| record.should == @user }
end

it "should set the status to :created" do
@after = lambda { @record.status.should == :created }
end
end

after(:each) do
record = @record.send(:create_or_update)
@after.call(record) if @after
end
end
end

describe "update" do
end

describe "create or update" do
end
end
72 changes: 72 additions & 0 deletions spec/germinate/seed_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require File.join(%W(#{File.dirname(__FILE__)} .. spec_helper))

describe Germinate::Seed do
it "should have an attribute accessor for the model" do
Germinate::Seed.should respond_to(:model, :model=)
end

it "should have an attribute accessor for records" do
Germinate::Seed.should respond_to(:records, :records=)
end

it "should have an attribute accessor for statuses" do
Germinate::Seed.should respond_to(:statuses, :statuses=)
end

it "should have an attribute reader for the loaded seeds" do
Germinate::Seed.should respond_to(:seeds)
end

it "should return a factory if it receives a method defined by Record" do
Germinate::Record.send(:remove_const, :METHODS)
Germinate::Record::METHODS = [:create]
factory = mock('factory')
Germinate::Factory.should_receive(:new).with(UserSeed, :create).and_return(factory)
UserSeed.create do |user|
user.should == factory
end
end

describe "implementation" do
it "should set the model attribute to the underlying model" do
UserSeed.should_receive(:model=).with(User)
Germinate::Seed.inherited(UserSeed)
end

it "should initialize the records set with an empty array" do
UserSeed.should_receive(:records=).with([])
Germinate::Seed.inherited(UserSeed)
end

it "should initialize the statuses set with an empty hash" do
UserSeed.should_receive(:statuses=).with({})
Germinate::Seed.inherited(UserSeed)
end

it "should add itself to the seeds set" do
Germinate::Seed.seeds.should include(UserSeed)
end

it "should keep the seeds list unique" do
seeds = mock('seeds')
seeds.should_receive(:<<)
seeds.should_receive(:uniq!)
Germinate::Seed.instance_variable_set('@seeds', seeds)
Germinate::Seed.inherited(UserSeed)
end

describe "accessors" do
it "should provide access to the underlying model" do
UserSeed.model.should == User
end

it "should provide access to the records set" do
UserSeed.records.should be_empty
end

it "should provide access to the statuses set" do
UserSeed.statuses.should be_empty
end
end
end
end
2 changes: 2 additions & 0 deletions spec/spec.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--colour
--format progress
13 changes: 13 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ENV["RAILS_ENV"] ||= "test"

require File.join(%W(#{File.dirname(__FILE__)} .. .. .. .. config environment))
require File.join(%W(#{File.dirname(__FILE__)} .. lib germinate))

Spec::Runner.configure do |config|
class User
end

class UserSeed < Germinate::Seed
end
end

8 changes: 0 additions & 8 deletions test/germinate_test.rb

This file was deleted.

0 comments on commit 14dd301

Please sign in to comment.