spec/draper/base_spec.rb in draper-0.15.0 vs spec/draper/base_spec.rb in draper-0.16.0

- old
+ new

@@ -1,9 +1,9 @@ require 'spec_helper' describe Draper::Base do - before(:each){ ApplicationController.new.set_current_view_context } + before(:each){ ApplicationController.new.view_context } subject{ Decorator.new(source) } let(:source){ Product.new } let(:non_active_model_source){ NonActiveModelProduct.new } context("proxying class methods") do @@ -55,48 +55,48 @@ expect do class BusinessDecorator < Draper::Base decorates:business end BusinessDecorator.model_class.should == Business - end.should_not raise_error + end.to_not raise_error end context("accepts ActiveRecord like :class_name option too") do it "accepts constants for :class" do expect do class CustomDecorator < Draper::Base decorates :product, :class => Product end CustomDecorator.model_class.should == Product - end.should_not raise_error + end.to_not raise_error end it "accepts constants for :class_name" do expect do class CustomDecorator < Draper::Base decorates :product, :class_name => Product end CustomDecorator.model_class.should == Product - end.should_not raise_error + end.to_not raise_error end it "accepts strings for :class" do expect do class CustomDecorator < Draper::Base decorates :product, :class => 'Product' end CustomDecorator.model_class.should == Product - end.should_not raise_error + end.to_not raise_error end it "accepts strings for :class_name" do expect do class CustomDecorator < Draper::Base decorates :product, :class_name => 'Product' end CustomDecorator.model_class.should == Product - end.should_not raise_error + end.to_not raise_error end end it "creates a named accessor for the wrapped model" do pd = ProductDecorator.new(source) @@ -181,15 +181,22 @@ end context('.decorates_associations') do subject { Decorator } it "decorates each of the associations" do - subject.should_receive(:decorates_association).with(:similar_products) - subject.should_receive(:decorates_association).with(:previous_version) + subject.should_receive(:decorates_association).with(:similar_products, {}) + subject.should_receive(:decorates_association).with(:previous_version, {}) subject.decorates_associations :similar_products, :previous_version end + + it "dispatches options" do + subject.should_receive(:decorates_association).with(:similar_products, :with => ProductDecorator) + subject.should_receive(:decorates_association).with(:previous_version, :with => ProductDecorator) + + subject.decorates_associations :similar_products, :previous_version, :with => ProductDecorator + end end context(".wrapped_object") do it "return the wrapped object" do subject.wrapped_object.should == source @@ -249,21 +256,10 @@ it "never proxy errors if it is defined on the decorator itself" do DecoratorWithSpecialMethods.new(source).errors.should be_an_instance_of Array end end - - context "when not an ActiveModel descendant" do - it "does not proxy to_param" do - non_active_model_source.stub(:to_param).and_return(1) - Draper::Base.new(non_active_model_source).to_param.should_not == 1 - end - - it "does not proxy errors" do - Draper::Base.new(non_active_model_source).should_not respond_to :errors - end - end end context 'the decorated model' do it 'receives the mixin' do source.class.ancestors.include?(Draper::ModelSupport) @@ -361,10 +357,19 @@ subject.context = :admin subject.each { |decorated| decorated.context.should == :admin } end end + context "when given a collection of sequel models" do + # Sequel models implement #each + let(:source) { [SequelProduct.new, SequelProduct.new] } + + it "returns a collection of wrapped objects" do + subject.each{ |decorated| decorated.should be_instance_of(Draper::Base) } + end + end + context "when given a single source object" do let(:source) { Product.new } it { should be_instance_of(Draper::Base) } @@ -466,14 +471,17 @@ subject.should == other end end context ".respond_to?" do + # respond_to? is called by some proxies (id, to_param, errors). + # This is, why I stub it this way. it "delegate respond_to? to the decorated model" do other = Draper::Base.new(source) - source.should_receive(:respond_to?).with(:whatever, true) - subject.respond_to?(:whatever, true) + source.stub(:respond_to?).and_return(false) + source.stub(:respond_to?).with(:whatever, true).once.and_return("mocked") + subject.respond_to?(:whatever, true).should == "mocked" end end context 'position accessors' do [:first, :last].each do |method| @@ -667,23 +675,23 @@ allows :hello_world end } it "raise an exception for a blank allows" do - expect {blank_allows}.should raise_error(ArgumentError) + expect {blank_allows}.to raise_error(ArgumentError) end it "raise an exception for a blank denies" do - expect {blank_denies}.should raise_error(ArgumentError) + expect {blank_denies}.to raise_error(ArgumentError) end it "raise an exception for calling allows then denies" do - expect {using_allows_then_denies}.should raise_error(ArgumentError) + expect {using_allows_then_denies}.to raise_error(ArgumentError) end it "raise an exception for calling denies then allows" do - expect {using_denies_then_allows}.should raise_error(ArgumentError) + expect {using_denies_then_allows}.to raise_error(ArgumentError) end end context "in a Rails application" do let(:decorator){ DecoratorWithApplicationHelper.decorate(Object.new) } @@ -704,11 +712,15 @@ decorator.sample_truncate.should == "Once..." end it "is able to use l rather than helpers.l" do now = Time.now - decorator.helpers.should_receive(:localize).with(now) + decorator.helpers.instance_variable_get(:@helpers).should_receive(:localize).with(now) decorator.l now + end + + it "is able to access html_escape, a private method" do + decorator.sample_html_escaped_text.should == '&lt;script&gt;danger&lt;/script&gt;' end end context "#method_missing" do context "with an isolated decorator class" do