Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make ActiveModel Tests work without relying on RSpec unstub!

  • Loading branch information...
commit 506182bcdb33637b53a8793bc33eff5abe53ba4c 1 parent 22f21ed
@yabawock yabawock authored
Showing with 88 additions and 93 deletions.
  1. +81 −92 spec/input_spec.rb
  2. +7 −1 spec/spec_helper.rb
View
173 spec/input_spec.rb
@@ -136,13 +136,6 @@
@new_post.class.stub!(:method_defined?).with(:reflect_on_validations_for).and_return(true)
end
- unless defined?(RSpec)
- after do
- ::Post.unstub!(:reflect_on_validations_for)
- @new_post.class.stub!(:method_defined?).with(:reflect_on_validations_for).and_return(false)
- end
- end
-
describe 'and validates_presence_of was called for the method' do
it 'should be required' do
@new_post.class.should_receive(:reflect_on_validations_for).with(:title).and_return([
@@ -240,115 +233,111 @@ def should_be_required(options)
end
- unless defined?(RSpec)
- describe 'and its a ActiveModel' do
- before do
- ::Post.stub!(:validators_on).and_return([])
- @new_post.class.stub!(:method_defined?).with(:validators_on).and_return(true)
- end
-
- after do
- ::Post.unstub!(:validators_on)
- @new_post.class.stub!(:method_defined?).with(:validators_on).and_return(false)
- end
- describe 'and validates_presence_of was called for the method' do
- it 'should be required' do
+ describe 'and its a ActiveModel' do
+ before do
+ @new_post.stub!(:class).and_return(::PostModel)
+ end
- @new_post.class.should_receive(:validators_on).with(:title).and_return([
- active_model_presence_validator([:title])
- ])
+ after do
+ @new_post.stub!(:class).and_return(::Post)
+ end
+ describe 'and validates_presence_of was called for the method' do
+ it 'should be required' do
- @new_post.class.should_receive(:validators_on).with(:body).and_return([
- active_model_presence_validator([:body], {:if => true})
- ])
+ @new_post.class.should_receive(:validators_on).with(:title).and_return([
+ active_model_presence_validator([:title])
+ ])
- form = semantic_form_for(@new_post) do |builder|
- concat(builder.input(:title))
- concat(builder.input(:body))
- end
- output_buffer.concat(form) if Formtastic::Util.rails3?
- output_buffer.should have_tag('form li.required')
- output_buffer.should_not have_tag('form li.optional')
- end
+ @new_post.class.should_receive(:validators_on).with(:body).and_return([
+ active_model_presence_validator([:body], {:if => true})
+ ])
- it 'should be not be required if the optional :if condition is not satisifed' do
- should_be_required(:required => false, :options => { :if => false })
+ form = semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:title))
+ concat(builder.input(:body))
end
+ output_buffer.concat(form) if Formtastic::Util.rails3?
+ output_buffer.should have_tag('form li.required')
+ output_buffer.should_not have_tag('form li.optional')
+ end
- it 'should not be required if the optional :if proc evaluates to false' do
- should_be_required(:required => false, :options => { :if => proc { |record| false } })
- end
+ it 'should be not be required if the optional :if condition is not satisifed' do
+ should_be_required(:required => false, :options => { :if => false })
+ end
- it 'should be required if the optional :if proc evaluates to true' do
- should_be_required(:required => true, :options => { :if => proc { |record| true } })
- end
+ it 'should not be required if the optional :if proc evaluates to false' do
+ should_be_required(:required => false, :options => { :if => proc { |record| false } })
+ end
- it 'should not be required if the optional :unless proc evaluates to true' do
- should_be_required(:required => false, :options => { :unless => proc { |record| true } })
- end
+ it 'should be required if the optional :if proc evaluates to true' do
+ should_be_required(:required => true, :options => { :if => proc { |record| true } })
+ end
- it 'should be required if the optional :unless proc evaluates to false' do
- should_be_required(:required => true, :options => { :unless => proc { |record| false } })
- end
+ it 'should not be required if the optional :unless proc evaluates to true' do
+ should_be_required(:required => false, :options => { :unless => proc { |record| true } })
+ end
- it 'should be required if the optional :if with a method string evaluates to true' do
- @new_post.should_receive(:required_condition).and_return(true)
- should_be_required(:required => true, :options => { :if => :required_condition })
- end
+ it 'should be required if the optional :unless proc evaluates to false' do
+ should_be_required(:required => true, :options => { :unless => proc { |record| false } })
+ end
- it 'should be required if the optional :if with a method string evaluates to false' do
- @new_post.should_receive(:required_condition).and_return(false)
- should_be_required(:required => false, :options => { :if => :required_condition })
- end
+ it 'should be required if the optional :if with a method string evaluates to true' do
+ @new_post.should_receive(:required_condition).and_return(true)
+ should_be_required(:required => true, :options => { :if => :required_condition })
+ end
- it 'should not be required if the optional :unless with a method string evaluates to false' do
- @new_post.should_receive(:required_condition).and_return(false)
- should_be_required(:required => true, :options => { :unless => :required_condition })
- end
+ it 'should be required if the optional :if with a method string evaluates to false' do
+ @new_post.should_receive(:required_condition).and_return(false)
+ should_be_required(:required => false, :options => { :if => :required_condition })
+ end
- it 'should be required if the optional :unless with a method string evaluates to true' do
- @new_post.should_receive(:required_condition).and_return(true)
- should_be_required(:required => false, :options => { :unless => :required_condition })
- end
+ it 'should not be required if the optional :unless with a method string evaluates to false' do
+ @new_post.should_receive(:required_condition).and_return(false)
+ should_be_required(:required => true, :options => { :unless => :required_condition })
end
- # TODO make a matcher for this?
- def should_be_required(options)
- @new_post.class.should_receive(:validators_on).with(:body).and_return([
- active_model_presence_validator([:body], options[:options])
- ])
+ it 'should be required if the optional :unless with a method string evaluates to true' do
+ @new_post.should_receive(:required_condition).and_return(true)
+ should_be_required(:required => false, :options => { :unless => :required_condition })
+ end
+ end
- form = semantic_form_for(@new_post) do |builder|
- concat(builder.input(:body))
- end
+ # TODO make a matcher for this?
+ def should_be_required(options)
+ @new_post.class.should_receive(:validators_on).with(:body).and_return([
+ active_model_presence_validator([:body], options[:options])
+ ])
- output_buffer.concat(form) if Formtastic::Util.rails3?
+ form = semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:body))
+ end
- if options[:required]
- output_buffer.should_not have_tag('form li.optional')
- output_buffer.should have_tag('form li.required')
- else
- output_buffer.should have_tag('form li.optional')
- output_buffer.should_not have_tag('form li.required')
- end
+ output_buffer.concat(form) if Formtastic::Util.rails3?
+
+ if options[:required]
+ output_buffer.should_not have_tag('form li.optional')
+ output_buffer.should have_tag('form li.required')
+ else
+ output_buffer.should have_tag('form li.optional')
+ output_buffer.should_not have_tag('form li.required')
end
+ end
- describe 'and validates_presence_of was not called for the method' do
- before do
- @new_post.class.should_receive(:validators_on).with(:title).and_return([])
- end
+ describe 'and validates_presence_of was not called for the method' do
+ before do
+ @new_post.class.should_receive(:validators_on).with(:title).and_return([])
+ end
- it 'should not be required' do
- form = semantic_form_for(@new_post) do |builder|
- concat(builder.input(:title))
- end
- output_buffer.concat(form) if Formtastic::Util.rails3?
- output_buffer.should_not have_tag('form li.required')
- output_buffer.should have_tag('form li.optional')
+ it 'should not be required' do
+ form = semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:title))
end
+ output_buffer.concat(form) if Formtastic::Util.rails3?
+ output_buffer.should_not have_tag('form li.required')
+ output_buffer.should have_tag('form li.optional')
end
-
end
+
end
describe 'and the validation reflection plugin is not available' do
View
8 spec/spec_helper.rb
@@ -91,11 +91,17 @@ class ::Continent
extend ActiveModel::Naming if defined?(ActiveModel::Naming)
include ActiveModel::Conversion if defined?(ActiveModel::Conversion)
end
+ class ::PostModel
+ extend ActiveModel::Naming if defined?(ActiveModel::Naming)
+ include ActiveModel::Conversion if defined?(ActiveModel::Conversion)
+ end
def mock_everything
# Resource-oriented styles like form_for(@post) will expect a path method for the object,
# so we're defining some here.
+ def post_models_path; "/postmodels/1"; end
+
def post_path(o); "/posts/1"; end
def posts_path; "/posts"; end
def new_post_path; "/posts/new"; end
@@ -180,7 +186,7 @@ def new_author_path; "/authors/new"; end
::Post.stub!(:human_attribute_name).and_return { |column_name| column_name.humanize }
::Post.stub!(:human_name).and_return('Post')
::Post.stub!(:reflect_on_all_validations).and_return([])
- ::Post.stub!(:reflect_on_validations_for).and_return([]) if defined?(RSpec)
+ ::Post.stub!(:reflect_on_validations_for).and_return([])
::Post.stub!(:reflections).and_return({})
::Post.stub!(:reflect_on_association).and_return do |column_name|
case column_name
Please sign in to comment.
Something went wrong with that request. Please try again.