Skip to content

Commit

Permalink
Make ActiveModel Tests work without relying on RSpec unstub!
Browse files Browse the repository at this point in the history
  • Loading branch information
mjonuschat committed Jun 18, 2010
1 parent 22f21ed commit 506182b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 93 deletions.
173 changes: 81 additions & 92 deletions spec/input_spec.rb
Expand Up @@ -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([
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 506182b

Please sign in to comment.