From 036be08ded836504f1cd4c94d689928b14b1121d Mon Sep 17 00:00:00 2001 From: Matt Casper Date: Thu, 21 May 2015 09:53:15 -0700 Subject: [PATCH 01/31] Update the rest of the specs to rspec3 syntax --- spec/draper/collection_decorator_spec.rb | 18 ++--- spec/draper/decoratable_spec.rb | 20 +++--- spec/draper/decorated_association_spec.rb | 12 ++-- spec/draper/decorates_assigned_spec.rb | 8 +-- spec/draper/decorator_spec.rb | 72 +++++++++---------- spec/draper/factory_spec.rb | 46 ++++++------ spec/draper/finders_spec.rb | 42 +++++------ spec/draper/helper_proxy_spec.rb | 4 +- spec/draper/lazy_helpers_spec.rb | 4 +- .../view_context/build_strategy_spec.rb | 8 +-- spec/draper/view_context_spec.rb | 42 +++++------ .../controller/controller_generator_spec.rb | 2 +- .../decorator/decorator_generator_spec.rb | 18 ++--- spec/support/shared_examples/view_helpers.rb | 16 ++--- 14 files changed, 156 insertions(+), 156 deletions(-) diff --git a/spec/draper/collection_decorator_spec.rb b/spec/draper/collection_decorator_spec.rb index d2e256ca..39e3e387 100644 --- a/spec/draper/collection_decorator_spec.rb +++ b/spec/draper/collection_decorator_spec.rb @@ -63,7 +63,7 @@ module Draper it "does not trigger decoration" do decorator = CollectionDecorator.new([]) - decorator.should_not_receive(:decorated_collection) + expect(decorator).not_to receive(:decorated_collection) decorator.context = {other: "context"} end @@ -110,12 +110,12 @@ module Draper protect_class ProductsDecorator it "defaults the :to option to :object" do - Object.should_receive(:delegate).with(:foo, :bar, to: :object) + expect(Object).to receive(:delegate).with(:foo, :bar, to: :object) ProductsDecorator.delegate :foo, :bar end it "does not overwrite the :to option if supplied" do - Object.should_receive(:delegate).with(:foo, :bar, to: :baz) + expect(Object).to receive(:delegate).with(:foo, :bar, to: :baz) ProductsDecorator.delegate :foo, :bar, to: :baz end end @@ -125,7 +125,7 @@ module Draper it "decorates Enumerable#find" do decorator = CollectionDecorator.new([]) - decorator.decorated_collection.should_receive(:find).and_return(:delegated) + expect(decorator.decorated_collection).to receive(:find).and_return(:delegated) expect(decorator.find{|p| p.title == "title"}).to be :delegated end end @@ -136,7 +136,7 @@ module Draper found = double(decorate: :decorated) decorator = CollectionDecorator.new(object) - object.should_receive(:find).and_return(found) + expect(object).to receive(:find).and_return(found) ActiveSupport::Deprecation.silence do expect(decorator.find(1)).to be :decorated end @@ -149,7 +149,7 @@ module Draper it "delegates to the decorated collection" do decorator = CollectionDecorator.new([]) - decorator.decorated_collection.should_receive(:to_ary).and_return(:delegated) + expect(decorator.decorated_collection).to receive(:to_ary).and_return(:delegated) expect(decorator.to_ary).to be :delegated end end @@ -157,7 +157,7 @@ module Draper it "delegates array methods to the decorated collection" do decorator = CollectionDecorator.new([]) - decorator.decorated_collection.should_receive(:[]).with(42).and_return(:delegated) + expect(decorator.decorated_collection).to receive(:[]).with(42).and_return(:delegated) expect(decorator[42]).to be :delegated end @@ -267,14 +267,14 @@ module Draper describe '#kind_of?' do it 'asks the kind of its decorated collection' do decorator = ProductsDecorator.new([]) - decorator.decorated_collection.should_receive(:kind_of?).with(Array).and_return("true") + expect(decorator.decorated_collection).to receive(:kind_of?).with(Array).and_return("true") expect(decorator.kind_of?(Array)).to eq "true" end context 'when asking the underlying collection returns false' do it 'asks the CollectionDecorator instance itself' do decorator = ProductsDecorator.new([]) - decorator.decorated_collection.stub(:kind_of?).with(::Draper::CollectionDecorator).and_return(false) + allow(decorator.decorated_collection).to receive(:kind_of?).with(::Draper::CollectionDecorator).and_return(false) expect(decorator.kind_of?(::Draper::CollectionDecorator)).to be true end end diff --git a/spec/draper/decoratable_spec.rb b/spec/draper/decoratable_spec.rb index 6f608f46..b6de4b95 100644 --- a/spec/draper/decoratable_spec.rb +++ b/spec/draper/decoratable_spec.rb @@ -22,7 +22,7 @@ module Draper it "uses the #decorator_class" do product = Product.new - product.stub decorator_class: OtherDecorator + allow(product).to receive_messages decorator_class: OtherDecorator expect(product.decorate).to be_an_instance_of OtherDecorator end @@ -70,7 +70,7 @@ module Draper it "delegates to .decorator_class" do product = Product.new - Product.should_receive(:decorator_class).and_return(:some_decorator) + expect(Product).to receive(:decorator_class).and_return(:some_decorator) expect(product.decorator_class).to be :some_decorator end end @@ -83,14 +83,14 @@ module Draper it "is true when #== is true" do product = Product.new - product.should_receive(:==).and_return(true) + expect(product).to receive(:==).and_return(true) expect(product === :anything).to be_truthy end it "is false when #== is false" do product = Product.new - product.should_receive(:==).and_return(false) + expect(product).to receive(:==).and_return(false) expect(product === :anything).to be_falsey end end @@ -132,17 +132,17 @@ module Draper it "calls #decorate_collection on .decorator_class" do scoped = [Product.new] - Product.stub scoping_method => scoped + allow(Product).to receive_messages scoping_method => scoped - Product.decorator_class.should_receive(:decorate_collection).with(scoped, with: nil).and_return(:decorated_collection) + expect(Product.decorator_class).to receive(:decorate_collection).with(scoped, with: nil).and_return(:decorated_collection) expect(Product.decorate).to be :decorated_collection end it "accepts options" do options = {with: ProductDecorator, context: {some: "context"}} - Product.stub scoping_method => [] + allow(Product).to receive_messages scoping_method => [] - Product.decorator_class.should_receive(:decorate_collection).with([], options) + expect(Product.decorator_class).to receive(:decorate_collection).with([], options) Product.decorate(options) end end @@ -177,7 +177,7 @@ module Draper context "for ActiveModel classes" do it "infers the decorator from the model name" do - Namespaced::Product.stub(:model_name).and_return("Namespaced::Other") + allow(Namespaced::Product).to receive(:model_name).and_return("Namespaced::Other") expect(Namespaced::Product.decorator_class).to be Namespaced::OtherDecorator end @@ -192,7 +192,7 @@ module Draper context "when an unrelated NameError is thrown" do it "re-raises that error" do - String.any_instance.stub(:constantize) { Draper::Base } + allow_any_instance_of(String).to receive(:constantize) { Draper::Base } expect{Product.decorator_class}.to raise_error NameError, /Draper::Base/ end end diff --git a/spec/draper/decorated_association_spec.rb b/spec/draper/decorated_association_spec.rb index 3bd2de68..daf35324 100644 --- a/spec/draper/decorated_association_spec.rb +++ b/spec/draper/decorated_association_spec.rb @@ -16,20 +16,20 @@ module Draper it "creates a factory" do options = {with: Decorator, context: {foo: "bar"}} - Factory.should_receive(:new).with(options) + expect(Factory).to receive(:new).with(options) DecoratedAssociation.new(double, :association, options) end describe ":with option" do it "defaults to nil" do - Factory.should_receive(:new).with(with: nil, context: anything()) + expect(Factory).to receive(:new).with(with: nil, context: anything()) DecoratedAssociation.new(double, :association, {}) end end describe ":context option" do it "defaults to the identity function" do - Factory.should_receive(:new) do |options| + expect(Factory).to receive(:new) do |options| options[:context].call(:anything) == :anything end DecoratedAssociation.new(double, :association, {}) @@ -48,7 +48,7 @@ module Draper decorated_association = DecoratedAssociation.new(owner, :association, {}) decorated = double - factory.should_receive(:decorate).with(associated, context_args: owner_context).and_return(decorated) + expect(factory).to receive(:decorate).with(associated, context_args: owner_context).and_return(decorated) expect(decorated_association.call).to be decorated end @@ -59,7 +59,7 @@ module Draper decorated_association = DecoratedAssociation.new(owner, :association, {}) decorated = double - factory.should_receive(:decorate).once.and_return(decorated) + expect(factory).to receive(:decorate).once.and_return(decorated) expect(decorated_association.call).to be decorated expect(decorated_association.call).to be decorated end @@ -74,7 +74,7 @@ module Draper decorated_association = DecoratedAssociation.new(owner, :association, scope: :applied_scope) decorated = double - factory.should_receive(:decorate).with(scoped, anything()).and_return(decorated) + expect(factory).to receive(:decorate).with(scoped, anything()).and_return(decorated) expect(decorated_association.call).to be decorated end end diff --git a/spec/draper/decorates_assigned_spec.rb b/spec/draper/decorates_assigned_spec.rb index 9694ca53..2ac20747 100644 --- a/spec/draper/decorates_assigned_spec.rb +++ b/spec/draper/decorates_assigned_spec.rb @@ -28,14 +28,14 @@ def self.helper_methods end it "creates a factory" do - Factory.should_receive(:new).once + expect(Factory).to receive(:new).once controller_class.decorates_assigned :article, :author end it "passes options to the factory" do options = {foo: "bar"} - Factory.should_receive(:new).with(options) + expect(Factory).to receive(:new).with(options) controller_class.decorates_assigned :article, :author, options end @@ -49,7 +49,7 @@ def self.helper_methods controller = controller_class.new controller.instance_variable_set "@article", object - factory.should_receive(:decorate).with(object, context_args: controller).and_return(:decorated) + expect(factory).to receive(:decorate).with(object, context_args: controller).and_return(:decorated) expect(controller.article).to be :decorated end @@ -60,7 +60,7 @@ def self.helper_methods controller_class.decorates_assigned :article controller = controller_class.new - factory.should_receive(:decorate).once + expect(factory).to receive(:decorate).once controller.article controller.article end diff --git a/spec/draper/decorator_spec.rb b/spec/draper/decorator_spec.rb index 36eb83eb..b1af6b92 100755 --- a/spec/draper/decorator_spec.rb +++ b/spec/draper/decorator_spec.rb @@ -73,7 +73,7 @@ module Draper decorated = OtherDecorator.new(Decorator.new(Model.new)) warning_message = nil - Object.any_instance.stub(:warn) { |instance, message| warning_message = message } + allow_any_instance_of(Object).to receive(:warn) { |instance, message| warning_message = message } expect{Decorator.new(decorated)}.to change{warning_message} expect(warning_message).to start_with "Reapplying Draper::Decorator" @@ -82,7 +82,7 @@ module Draper it "decorates anyway" do decorated = OtherDecorator.new(Decorator.new(Model.new)) - Object.any_instance.stub(:warn) + allow_any_instance_of(Object).to receive(:warn) redecorated = Decorator.decorate(decorated) expect(redecorated.object).to be decorated @@ -102,7 +102,7 @@ module Draper describe ".decorate_collection" do describe "options validation" do - before { CollectionDecorator.stub(:new) } + before { allow(CollectionDecorator).to receive(:new) } it "does not raise error on valid options" do valid_options = {with: OtherDecorator, context: {}} @@ -118,14 +118,14 @@ module Draper it "creates a CollectionDecorator using itself for each item" do object = [Model.new] - CollectionDecorator.should_receive(:new).with(object, with: Decorator) + expect(CollectionDecorator).to receive(:new).with(object, with: Decorator) Decorator.decorate_collection(object) end it "passes options to the collection decorator" do options = {with: OtherDecorator, context: {some: "context"}} - CollectionDecorator.should_receive(:new).with([], options) + expect(CollectionDecorator).to receive(:new).with([], options) Decorator.decorate_collection([], options) end end @@ -134,21 +134,21 @@ module Draper it "creates a custom collection decorator using itself for each item" do object = [Model.new] - ProductsDecorator.should_receive(:new).with(object, with: ProductDecorator) + expect(ProductsDecorator).to receive(:new).with(object, with: ProductDecorator) ProductDecorator.decorate_collection(object) end it "passes options to the collection decorator" do options = {with: OtherDecorator, context: {some: "context"}} - ProductsDecorator.should_receive(:new).with([], options) + expect(ProductsDecorator).to receive(:new).with([], options) ProductDecorator.decorate_collection([], options) end end context "when a NameError is thrown" do it "re-raises that error" do - String.any_instance.stub(:constantize) { Draper::DecoratedEnumerableProxy } + allow_any_instance_of(String).to receive(:constantize) { Draper::DecoratedEnumerableProxy } expect{ProductDecorator.decorate_collection([])}.to raise_error NameError, /Draper::DecoratedEnumerableProxy/ end end @@ -208,7 +208,7 @@ module Draper context "when an unrelated NameError is thrown" do it "re-raises that error" do - String.any_instance.stub(:constantize) { SomethingThatDoesntExist } + allow_any_instance_of(String).to receive(:constantize) { SomethingThatDoesntExist } expect{ProductDecorator.object_class}.to raise_error NameError, /SomethingThatDoesntExist/ end end @@ -221,19 +221,19 @@ module Draper describe ".object_class?" do it "returns truthy when .object_class is set" do - Decorator.stub(:object_class).and_return(Model) + allow(Decorator).to receive(:object_class).and_return(Model) expect(Decorator.object_class?).to be_truthy end it "returns false when .object_class is not inferrable" do - Decorator.stub(:object_class).and_raise(UninferrableSourceError.new(Decorator)) + allow(Decorator).to receive(:object_class).and_raise(UninferrableSourceError.new(Decorator)) expect(Decorator.object_class?).to be_falsey end it "is aliased to .source_class?" do - Decorator.stub(:object_class).and_return(Model) + allow(Decorator).to receive(:object_class).and_return(Model) expect(Decorator.source_class?).to be_truthy end @@ -243,7 +243,7 @@ module Draper protect_class Decorator describe "options validation" do - before { DecoratedAssociation.stub(:new).and_return(->{}) } + before { allow(DecoratedAssociation).to receive(:new).and_return(->{}) } it "does not raise error on valid options" do valid_options = {with: Class, scope: :sorted, context: {}} @@ -261,7 +261,7 @@ module Draper Decorator.decorates_association :children, options decorator = Decorator.new(Model.new) - DecoratedAssociation.should_receive(:new).with(decorator, :children, options).and_return(->{}) + expect(DecoratedAssociation).to receive(:new).with(decorator, :children, options).and_return(->{}) decorator.children end @@ -269,7 +269,7 @@ module Draper Decorator.decorates_association :children decorator = Decorator.new(Model.new) - DecoratedAssociation.should_receive(:new).once.and_return(->{}) + expect(DecoratedAssociation).to receive(:new).once.and_return(->{}) decorator.children decorator.children end @@ -278,9 +278,9 @@ module Draper Decorator.decorates_association :children decorator = Decorator.new(Model.new) decorated_association = ->{} - DecoratedAssociation.stub(:new).and_return(decorated_association) + allow(DecoratedAssociation).to receive(:new).and_return(decorated_association) - decorated_association.should_receive(:call).and_return(:decorated) + expect(decorated_association).to receive(:call).and_return(:decorated) expect(decorator.children).to be :decorated end end @@ -290,16 +290,16 @@ module Draper protect_class Decorator it "decorates each of the associations" do - Decorator.should_receive(:decorates_association).with(:friends, {}) - Decorator.should_receive(:decorates_association).with(:enemies, {}) + expect(Decorator).to receive(:decorates_association).with(:friends, {}) + expect(Decorator).to receive(:decorates_association).with(:enemies, {}) Decorator.decorates_associations :friends, :enemies end it "dispatches options" do options = {with: Class.new, scope: :foo, context: {}} - Decorator.should_receive(:decorates_association).with(:friends, options) - Decorator.should_receive(:decorates_association).with(:enemies, options) + expect(Decorator).to receive(:decorates_association).with(:friends, options) + expect(Decorator).to receive(:decorates_association).with(:enemies, options) Decorator.decorates_associations :friends, :enemies, options end end @@ -480,7 +480,7 @@ module Draper describe "#attributes" do it "returns only the object's attributes that are implemented by the decorator" do decorator = Decorator.new(double(attributes: {foo: "bar", baz: "qux"})) - decorator.stub(:foo) + allow(decorator).to receive(:foo) expect(decorator.attributes).to eq({foo: "bar"}) end @@ -488,7 +488,7 @@ module Draper describe ".model_name" do it "delegates to the source class" do - Decorator.stub object_class: double(model_name: :delegated) + allow(Decorator).to receive_messages object_class: double(model_name: :delegated) expect(Decorator.model_name).to be :delegated end @@ -514,7 +514,7 @@ module Draper decorator = Decorator.new(object) other = double(object: Model.new) - object.should_receive(:==).with(other).and_return(true) + expect(object).to receive(:==).with(other).and_return(true) expect(decorator == other).to be_truthy end @@ -523,7 +523,7 @@ module Draper decorator = Decorator.new(object) other = double(object: Model.new) - object.should_receive(:==).with(other).and_return(false) + expect(object).to receive(:==).with(other).and_return(false) expect(decorator == other).to be_falsey end end @@ -538,7 +538,7 @@ module Draper it "is false when #== is false" do decorator = Decorator.new(Model.new) - decorator.stub(:==).with(:anything).and_return(false) + allow(decorator).to receive(:==).with(:anything).and_return(false) expect(decorator === :anything).to be_falsey end @@ -574,12 +574,12 @@ module Draper protect_class Decorator it "defaults the :to option to :object" do - Object.should_receive(:delegate).with(:foo, :bar, to: :object) + expect(Object).to receive(:delegate).with(:foo, :bar, to: :object) Decorator.delegate :foo, :bar end it "does not overwrite the :to option if supplied" do - Object.should_receive(:delegate).with(:foo, :bar, to: :baz) + expect(Object).to receive(:delegate).with(:foo, :bar, to: :baz) Decorator.delegate :foo, :bar, to: :baz end end @@ -606,7 +606,7 @@ module Draper it "passes blocks to delegated methods" do object = Model.new - object.stub(:hello_world) { |*args, &block| block.call } + allow(object).to receive(:hello_world) { |*args, &block| block.call } decorator = Decorator.new(object) expect(decorator.hello_world{:yielded}).to be :yielded @@ -620,7 +620,7 @@ module Draper it "delegates already-delegated methods" do object = Class.new{ delegate :bar, to: :foo }.new - object.stub foo: double(bar: :delegated) + allow(object).to receive_messages foo: double(bar: :delegated) decorator = Decorator.new(object) expect(decorator.bar).to be :delegated @@ -652,14 +652,14 @@ module Draper context "with a source class" do it "delegates methods that exist on the source class" do object_class = Class.new - object_class.stub hello_world: :delegated - Decorator.stub object_class: object_class + allow(object_class).to receive_messages hello_world: :delegated + allow(Decorator).to receive_messages object_class: object_class expect(Decorator.hello_world).to be :delegated end it "does not delegate methods that do not exist on the source class" do - Decorator.stub object_class: Class.new + allow(Decorator).to receive_messages object_class: Class.new expect{Decorator.hello_world}.to raise_error NoMethodError end @@ -713,13 +713,13 @@ module Draper context "with a source class" do it "returns true for its own class methods" do Decorator.class_eval{def self.hello_world; end} - Decorator.stub object_class: Class.new + allow(Decorator).to receive_messages object_class: Class.new expect(Decorator).to respond_to :hello_world end it "returns true for the source's class methods" do - Decorator.stub object_class: double(hello_world: :delegated) + allow(Decorator).to receive_messages object_class: double(hello_world: :delegated) expect(Decorator).to respond_to :hello_world end @@ -737,7 +737,7 @@ module Draper describe ".respond_to_missing?" do it "allows .method to be called on delegated class methods" do - Decorator.stub object_class: double(hello_world: :delegated) + allow(Decorator).to receive_messages object_class: double(hello_world: :delegated) expect(Decorator.method(:hello_world)).not_to be_nil end diff --git a/spec/draper/factory_spec.rb b/spec/draper/factory_spec.rb index 03a4296d..6560ad2b 100644 --- a/spec/draper/factory_spec.rb +++ b/spec/draper/factory_spec.rb @@ -27,7 +27,7 @@ module Draper factory = Factory.new worker = ->(*){ :decorated } - Factory::Worker.should_receive(:new).and_return(worker) + expect(Factory::Worker).to receive(:new).and_return(worker) expect(factory.decorate(double)).to be :decorated end @@ -35,7 +35,7 @@ module Draper factory = Factory.new object = double - Factory::Worker.should_receive(:new).with(anything(), object).and_return(->(*){}) + expect(Factory::Worker).to receive(:new).with(anything(), object).and_return(->(*){}) factory.decorate(object) end @@ -44,7 +44,7 @@ module Draper decorator_class = double factory = Factory.new(with: decorator_class) - Factory::Worker.should_receive(:new).with(decorator_class, anything()).and_return(->(*){}) + expect(Factory::Worker).to receive(:new).with(decorator_class, anything()).and_return(->(*){}) factory.decorate(double) end end @@ -53,7 +53,7 @@ module Draper it "passes nil to the worker" do factory = Factory.new - Factory::Worker.should_receive(:new).with(nil, anything()).and_return(->(*){}) + expect(Factory::Worker).to receive(:new).with(nil, anything()).and_return(->(*){}) factory.decorate(double) end end @@ -61,10 +61,10 @@ module Draper it "passes options to the call" do factory = Factory.new worker = ->(*){} - Factory::Worker.stub new: worker + allow(Factory::Worker).to receive_messages new: worker options = {foo: "bar"} - worker.should_receive(:call).with(options) + expect(worker).to receive(:call).with(options) factory.decorate(double, options) end @@ -72,18 +72,18 @@ module Draper it "sets the passed context" do factory = Factory.new(context: {foo: "bar"}) worker = ->(*){} - Factory::Worker.stub new: worker + allow(Factory::Worker).to receive_messages new: worker - worker.should_receive(:call).with(baz: "qux", context: {foo: "bar"}) + expect(worker).to receive(:call).with(baz: "qux", context: {foo: "bar"}) factory.decorate(double, {baz: "qux"}) end it "is overridden by explicitly-specified context" do factory = Factory.new(context: {foo: "bar"}) worker = ->(*){} - Factory::Worker.stub new: worker + allow(Factory::Worker).to receive_messages new: worker - worker.should_receive(:call).with(context: {baz: "qux"}) + expect(worker).to receive(:call).with(context: {baz: "qux"}) factory.decorate(double, context: {baz: "qux"}) end end @@ -101,7 +101,7 @@ module Draper decorator = ->(*){} allow(worker).to receive(:decorator){ decorator } - decorator.should_receive(:call).with(object, options).and_return(:decorated) + expect(decorator).to receive(:call).with(object, options).and_return(:decorated) expect(worker.call(options)).to be :decorated end @@ -109,29 +109,29 @@ module Draper it "calls it" do worker = Factory::Worker.new(double, double) decorator = ->(*){} - worker.stub decorator: decorator + allow(worker).to receive_messages decorator: decorator context = {foo: "bar"} - decorator.should_receive(:call).with(anything(), context: context) + expect(decorator).to receive(:call).with(anything(), context: context) worker.call(context: ->{ context }) end it "receives arguments from the :context_args option" do worker = Factory::Worker.new(double, double) - worker.stub decorator: ->(*){} + allow(worker).to receive_messages decorator: ->(*){} context = ->{} - context.should_receive(:call).with(:foo, :bar) + expect(context).to receive(:call).with(:foo, :bar) worker.call(context: context, context_args: [:foo, :bar]) end it "wraps non-arrays passed to :context_args" do worker = Factory::Worker.new(double, double) - worker.stub decorator: ->(*){} + allow(worker).to receive_messages decorator: ->(*){} context = ->{} hash = {foo: "bar"} - context.should_receive(:call).with(hash) + expect(context).to receive(:call).with(hash) worker.call(context: context, context_args: hash) end end @@ -140,10 +140,10 @@ module Draper it "doesn't call it" do worker = Factory::Worker.new(double, double) decorator = ->(*){} - worker.stub decorator: decorator + allow(worker).to receive_messages decorator: decorator context = {foo: "bar"} - decorator.should_receive(:call).with(anything(), context: context) + expect(decorator).to receive(:call).with(anything(), context: context) worker.call(context: context) end end @@ -151,9 +151,9 @@ module Draper it "does not pass the :context_args option to the decorator" do worker = Factory::Worker.new(double, double) decorator = ->(*){} - worker.stub decorator: decorator + allow(worker).to receive_messages decorator: decorator - decorator.should_receive(:call).with(anything(), foo: "bar") + expect(decorator).to receive(:call).with(anything(), foo: "bar") worker.call(foo: "bar", context_args: []) end end @@ -176,7 +176,7 @@ module Draper options = {foo: "bar"} worker = Factory::Worker.new(nil, object) - object.should_receive(:decorate).with(options).and_return(:decorated) + expect(object).to receive(:decorate).with(options).and_return(:decorated) expect(worker.decorator.call(object, options)).to be :decorated end end @@ -231,7 +231,7 @@ module Draper allow(object).to receive(:decorate){ nil } worker = Factory::Worker.new(nil, object) - decorator_class.should_receive(:decorate_collection).with(object, foo: "bar", with: nil).and_return(:decorated) + expect(decorator_class).to receive(:decorate_collection).with(object, foo: "bar", with: nil).and_return(:decorated) expect(worker.decorator.call(object, foo: "bar")).to be :decorated end end diff --git a/spec/draper/finders_spec.rb b/spec/draper/finders_spec.rb index 5717d8e7..131903e4 100644 --- a/spec/draper/finders_spec.rb +++ b/spec/draper/finders_spec.rb @@ -7,20 +7,20 @@ module Draper describe ".find" do it "proxies to the model class" do - Product.should_receive(:find).with(1) + expect(Product).to receive(:find).with(1) ProductDecorator.find(1) end it "decorates the result" do found = Product.new - Product.stub(:find).and_return(found) + allow(Product).to receive(:find).and_return(found) decorator = ProductDecorator.find(1) expect(decorator).to be_a ProductDecorator expect(decorator.object).to be found end it "passes context to the decorator" do - Product.stub(:find) + allow(Product).to receive(:find) context = {some: "context"} decorator = ProductDecorator.find(1, context: context) @@ -30,40 +30,40 @@ module Draper describe ".find_by_(x)" do it "proxies to the model class" do - Product.should_receive(:find_by_name).with("apples") + expect(Product).to receive(:find_by_name).with("apples") ProductDecorator.find_by_name("apples") end it "decorates the result" do found = Product.new - Product.stub(:find_by_name).and_return(found) + allow(Product).to receive(:find_by_name).and_return(found) decorator = ProductDecorator.find_by_name("apples") expect(decorator).to be_a ProductDecorator expect(decorator.object).to be found end it "proxies complex ProductDecorators" do - Product.should_receive(:find_by_name_and_size).with("apples", "large") + expect(Product).to receive(:find_by_name_and_size).with("apples", "large") ProductDecorator.find_by_name_and_size("apples", "large") end it "proxies find_last_by_(x) ProductDecorators" do - Product.should_receive(:find_last_by_name_and_size).with("apples", "large") + expect(Product).to receive(:find_last_by_name_and_size).with("apples", "large") ProductDecorator.find_last_by_name_and_size("apples", "large") end it "proxies find_or_initialize_by_(x) ProductDecorators" do - Product.should_receive(:find_or_initialize_by_name_and_size).with("apples", "large") + expect(Product).to receive(:find_or_initialize_by_name_and_size).with("apples", "large") ProductDecorator.find_or_initialize_by_name_and_size("apples", "large") end it "proxies find_or_create_by_(x) ProductDecorators" do - Product.should_receive(:find_or_create_by_name_and_size).with("apples", "large") + expect(Product).to receive(:find_or_create_by_name_and_size).with("apples", "large") ProductDecorator.find_or_create_by_name_and_size("apples", "large") end it "passes context to the decorator" do - Product.stub(:find_by_name_and_size) + allow(Product).to receive(:find_by_name_and_size) context = {some: "context"} decorator = ProductDecorator.find_by_name_and_size("apples", "large", context: context) @@ -73,13 +73,13 @@ module Draper describe ".find_all_by_" do it "proxies to the model class" do - Product.should_receive(:find_all_by_name_and_size).with("apples", "large").and_return([]) + expect(Product).to receive(:find_all_by_name_and_size).with("apples", "large").and_return([]) ProductDecorator.find_all_by_name_and_size("apples", "large") end it "decorates the result" do found = [Product.new, Product.new] - Product.stub(:find_all_by_name).and_return(found) + allow(Product).to receive(:find_all_by_name).and_return(found) decorator = ProductDecorator.find_all_by_name("apples") expect(decorator).to be_a Draper::CollectionDecorator @@ -88,7 +88,7 @@ module Draper end it "passes context to the decorator" do - Product.stub(:find_all_by_name) + allow(Product).to receive(:find_all_by_name) context = {some: "context"} decorator = ProductDecorator.find_all_by_name("apples", context: context) @@ -99,7 +99,7 @@ module Draper describe ".all" do it "returns a decorated collection" do found = [Product.new, Product.new] - Product.stub all: found + allow(Product).to receive_messages all: found decorator = ProductDecorator.all expect(decorator).to be_a Draper::CollectionDecorator @@ -108,7 +108,7 @@ module Draper end it "passes context to the decorator" do - Product.stub(:all) + allow(Product).to receive(:all) context = {some: "context"} decorator = ProductDecorator.all(context: context) @@ -118,20 +118,20 @@ module Draper describe ".first" do it "proxies to the model class" do - Product.should_receive(:first) + expect(Product).to receive(:first) ProductDecorator.first end it "decorates the result" do first = Product.new - Product.stub(:first).and_return(first) + allow(Product).to receive(:first).and_return(first) decorator = ProductDecorator.first expect(decorator).to be_a ProductDecorator expect(decorator.object).to be first end it "passes context to the decorator" do - Product.stub(:first) + allow(Product).to receive(:first) context = {some: "context"} decorator = ProductDecorator.first(context: context) @@ -141,20 +141,20 @@ module Draper describe ".last" do it "proxies to the model class" do - Product.should_receive(:last) + expect(Product).to receive(:last) ProductDecorator.last end it "decorates the result" do last = Product.new - Product.stub(:last).and_return(last) + allow(Product).to receive(:last).and_return(last) decorator = ProductDecorator.last expect(decorator).to be_a ProductDecorator expect(decorator.object).to be last end it "passes context to the decorator" do - Product.stub(:last) + allow(Product).to receive(:last) context = {some: "context"} decorator = ProductDecorator.last(context: context) diff --git a/spec/draper/helper_proxy_spec.rb b/spec/draper/helper_proxy_spec.rb index ce5312b9..de4d030d 100644 --- a/spec/draper/helper_proxy_spec.rb +++ b/spec/draper/helper_proxy_spec.rb @@ -18,7 +18,7 @@ module Draper view_context = double helper_proxy = HelperProxy.new(view_context) - view_context.stub(:foo) { |arg| arg } + allow(view_context).to receive(:foo) { |arg| arg } expect(helper_proxy.foo(:passed)).to be :passed end @@ -26,7 +26,7 @@ module Draper view_context = double helper_proxy = HelperProxy.new(view_context) - view_context.stub(:foo) { |&block| block.call } + allow(view_context).to receive(:foo) { |&block| block.call } expect(helper_proxy.foo{:yielded}).to be :yielded end diff --git a/spec/draper/lazy_helpers_spec.rb b/spec/draper/lazy_helpers_spec.rb index e1506029..e53c618e 100644 --- a/spec/draper/lazy_helpers_spec.rb +++ b/spec/draper/lazy_helpers_spec.rb @@ -8,12 +8,12 @@ module Draper end it "proxies methods to #helpers" do - decorator.helpers.stub(:foo) { |arg| arg } + allow(decorator.helpers).to receive(:foo) { |arg| arg } expect(decorator.foo(:passed)).to be :passed end it "passes blocks" do - decorator.helpers.stub(:foo) { |&block| block.call } + allow(decorator.helpers).to receive(:foo) { |&block| block.call } expect(decorator.foo{:yielded}).to be :yielded end end diff --git a/spec/draper/view_context/build_strategy_spec.rb b/spec/draper/view_context/build_strategy_spec.rb index abb154c9..df7d3117 100644 --- a/spec/draper/view_context/build_strategy_spec.rb +++ b/spec/draper/view_context/build_strategy_spec.rb @@ -14,7 +14,7 @@ module Draper context "when a current controller is set" do it "returns the controller's view context" do view_context = fake_view_context - ViewContext.stub controller: fake_controller(view_context) + allow(ViewContext).to receive_messages controller: fake_controller(view_context) strategy = ViewContext::BuildStrategy::Full.new expect(strategy.call).to be view_context @@ -33,7 +33,7 @@ module Draper it "adds a request if one is not defined" do controller = Class.new(ActionController::Base).new - ViewContext.stub controller: controller + allow(ViewContext).to receive_messages controller: controller strategy = ViewContext::BuildStrategy::Full.new expect(controller.request).to be_nil @@ -47,7 +47,7 @@ module Draper end it "adds methods to the view context from the constructor block" do - ViewContext.stub controller: fake_controller + allow(ViewContext).to receive_messages controller: fake_controller strategy = ViewContext::BuildStrategy::Full.new do def a_helper_method; end end @@ -57,7 +57,7 @@ def a_helper_method; end it "includes modules into the view context from the constructor block" do view_context = Object.new - ViewContext.stub controller: fake_controller(view_context) + allow(ViewContext).to receive_messages controller: fake_controller(view_context) helpers = Module.new do def a_helper_method; end end diff --git a/spec/draper/view_context_spec.rb b/spec/draper/view_context_spec.rb index b1f06af8..1816cd1f 100644 --- a/spec/draper/view_context_spec.rb +++ b/spec/draper/view_context_spec.rb @@ -7,7 +7,7 @@ module Draper let(:controller) { Class.new(base) { include ViewContext } } it "saves the superclass's view context" do - ViewContext.should_receive(:current=).with(:controller_view_context) + expect(ViewContext).to receive(:current=).with(:controller_view_context) controller.new.view_context end @@ -18,7 +18,7 @@ module Draper describe ".controller" do it "returns the stored controller from RequestStore" do - RequestStore.stub store: {current_controller: :stored_controller} + allow(RequestStore).to receive_messages store: {current_controller: :stored_controller} expect(ViewContext.controller).to be :stored_controller end @@ -27,7 +27,7 @@ module Draper describe ".controller=" do it "stores a controller in RequestStore" do store = {} - RequestStore.stub store: store + allow(RequestStore).to receive_messages store: store ViewContext.controller = :stored_controller expect(store[:current_controller]).to be :stored_controller @@ -36,25 +36,25 @@ module Draper describe ".current" do it "returns the stored view context from RequestStore" do - RequestStore.stub store: {current_view_context: :stored_view_context} + allow(RequestStore).to receive_messages store: {current_view_context: :stored_view_context} expect(ViewContext.current).to be :stored_view_context end context "when no view context is stored" do it "builds a view context" do - RequestStore.stub store: {} - ViewContext.stub build_strategy: ->{ :new_view_context } - HelperProxy.stub(:new).with(:new_view_context).and_return(:new_helper_proxy) + allow(RequestStore).to receive_messages store: {} + allow(ViewContext).to receive_messages build_strategy: ->{ :new_view_context } + allow(HelperProxy).to receive(:new).with(:new_view_context).and_return(:new_helper_proxy) expect(ViewContext.current).to be :new_helper_proxy end it "stores the built view context" do store = {} - RequestStore.stub store: store - ViewContext.stub build_strategy: ->{ :new_view_context } - HelperProxy.stub(:new).with(:new_view_context).and_return(:new_helper_proxy) + allow(RequestStore).to receive_messages store: store + allow(ViewContext).to receive_messages build_strategy: ->{ :new_view_context } + allow(HelperProxy).to receive(:new).with(:new_view_context).and_return(:new_helper_proxy) ViewContext.current expect(store[:current_view_context]).to be :new_helper_proxy @@ -65,8 +65,8 @@ module Draper describe ".current=" do it "stores a helper proxy for the view context in RequestStore" do store = {} - RequestStore.stub store: store - HelperProxy.stub(:new).with(:stored_view_context).and_return(:stored_helper_proxy) + allow(RequestStore).to receive_messages store: store + allow(HelperProxy).to receive(:new).with(:stored_view_context).and_return(:stored_helper_proxy) ViewContext.current = :stored_view_context expect(store[:current_view_context]).to be :stored_helper_proxy @@ -76,7 +76,7 @@ module Draper describe ".clear!" do it "clears the stored controller and view controller" do store = {current_controller: :stored_controller, current_view_context: :stored_view_context} - RequestStore.stub store: store + allow(RequestStore).to receive_messages store: store ViewContext.clear! expect(store).not_to have_key :current_controller @@ -86,7 +86,7 @@ module Draper describe ".build" do it "returns a new view context using the build strategy" do - ViewContext.stub build_strategy: ->{ :new_view_context } + allow(ViewContext).to receive_messages build_strategy: ->{ :new_view_context } expect(ViewContext.build).to be :new_view_context end @@ -94,17 +94,17 @@ module Draper describe ".build!" do it "returns a helper proxy for the new view context" do - ViewContext.stub build_strategy: ->{ :new_view_context } - HelperProxy.stub(:new).with(:new_view_context).and_return(:new_helper_proxy) + allow(ViewContext).to receive_messages build_strategy: ->{ :new_view_context } + allow(HelperProxy).to receive(:new).with(:new_view_context).and_return(:new_helper_proxy) expect(ViewContext.build!).to be :new_helper_proxy end it "stores the helper proxy" do store = {} - RequestStore.stub store: store - ViewContext.stub build_strategy: ->{ :new_view_context } - HelperProxy.stub(:new).with(:new_view_context).and_return(:new_helper_proxy) + allow(RequestStore).to receive_messages store: store + allow(ViewContext).to receive_messages build_strategy: ->{ :new_view_context } + allow(HelperProxy).to receive(:new).with(:new_view_context).and_return(:new_helper_proxy) ViewContext.build! expect(store[:current_view_context]).to be :new_helper_proxy @@ -131,7 +131,7 @@ module Draper end it "passes a block to the strategy" do - ViewContext::BuildStrategy::Fast.stub(:new) { |&block| block.call } + allow(ViewContext::BuildStrategy::Fast).to receive(:new) { |&block| block.call } expect(ViewContext.test_strategy(:fast){:passed}).to be :passed end @@ -144,7 +144,7 @@ module Draper end it "passes a block to the strategy" do - ViewContext::BuildStrategy::Full.stub(:new) { |&block| block.call } + allow(ViewContext::BuildStrategy::Full).to receive(:new) { |&block| block.call } expect(ViewContext.test_strategy(:full){:passed}).to be :passed end diff --git a/spec/generators/controller/controller_generator_spec.rb b/spec/generators/controller/controller_generator_spec.rb index af77c1fb..96cfe9f4 100644 --- a/spec/generators/controller/controller_generator_spec.rb +++ b/spec/generators/controller/controller_generator_spec.rb @@ -16,7 +16,7 @@ describe "naming" do before { run_generator %w(YourModels) } - it { should contain "class YourModelDecorator" } + it { is_expected.to contain "class YourModelDecorator" } end end end \ No newline at end of file diff --git a/spec/generators/decorator/decorator_generator_spec.rb b/spec/generators/decorator/decorator_generator_spec.rb index 08a9601f..f8344c8a 100644 --- a/spec/generators/decorator/decorator_generator_spec.rb +++ b/spec/generators/decorator/decorator_generator_spec.rb @@ -15,27 +15,27 @@ describe "naming" do before { run_generator %w(YourModel) } - it { should contain "class YourModelDecorator" } + it { is_expected.to contain "class YourModelDecorator" } end describe "namespacing" do subject { file("app/decorators/namespace/your_model_decorator.rb") } before { run_generator %w(Namespace::YourModel) } - it { should contain "class Namespace::YourModelDecorator" } + it { is_expected.to contain "class Namespace::YourModelDecorator" } end describe "inheritance" do context "by default" do before { run_generator %w(YourModel) } - it { should contain "class YourModelDecorator < Draper::Decorator" } + it { is_expected.to contain "class YourModelDecorator < Draper::Decorator" } end context "with the --parent option" do before { run_generator %w(YourModel --parent=FooDecorator) } - it { should contain "class YourModelDecorator < FooDecorator" } + it { is_expected.to contain "class YourModelDecorator < FooDecorator" } end context "with an ApplicationDecorator" do @@ -47,7 +47,7 @@ before { run_generator %w(YourModel) } - it { should contain "class YourModelDecorator < ApplicationDecorator" } + it { is_expected.to contain "class YourModelDecorator < ApplicationDecorator" } end end end @@ -59,14 +59,14 @@ describe "naming" do before { run_generator %w(YourModel -t=rspec) } - it { should contain "describe YourModelDecorator" } + it { is_expected.to contain "describe YourModelDecorator" } end describe "namespacing" do subject { file("spec/decorators/namespace/your_model_decorator_spec.rb") } before { run_generator %w(Namespace::YourModel -t=rspec) } - it { should contain "describe Namespace::YourModelDecorator" } + it { is_expected.to contain "describe Namespace::YourModelDecorator" } end end end @@ -78,14 +78,14 @@ describe "naming" do before { run_generator %w(YourModel -t=test_unit) } - it { should contain "class YourModelDecoratorTest < Draper::TestCase" } + it { is_expected.to contain "class YourModelDecoratorTest < Draper::TestCase" } end describe "namespacing" do subject { file("test/decorators/namespace/your_model_decorator_test.rb") } before { run_generator %w(Namespace::YourModel -t=test_unit) } - it { should contain "class Namespace::YourModelDecoratorTest < Draper::TestCase" } + it { is_expected.to contain "class Namespace::YourModelDecoratorTest < Draper::TestCase" } end end end diff --git a/spec/support/shared_examples/view_helpers.rb b/spec/support/shared_examples/view_helpers.rb index f7d06213..dad202e3 100644 --- a/spec/support/shared_examples/view_helpers.rb +++ b/spec/support/shared_examples/view_helpers.rb @@ -1,38 +1,38 @@ shared_examples_for "view helpers" do |subject| describe "#helpers" do it "returns the current view context" do - Draper::ViewContext.stub current: :current_view_context + allow(Draper::ViewContext).to receive_messages current: :current_view_context expect(subject.helpers).to be :current_view_context end it "is aliased to #h" do - Draper::ViewContext.stub current: :current_view_context + allow(Draper::ViewContext).to receive_messages current: :current_view_context expect(subject.h).to be :current_view_context end end describe "#localize" do it "delegates to #helpers" do - subject.stub helpers: double - subject.helpers.should_receive(:localize).with(:an_object, some: "parameter") + allow(subject).to receive_messages helpers: double + expect(subject.helpers).to receive(:localize).with(:an_object, some: "parameter") subject.localize(:an_object, some: "parameter") end it "is aliased to #l" do - subject.stub helpers: double - subject.helpers.should_receive(:localize).with(:an_object, some: "parameter") + allow(subject).to receive_messages helpers: double + expect(subject.helpers).to receive(:localize).with(:an_object, some: "parameter") subject.l(:an_object, some: "parameter") end end describe ".helpers" do it "returns the current view context" do - Draper::ViewContext.stub current: :current_view_context + allow(Draper::ViewContext).to receive_messages current: :current_view_context expect(subject.class.helpers).to be :current_view_context end it "is aliased to .h" do - Draper::ViewContext.stub current: :current_view_context + allow(Draper::ViewContext).to receive_messages current: :current_view_context expect(subject.class.h).to be :current_view_context end end From 46f97aaad72e97e673fccfb6b7dcbf2c1c668cdc Mon Sep 17 00:00:00 2001 From: Kurtis Rainbolt-Greene Date: Sat, 23 May 2015 20:24:23 -0500 Subject: [PATCH 02/31] rspec-rails ~> 3.2 uses rails_helper fixes #668 As of rspec-rails 3.2 they generate two files: - spec/spec_helper.rb - spec/rails_helper.rb This allows the user to either do pure ruby unit tests or include the entire rails environment. Draper really only works in a rails application if it has the rails environment. This matches ~> 3.2 and uses the rails_helper instead of spec_helper. --- lib/generators/rspec/templates/decorator_spec.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/generators/rspec/templates/decorator_spec.rb b/lib/generators/rspec/templates/decorator_spec.rb index 6acbd52f..e15e8328 100644 --- a/lib/generators/rspec/templates/decorator_spec.rb +++ b/lib/generators/rspec/templates/decorator_spec.rb @@ -1,4 +1,9 @@ -require 'spec_helper' +<% if RSpec::Rails::Version::STRING.match(/\A3\.[^10]/) %> + require 'rails_helper' +<% else %> + require 'spec_helper' +<% end %> describe <%= class_name %>Decorator do + end From 59bdeb1d6938c95eb349c56f532b77c1ce3f3a95 Mon Sep 17 00:00:00 2001 From: Kurtis Rainbolt-Greene Date: Mon, 1 Jun 2015 16:17:54 -0700 Subject: [PATCH 03/31] RSpec is now into the whole type based tagging Update gitignore ignore .ruby-gemset and .ruby-version --- .gitignore | 2 ++ lib/generators/rspec/templates/decorator_spec.rb | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5c99549c..995bc6ef 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,5 @@ vendor/bundle *.DS_Store spec/dummy/log/* spec/dummy/db/*.sqlite3 +.ruby-version +.ruby-gemset diff --git a/lib/generators/rspec/templates/decorator_spec.rb b/lib/generators/rspec/templates/decorator_spec.rb index e15e8328..a44ddbd3 100644 --- a/lib/generators/rspec/templates/decorator_spec.rb +++ b/lib/generators/rspec/templates/decorator_spec.rb @@ -4,6 +4,6 @@ require 'spec_helper' <% end %> -describe <%= class_name %>Decorator do +describe <%= class_name %>Decorator, type: :decorator do end From 66c5b398c4c0342faec5427ca82eee3921ed0e62 Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Tue, 25 Aug 2015 14:16:53 -0600 Subject: [PATCH 04/31] Merge PR #668 #675 from drapergem/master Fix Spec spec generator because RSpec no longer prefers monkey-patching. --- lib/generators/rspec/templates/decorator_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/generators/rspec/templates/decorator_spec.rb b/lib/generators/rspec/templates/decorator_spec.rb index a44ddbd3..72ba11ad 100644 --- a/lib/generators/rspec/templates/decorator_spec.rb +++ b/lib/generators/rspec/templates/decorator_spec.rb @@ -4,6 +4,6 @@ require 'spec_helper' <% end %> -describe <%= class_name %>Decorator, type: :decorator do +RSpec.describe <%= class_name %>Decorator, type: :decorator do end From 2ae00efc6f070b9f1c48de2087594de59591179f Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 15:19:08 -0600 Subject: [PATCH 05/31] Update .gitignore. Ignore all .ruby-version, .ruby-gemset, and .rake-tasks files. --- .gitignore | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 995bc6ef..99cf8ff8 100644 --- a/.gitignore +++ b/.gitignore @@ -14,5 +14,6 @@ vendor/bundle *.DS_Store spec/dummy/log/* spec/dummy/db/*.sqlite3 -.ruby-version -.ruby-gemset +.ruby-version* +.ruby-gemset* +.rake_tasks From ed5c3c6c8adf958d06e23f56861f4210d13f6ac8 Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 15:35:03 -0600 Subject: [PATCH 06/31] Refactored spec setup and helpers Moved dummy/spec to spec, symlinked dummy/spec to spec. This should make the test suite easier to maintain, especially given the changes to RSpec v3. Should also mitigate NameErrors and LoadErrors when running the specs. --- .../active_model_serializers_spec.rb | 0 .../spec => }/decorators/devise_spec.rb | 0 .../spec => }/decorators/helpers_spec.rb | 0 .../decorators/post_decorator_spec.rb | 0 .../spec => }/decorators/spec_type_spec.rb | 0 .../spec => }/decorators/view_context_spec.rb | 0 spec/dummy/spec | 1 + spec/dummy/spec/spec_helper.rb | 8 ----- .../spec => }/mailers/post_mailer_spec.rb | 0 .../spec => }/models/mongoid_post_spec.rb | 0 spec/{dummy/spec => }/models/post_spec.rb | 0 spec/rails_helper.rb | 32 +++++++++++++++++++ .../spec => }/shared_examples/decoratable.rb | 0 spec/spec_helper.rb | 20 ++++++++++-- 14 files changed, 51 insertions(+), 10 deletions(-) rename spec/{dummy/spec => }/decorators/active_model_serializers_spec.rb (100%) rename spec/{dummy/spec => }/decorators/devise_spec.rb (100%) rename spec/{dummy/spec => }/decorators/helpers_spec.rb (100%) rename spec/{dummy/spec => }/decorators/post_decorator_spec.rb (100%) rename spec/{dummy/spec => }/decorators/spec_type_spec.rb (100%) rename spec/{dummy/spec => }/decorators/view_context_spec.rb (100%) create mode 120000 spec/dummy/spec delete mode 100644 spec/dummy/spec/spec_helper.rb rename spec/{dummy/spec => }/mailers/post_mailer_spec.rb (100%) rename spec/{dummy/spec => }/models/mongoid_post_spec.rb (100%) rename spec/{dummy/spec => }/models/post_spec.rb (100%) create mode 100644 spec/rails_helper.rb rename spec/{dummy/spec => }/shared_examples/decoratable.rb (100%) diff --git a/spec/dummy/spec/decorators/active_model_serializers_spec.rb b/spec/decorators/active_model_serializers_spec.rb similarity index 100% rename from spec/dummy/spec/decorators/active_model_serializers_spec.rb rename to spec/decorators/active_model_serializers_spec.rb diff --git a/spec/dummy/spec/decorators/devise_spec.rb b/spec/decorators/devise_spec.rb similarity index 100% rename from spec/dummy/spec/decorators/devise_spec.rb rename to spec/decorators/devise_spec.rb diff --git a/spec/dummy/spec/decorators/helpers_spec.rb b/spec/decorators/helpers_spec.rb similarity index 100% rename from spec/dummy/spec/decorators/helpers_spec.rb rename to spec/decorators/helpers_spec.rb diff --git a/spec/dummy/spec/decorators/post_decorator_spec.rb b/spec/decorators/post_decorator_spec.rb similarity index 100% rename from spec/dummy/spec/decorators/post_decorator_spec.rb rename to spec/decorators/post_decorator_spec.rb diff --git a/spec/dummy/spec/decorators/spec_type_spec.rb b/spec/decorators/spec_type_spec.rb similarity index 100% rename from spec/dummy/spec/decorators/spec_type_spec.rb rename to spec/decorators/spec_type_spec.rb diff --git a/spec/dummy/spec/decorators/view_context_spec.rb b/spec/decorators/view_context_spec.rb similarity index 100% rename from spec/dummy/spec/decorators/view_context_spec.rb rename to spec/decorators/view_context_spec.rb diff --git a/spec/dummy/spec b/spec/dummy/spec new file mode 120000 index 00000000..b870225a --- /dev/null +++ b/spec/dummy/spec @@ -0,0 +1 @@ +../ \ No newline at end of file diff --git a/spec/dummy/spec/spec_helper.rb b/spec/dummy/spec/spec_helper.rb deleted file mode 100644 index aa3282b2..00000000 --- a/spec/dummy/spec/spec_helper.rb +++ /dev/null @@ -1,8 +0,0 @@ -ENV['RAILS_ENV'] ||= 'test' -require File.expand_path('../../config/environment', __FILE__) -require 'rspec/rails' - -RSpec.configure do |config| - config.expect_with(:rspec) {|c| c.syntax = :expect} - config.order = :random -end diff --git a/spec/dummy/spec/mailers/post_mailer_spec.rb b/spec/mailers/post_mailer_spec.rb similarity index 100% rename from spec/dummy/spec/mailers/post_mailer_spec.rb rename to spec/mailers/post_mailer_spec.rb diff --git a/spec/dummy/spec/models/mongoid_post_spec.rb b/spec/models/mongoid_post_spec.rb similarity index 100% rename from spec/dummy/spec/models/mongoid_post_spec.rb rename to spec/models/mongoid_post_spec.rb diff --git a/spec/dummy/spec/models/post_spec.rb b/spec/models/post_spec.rb similarity index 100% rename from spec/dummy/spec/models/post_spec.rb rename to spec/models/post_spec.rb diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 00000000..34e083f5 --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,32 @@ +ENV['RAILS_ENV'] ||= 'test' +require File.expand_path('../../config/environment', __FILE__) +# Prevent database truncation if the environment is production +abort('Rails is running in production mode!') if Rails.env.production? +require 'spec_helper' +require 'rspec/rails' +# Add additional requires below this line. Rails is not loaded until this point! + +# Requires supporting ruby files with custom matchers and macros, etc, in +# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are +# run as spec files by default. This means that files in spec/support that end +# in _spec.rb will both be required and run as specs, causing the specs to be +# run twice. It is recommended that you do not name files matching this glob to +# end with _spec.rb. You can configure this pattern with the --pattern +# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. +# +# The following line is provided for convenience purposes. It has the downside +# of increasing the boot-up time by auto-requiring all files in the support +# directory. Alternatively, in the individual `*_spec.rb` files, manually +# require only the support files necessary. +# +# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } + +# Checks for pending migrations before tests are run. +# If you are not using ActiveRecord, you can remove this line. +ActiveRecord::Migration.maintain_test_schema! + +RSpec.configure do |config| + config.fixture_path = "#{::Rails.root}/spec/fixtures" + config.use_transactional_fixtures = true + config.infer_spec_type_from_file_location! +end diff --git a/spec/dummy/spec/shared_examples/decoratable.rb b/spec/shared_examples/decoratable.rb similarity index 100% rename from spec/dummy/spec/shared_examples/decoratable.rb rename to spec/shared_examples/decoratable.rb diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index df3aad9e..cccdb8ac 100755 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,11 +5,27 @@ require 'action_controller/test_case' RSpec.configure do |config| - config.expect_with(:rspec) {|c| c.syntax = :expect} - config.order = :random + config.expect_with :rspec do |expectations| + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + config.mock_with :rspec do |mocks| + mocks.verify_partial_doubles = true mocks.yield_receiver_to_any_instance_implementation_blocks = true end + + config.filter_run :focus + config.run_all_when_everything_filtered = true + config.disable_monkey_patching! + + if config.files_to_run.one? + config.default_formatter = 'doc' + else + config.default_formatter = 'progress' + end + + config.order = :random + Kernel.srand config.seed end class Model; include Draper::Decoratable; end From 354b7e9d9ec393a37df3d6989b6d6c66a485b129 Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 20:22:13 -0600 Subject: [PATCH 07/31] Moved back to separated spec & spec/dummy/spec Combined spec files were causing a problem with the already-existing rake task and cwd for dummy specs. Moved back to the original setup. Also changed most of the object.stub(method, return_value) to allow(object).to receive(:method).and_return(return_value) --- spec/dummy/spec | 1 - .../active_model_serializers_spec.rb | 4 +- .../spec}/decorators/devise_spec.rb | 4 +- .../spec}/decorators/helpers_spec.rb | 4 +- .../spec}/decorators/post_decorator_spec.rb | 4 +- .../spec}/decorators/spec_type_spec.rb | 4 +- .../spec}/decorators/view_context_spec.rb | 8 +-- .../spec}/mailers/post_mailer_spec.rb | 4 +- spec/dummy/spec/models/mongoid_post_spec.rb | 8 +++ spec/dummy/spec/models/post_spec.rb | 6 ++ spec/{ => dummy/spec}/rails_helper.rb | 0 .../spec}/shared_examples/decoratable.rb | 2 +- spec/dummy/spec/spec_helper.rb | 60 +++++++++++++++++++ spec/models/mongoid_post_spec.rb | 8 --- spec/models/post_spec.rb | 6 -- 15 files changed, 91 insertions(+), 32 deletions(-) delete mode 120000 spec/dummy/spec rename spec/{ => dummy/spec}/decorators/active_model_serializers_spec.rb (85%) rename spec/{ => dummy/spec}/decorators/devise_spec.rb (94%) rename spec/{ => dummy/spec}/decorators/helpers_spec.rb (87%) rename spec/{ => dummy/spec}/decorators/post_decorator_spec.rb (96%) rename spec/{ => dummy/spec}/decorators/spec_type_spec.rb (58%) rename spec/{ => dummy/spec}/decorators/view_context_spec.rb (62%) rename spec/{ => dummy/spec}/mailers/post_mailer_spec.rb (94%) create mode 100644 spec/dummy/spec/models/mongoid_post_spec.rb create mode 100644 spec/dummy/spec/models/post_spec.rb rename spec/{ => dummy/spec}/rails_helper.rb (100%) rename spec/{ => dummy/spec}/shared_examples/decoratable.rb (92%) create mode 100644 spec/dummy/spec/spec_helper.rb delete mode 100644 spec/models/mongoid_post_spec.rb delete mode 100644 spec/models/post_spec.rb diff --git a/spec/dummy/spec b/spec/dummy/spec deleted file mode 120000 index b870225a..00000000 --- a/spec/dummy/spec +++ /dev/null @@ -1 +0,0 @@ -../ \ No newline at end of file diff --git a/spec/decorators/active_model_serializers_spec.rb b/spec/dummy/spec/decorators/active_model_serializers_spec.rb similarity index 85% rename from spec/decorators/active_model_serializers_spec.rb rename to spec/dummy/spec/decorators/active_model_serializers_spec.rb index 7de50532..e4d00a8f 100644 --- a/spec/decorators/active_model_serializers_spec.rb +++ b/spec/dummy/spec/decorators/active_model_serializers_spec.rb @@ -1,6 +1,6 @@ -require 'spec_helper' +require_relative '../rails_helper' -describe Draper::CollectionDecorator do +RSpec.describe Draper::CollectionDecorator do describe "#active_model_serializer" do it "returns ActiveModel::ArraySerializer" do collection_decorator = Draper::CollectionDecorator.new([]) diff --git a/spec/decorators/devise_spec.rb b/spec/dummy/spec/decorators/devise_spec.rb similarity index 94% rename from spec/decorators/devise_spec.rb rename to spec/dummy/spec/decorators/devise_spec.rb index 6480751f..540a04d9 100644 --- a/spec/decorators/devise_spec.rb +++ b/spec/dummy/spec/decorators/devise_spec.rb @@ -1,7 +1,7 @@ -require 'spec_helper' +require_relative '../rails_helper' if defined?(Devise) - describe "A decorator spec" do + RSpec.describe "A decorator spec" do it "can sign in a real user" do user = User.new sign_in user diff --git a/spec/decorators/helpers_spec.rb b/spec/dummy/spec/decorators/helpers_spec.rb similarity index 87% rename from spec/decorators/helpers_spec.rb rename to spec/dummy/spec/decorators/helpers_spec.rb index eb171f89..8d32a545 100644 --- a/spec/decorators/helpers_spec.rb +++ b/spec/dummy/spec/decorators/helpers_spec.rb @@ -1,6 +1,6 @@ -require 'spec_helper' +require_relative '../rails_helper' -describe "A decorator spec" do +RSpec.describe "A decorator spec" do it "can access helpers through `helper`" do expect(helper.content_tag(:p, "Help!")).to eq "

Help!

" end diff --git a/spec/decorators/post_decorator_spec.rb b/spec/dummy/spec/decorators/post_decorator_spec.rb similarity index 96% rename from spec/decorators/post_decorator_spec.rb rename to spec/dummy/spec/decorators/post_decorator_spec.rb index 75aaf41d..a7635990 100755 --- a/spec/decorators/post_decorator_spec.rb +++ b/spec/dummy/spec/decorators/post_decorator_spec.rb @@ -1,6 +1,6 @@ -require 'spec_helper' +require_relative '../rails_helper' -describe PostDecorator do +RSpec.describe PostDecorator do let(:decorator) { PostDecorator.new(object) } let(:object) { Post.create } diff --git a/spec/decorators/spec_type_spec.rb b/spec/dummy/spec/decorators/spec_type_spec.rb similarity index 58% rename from spec/decorators/spec_type_spec.rb rename to spec/dummy/spec/decorators/spec_type_spec.rb index 2c52c03a..d0ac746f 100644 --- a/spec/decorators/spec_type_spec.rb +++ b/spec/dummy/spec/decorators/spec_type_spec.rb @@ -1,6 +1,6 @@ -require 'spec_helper' +require_relative '../rails_helper' -describe "A spec in this folder" do +RSpec.describe "A spec in this folder" do it "is a decorator spec" do expect(RSpec.current_example.metadata[:type]).to be :decorator end diff --git a/spec/decorators/view_context_spec.rb b/spec/dummy/spec/decorators/view_context_spec.rb similarity index 62% rename from spec/decorators/view_context_spec.rb rename to spec/dummy/spec/decorators/view_context_spec.rb index 2ba93a95..13498cf6 100644 --- a/spec/decorators/view_context_spec.rb +++ b/spec/dummy/spec/decorators/view_context_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require_relative '../rails_helper' def it_does_not_leak_view_context 2.times do @@ -9,14 +9,14 @@ def it_does_not_leak_view_context end end -describe "A decorator spec", type: :decorator do +RSpec.describe "A decorator spec", type: :decorator do it_does_not_leak_view_context end -describe "A controller spec", type: :controller do +RSpec.describe "A controller spec", type: :controller do it_does_not_leak_view_context end -describe "A mailer spec", type: :mailer do +RSpec.describe "A mailer spec", type: :mailer do it_does_not_leak_view_context end diff --git a/spec/mailers/post_mailer_spec.rb b/spec/dummy/spec/mailers/post_mailer_spec.rb similarity index 94% rename from spec/mailers/post_mailer_spec.rb rename to spec/dummy/spec/mailers/post_mailer_spec.rb index 3d6699ad..1838fef3 100644 --- a/spec/mailers/post_mailer_spec.rb +++ b/spec/dummy/spec/mailers/post_mailer_spec.rb @@ -1,6 +1,6 @@ -require 'spec_helper' +require_relative '../rails_helper' -describe PostMailer do +RSpec.describe PostMailer do describe "#decorated_email" do let(:email_body) { Capybara.string(email.body.to_s) } let(:email) { PostMailer.decorated_email(post).deliver } diff --git a/spec/dummy/spec/models/mongoid_post_spec.rb b/spec/dummy/spec/models/mongoid_post_spec.rb new file mode 100644 index 00000000..ea139585 --- /dev/null +++ b/spec/dummy/spec/models/mongoid_post_spec.rb @@ -0,0 +1,8 @@ +require_relative '../spec_helper' +require_relative '../shared_examples/decoratable' + +if defined?(Mongoid) + RSpec.describe MongoidPost do + it_behaves_like "a decoratable model" + end +end diff --git a/spec/dummy/spec/models/post_spec.rb b/spec/dummy/spec/models/post_spec.rb new file mode 100644 index 00000000..4c453fed --- /dev/null +++ b/spec/dummy/spec/models/post_spec.rb @@ -0,0 +1,6 @@ +require_relative '../spec_helper' +require_relative '../shared_examples/decoratable' + +RSpec.describe Post do + it_behaves_like "a decoratable model" +end diff --git a/spec/rails_helper.rb b/spec/dummy/spec/rails_helper.rb similarity index 100% rename from spec/rails_helper.rb rename to spec/dummy/spec/rails_helper.rb diff --git a/spec/shared_examples/decoratable.rb b/spec/dummy/spec/shared_examples/decoratable.rb similarity index 92% rename from spec/shared_examples/decoratable.rb rename to spec/dummy/spec/shared_examples/decoratable.rb index 90863bcb..331b4ad9 100644 --- a/spec/shared_examples/decoratable.rb +++ b/spec/dummy/spec/shared_examples/decoratable.rb @@ -1,4 +1,4 @@ -shared_examples_for "a decoratable model" do +RSpec.shared_examples_for "a decoratable model" do describe ".decorate" do it "applies a collection decorator to a scope" do described_class.create diff --git a/spec/dummy/spec/spec_helper.rb b/spec/dummy/spec/spec_helper.rb new file mode 100644 index 00000000..32edd580 --- /dev/null +++ b/spec/dummy/spec/spec_helper.rb @@ -0,0 +1,60 @@ +RSpec.configure do |config| + config.expect_with :rspec do |expectations| + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + config.mock_with :rspec do |mocks| + mocks.verify_partial_doubles = true + end + + config.disable_monkey_patching! + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # These two settings work together to allow you to limit a spec run + # to individual examples or groups you care about by tagging them with + # `:focus` metadata. When nothing is tagged with `:focus`, all examples + # get run. + config.filter_run :focus + config.run_all_when_everything_filtered = true + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax + # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = 'doc' + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end diff --git a/spec/models/mongoid_post_spec.rb b/spec/models/mongoid_post_spec.rb deleted file mode 100644 index 1707bd1a..00000000 --- a/spec/models/mongoid_post_spec.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'spec_helper' -require 'shared_examples/decoratable' - -if defined?(Mongoid) - describe MongoidPost do - it_behaves_like "a decoratable model" - end -end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb deleted file mode 100644 index 8bdd1926..00000000 --- a/spec/models/post_spec.rb +++ /dev/null @@ -1,6 +0,0 @@ -require 'spec_helper' -require 'shared_examples/decoratable' - -describe Post do - it_behaves_like "a decoratable model" -end From adfa1a4ea7bdb5f1cb00a1980511df80cc78b16b Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 20:24:34 -0600 Subject: [PATCH 08/31] Updated spec/draper to RSpec 3.0 syntax Updated all `should` to `expect` for draper specs, and changed all `object.stub(method)` to `allow(object).to receive(:method)` --- spec/draper/collection_decorator_spec.rb | 20 ++-- spec/draper/decoratable/equality_spec.rb | 2 +- spec/draper/decoratable_spec.rb | 25 ++--- spec/draper/decorated_association_spec.rb | 22 ++-- spec/draper/decorates_assigned_spec.rb | 16 +-- spec/draper/decorator_spec.rb | 102 +++++++++++------- spec/draper/factory_spec.rb | 52 ++++----- spec/draper/finders_spec.rb | 44 ++++---- spec/draper/helper_proxy_spec.rb | 8 +- spec/draper/lazy_helpers_spec.rb | 6 +- spec/draper/undecorate_spec.rb | 2 +- .../view_context/build_strategy_spec.rb | 13 +-- spec/draper/view_context_spec.rb | 44 ++++---- spec/draper/view_helpers_spec.rb | 2 +- 14 files changed, 196 insertions(+), 162 deletions(-) diff --git a/spec/draper/collection_decorator_spec.rb b/spec/draper/collection_decorator_spec.rb index d2e256ca..de3b7dd1 100644 --- a/spec/draper/collection_decorator_spec.rb +++ b/spec/draper/collection_decorator_spec.rb @@ -2,7 +2,7 @@ require 'support/shared_examples/view_helpers' module Draper - describe CollectionDecorator do + RSpec.describe CollectionDecorator do it_behaves_like "view helpers", CollectionDecorator.new([]) describe "#initialize" do @@ -63,7 +63,7 @@ module Draper it "does not trigger decoration" do decorator = CollectionDecorator.new([]) - decorator.should_not_receive(:decorated_collection) + expect(decorator).to_not receive(:decorated_collection) decorator.context = {other: "context"} end @@ -110,12 +110,12 @@ module Draper protect_class ProductsDecorator it "defaults the :to option to :object" do - Object.should_receive(:delegate).with(:foo, :bar, to: :object) + expect(Object).to receive(:delegate).with(:foo, :bar, to: :object) ProductsDecorator.delegate :foo, :bar end it "does not overwrite the :to option if supplied" do - Object.should_receive(:delegate).with(:foo, :bar, to: :baz) + expect(Object).to receive(:delegate).with(:foo, :bar, to: :baz) ProductsDecorator.delegate :foo, :bar, to: :baz end end @@ -125,7 +125,7 @@ module Draper it "decorates Enumerable#find" do decorator = CollectionDecorator.new([]) - decorator.decorated_collection.should_receive(:find).and_return(:delegated) + expect(decorator.decorated_collection).to receive(:find).and_return(:delegated) expect(decorator.find{|p| p.title == "title"}).to be :delegated end end @@ -136,7 +136,7 @@ module Draper found = double(decorate: :decorated) decorator = CollectionDecorator.new(object) - object.should_receive(:find).and_return(found) + expect(object).to receive(:find).and_return(found) ActiveSupport::Deprecation.silence do expect(decorator.find(1)).to be :decorated end @@ -149,7 +149,7 @@ module Draper it "delegates to the decorated collection" do decorator = CollectionDecorator.new([]) - decorator.decorated_collection.should_receive(:to_ary).and_return(:delegated) + expect(decorator.decorated_collection).to receive(:to_ary).and_return(:delegated) expect(decorator.to_ary).to be :delegated end end @@ -157,7 +157,7 @@ module Draper it "delegates array methods to the decorated collection" do decorator = CollectionDecorator.new([]) - decorator.decorated_collection.should_receive(:[]).with(42).and_return(:delegated) + expect(decorator.decorated_collection).to receive(:[]).with(42).and_return(:delegated) expect(decorator[42]).to be :delegated end @@ -267,14 +267,14 @@ module Draper describe '#kind_of?' do it 'asks the kind of its decorated collection' do decorator = ProductsDecorator.new([]) - decorator.decorated_collection.should_receive(:kind_of?).with(Array).and_return("true") + expect(decorator.decorated_collection).to receive(:kind_of?).with(Array).and_return("true") expect(decorator.kind_of?(Array)).to eq "true" end context 'when asking the underlying collection returns false' do it 'asks the CollectionDecorator instance itself' do decorator = ProductsDecorator.new([]) - decorator.decorated_collection.stub(:kind_of?).with(::Draper::CollectionDecorator).and_return(false) + allow(decorator.decorated_collection).to receive(:kind_of?).with(::Draper::CollectionDecorator).and_return(false) expect(decorator.kind_of?(::Draper::CollectionDecorator)).to be true end end diff --git a/spec/draper/decoratable/equality_spec.rb b/spec/draper/decoratable/equality_spec.rb index c2d96627..e244c1c2 100644 --- a/spec/draper/decoratable/equality_spec.rb +++ b/spec/draper/decoratable/equality_spec.rb @@ -2,7 +2,7 @@ require 'support/shared_examples/decoratable_equality' module Draper - describe Decoratable::Equality do + RSpec.describe Decoratable::Equality do describe "#==" do it_behaves_like "decoration-aware #==", Object.new.extend(Decoratable::Equality) end diff --git a/spec/draper/decoratable_spec.rb b/spec/draper/decoratable_spec.rb index 6f608f46..a6cf9497 100644 --- a/spec/draper/decoratable_spec.rb +++ b/spec/draper/decoratable_spec.rb @@ -2,7 +2,7 @@ require 'support/shared_examples/decoratable_equality' module Draper - describe Decoratable do + RSpec.describe Decoratable do describe "#decorate" do it "returns a decorator for self" do @@ -22,7 +22,8 @@ module Draper it "uses the #decorator_class" do product = Product.new - product.stub decorator_class: OtherDecorator + allow(product).to receive(:decorator_class) { OtherDecorator } + # product.stub decorator_class: OtherDecorator expect(product.decorate).to be_an_instance_of OtherDecorator end @@ -70,7 +71,7 @@ module Draper it "delegates to .decorator_class" do product = Product.new - Product.should_receive(:decorator_class).and_return(:some_decorator) + expect(Product).to receive(:decorator_class).and_return(:some_decorator) expect(product.decorator_class).to be :some_decorator end end @@ -83,14 +84,14 @@ module Draper it "is true when #== is true" do product = Product.new - product.should_receive(:==).and_return(true) + expect(product).to receive(:==).and_return(true) expect(product === :anything).to be_truthy end it "is false when #== is false" do product = Product.new - product.should_receive(:==).and_return(false) + expect(product).to receive(:==).and_return(false) expect(product === :anything).to be_falsey end end @@ -132,17 +133,18 @@ module Draper it "calls #decorate_collection on .decorator_class" do scoped = [Product.new] - Product.stub scoping_method => scoped + allow(Product).to receive(scoping_method).and_return(scoped) + # Product.stub scoping_method => scoped - Product.decorator_class.should_receive(:decorate_collection).with(scoped, with: nil).and_return(:decorated_collection) + expect(Product.decorator_class).to receive(:decorate_collection).with(scoped, with: nil).and_return(:decorated_collection) expect(Product.decorate).to be :decorated_collection end it "accepts options" do options = {with: ProductDecorator, context: {some: "context"}} - Product.stub scoping_method => [] + allow(Product).to receive(scoping_method).and_return([]) - Product.decorator_class.should_receive(:decorate_collection).with([], options) + expect(Product.decorator_class).to receive(:decorate_collection).with([], options) Product.decorate(options) end end @@ -177,7 +179,7 @@ module Draper context "for ActiveModel classes" do it "infers the decorator from the model name" do - Namespaced::Product.stub(:model_name).and_return("Namespaced::Other") + allow(Namespaced::Product).to receive(:model_name).and_return("Namespaced::Other") expect(Namespaced::Product.decorator_class).to be Namespaced::OtherDecorator end @@ -192,11 +194,10 @@ module Draper context "when an unrelated NameError is thrown" do it "re-raises that error" do - String.any_instance.stub(:constantize) { Draper::Base } + allow_any_instance_of(String).to receive(:constantize) { Draper::Base } expect{Product.decorator_class}.to raise_error NameError, /Draper::Base/ end end end - end end diff --git a/spec/draper/decorated_association_spec.rb b/spec/draper/decorated_association_spec.rb index 3bd2de68..ad70e3be 100644 --- a/spec/draper/decorated_association_spec.rb +++ b/spec/draper/decorated_association_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' module Draper - describe DecoratedAssociation do + RSpec.describe DecoratedAssociation do describe "#initialize" do it "accepts valid options" do @@ -16,20 +16,22 @@ module Draper it "creates a factory" do options = {with: Decorator, context: {foo: "bar"}} - Factory.should_receive(:new).with(options) + expect(Factory).to receive(:new).with(options) + # Factory.should_receive(:new).with(options) DecoratedAssociation.new(double, :association, options) end describe ":with option" do it "defaults to nil" do - Factory.should_receive(:new).with(with: nil, context: anything()) + expect(Factory).to receive(:new).with(with: nil, context: anything()) + # Factory.should_receive(:new).with(with: nil, context: anything()) DecoratedAssociation.new(double, :association, {}) end end describe ":context option" do it "defaults to the identity function" do - Factory.should_receive(:new) do |options| + expect(Factory).to receive(:new) do |options| options[:context].call(:anything) == :anything end DecoratedAssociation.new(double, :association, {}) @@ -40,7 +42,7 @@ module Draper describe "#call" do it "calls the factory" do factory = double - Factory.stub new: factory + allow(Factory).to receive(:new).and_return(factory) associated = double owner_context = {foo: "bar"} object = double(association: associated) @@ -48,18 +50,18 @@ module Draper decorated_association = DecoratedAssociation.new(owner, :association, {}) decorated = double - factory.should_receive(:decorate).with(associated, context_args: owner_context).and_return(decorated) + expect(factory).to receive(:decorate).with(associated, context_args: owner_context).and_return(decorated) expect(decorated_association.call).to be decorated end it "memoizes" do factory = double - Factory.stub new: factory + allow(Factory).to receive(:new).and_return(factory) owner = double(object: double(association: double), context: {}) decorated_association = DecoratedAssociation.new(owner, :association, {}) decorated = double - factory.should_receive(:decorate).once.and_return(decorated) + allow(factory).to receive(:decorate).once.and_return(decorated) expect(decorated_association.call).to be decorated expect(decorated_association.call).to be decorated end @@ -67,14 +69,14 @@ module Draper context "when the :scope option was given" do it "applies the scope before decoration" do factory = double - Factory.stub new: factory + allow(Factory).to receive(:new).and_return(factory) scoped = double object = double(association: double(applied_scope: scoped)) owner = double(object: object, context: {}) decorated_association = DecoratedAssociation.new(owner, :association, scope: :applied_scope) decorated = double - factory.should_receive(:decorate).with(scoped, anything()).and_return(decorated) + expect(factory).to receive(:decorate).with(scoped, anything()).and_return(decorated) expect(decorated_association.call).to be decorated end end diff --git a/spec/draper/decorates_assigned_spec.rb b/spec/draper/decorates_assigned_spec.rb index 9694ca53..a7c40c1a 100644 --- a/spec/draper/decorates_assigned_spec.rb +++ b/spec/draper/decorates_assigned_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' module Draper - describe DecoratesAssigned do + RSpec.describe DecoratesAssigned do let(:controller_class) do Class.new do extend DecoratesAssigned @@ -28,14 +28,15 @@ def self.helper_methods end it "creates a factory" do - Factory.should_receive(:new).once + expect(Factory).to receive(:new).once + # Factory.should_receive(:new).once controller_class.decorates_assigned :article, :author end it "passes options to the factory" do options = {foo: "bar"} - Factory.should_receive(:new).with(options) + expect(Factory).to receive(:new).with(options) controller_class.decorates_assigned :article, :author, options end @@ -43,29 +44,28 @@ def self.helper_methods it "decorates the instance variable" do object = double factory = double - Factory.stub new: factory + allow(Factory).to receive(:new).and_return(factory) controller_class.decorates_assigned :article controller = controller_class.new controller.instance_variable_set "@article", object - factory.should_receive(:decorate).with(object, context_args: controller).and_return(:decorated) + allow(factory).to receive(:decorate).with(object, context_args: controller).and_return(:decorated) expect(controller.article).to be :decorated end it "memoizes" do factory = double - Factory.stub new: factory + allow(Factory).to receive(:new).and_return(factory) controller_class.decorates_assigned :article controller = controller_class.new - factory.should_receive(:decorate).once + expect(factory).to receive(:decorate).once controller.article controller.article end end end - end end diff --git a/spec/draper/decorator_spec.rb b/spec/draper/decorator_spec.rb index 36eb83eb..53126fe7 100755 --- a/spec/draper/decorator_spec.rb +++ b/spec/draper/decorator_spec.rb @@ -2,7 +2,7 @@ require 'support/shared_examples/view_helpers' module Draper - describe Decorator do + RSpec.describe Decorator do it_behaves_like "view helpers", Decorator.new(Model.new) describe "#initialize" do @@ -73,7 +73,7 @@ module Draper decorated = OtherDecorator.new(Decorator.new(Model.new)) warning_message = nil - Object.any_instance.stub(:warn) { |instance, message| warning_message = message } + allow_any_instance_of(Object).to receive(:warn) { |instance, message| warning_message = message } expect{Decorator.new(decorated)}.to change{warning_message} expect(warning_message).to start_with "Reapplying Draper::Decorator" @@ -82,7 +82,9 @@ module Draper it "decorates anyway" do decorated = OtherDecorator.new(Decorator.new(Model.new)) - Object.any_instance.stub(:warn) + + allow_any_instance_of(Object).to receive(:warn) + # Object.any_instance.stub(:warn) redecorated = Decorator.decorate(decorated) expect(redecorated.object).to be decorated @@ -102,7 +104,10 @@ module Draper describe ".decorate_collection" do describe "options validation" do - before { CollectionDecorator.stub(:new) } + before do + allow(CollectionDecorator).to receive(:new) + end + # before { CollectionDecorator.stub(:new) } it "does not raise error on valid options" do valid_options = {with: OtherDecorator, context: {}} @@ -118,14 +123,15 @@ module Draper it "creates a CollectionDecorator using itself for each item" do object = [Model.new] - CollectionDecorator.should_receive(:new).with(object, with: Decorator) + expect(CollectionDecorator).to receive(:new).with(object, with: Decorator) + # CollectionDecorator.should_receive(:new).with(object, with: Decorator) Decorator.decorate_collection(object) end it "passes options to the collection decorator" do options = {with: OtherDecorator, context: {some: "context"}} - CollectionDecorator.should_receive(:new).with([], options) + expect(CollectionDecorator).to receive(:new).with([], options) Decorator.decorate_collection([], options) end end @@ -134,21 +140,22 @@ module Draper it "creates a custom collection decorator using itself for each item" do object = [Model.new] - ProductsDecorator.should_receive(:new).with(object, with: ProductDecorator) + expect(ProductsDecorator).to receive(:new).with(object, with: ProductDecorator) ProductDecorator.decorate_collection(object) end it "passes options to the collection decorator" do options = {with: OtherDecorator, context: {some: "context"}} - ProductsDecorator.should_receive(:new).with([], options) + expect(ProductsDecorator).to receive(:new).with([], options) ProductDecorator.decorate_collection([], options) end end context "when a NameError is thrown" do it "re-raises that error" do - String.any_instance.stub(:constantize) { Draper::DecoratedEnumerableProxy } + allow_any_instance_of(String).to receive(:constantize) { Draper::DecoratedEnumerableProxy } + # String.any_instance.stub(:constantize) { Draper::DecoratedEnumerableProxy } expect{ProductDecorator.decorate_collection([])}.to raise_error NameError, /Draper::DecoratedEnumerableProxy/ end end @@ -208,7 +215,8 @@ module Draper context "when an unrelated NameError is thrown" do it "re-raises that error" do - String.any_instance.stub(:constantize) { SomethingThatDoesntExist } + allow_any_instance_of(String).to receive(:constantize) { SomethingThatDoesntExist } + # String.any_instance.stub(:constantize) { SomethingThatDoesntExist } expect{ProductDecorator.object_class}.to raise_error NameError, /SomethingThatDoesntExist/ end end @@ -221,19 +229,22 @@ module Draper describe ".object_class?" do it "returns truthy when .object_class is set" do - Decorator.stub(:object_class).and_return(Model) + allow(Decorator).to receive(:object_class).and_return(Model) + # Decorator.stub(:object_class).and_return(Model) expect(Decorator.object_class?).to be_truthy end it "returns false when .object_class is not inferrable" do - Decorator.stub(:object_class).and_raise(UninferrableSourceError.new(Decorator)) + allow(Decorator).to receive(:object_class).and_raise(UninferrableSourceError.new(Decorator)) + # Decorator.stub(:object_class).and_raise(UninferrableSourceError.new(Decorator)) expect(Decorator.object_class?).to be_falsey end it "is aliased to .source_class?" do - Decorator.stub(:object_class).and_return(Model) + allow(Decorator).to receive(:object_class).and_return(Model) + # Decorator.stub(:object_class).and_return(Model) expect(Decorator.source_class?).to be_truthy end @@ -243,7 +254,10 @@ module Draper protect_class Decorator describe "options validation" do - before { DecoratedAssociation.stub(:new).and_return(->{}) } + before do + allow(DecoratedAssociation).to receive(:new).and_return(->{}) + end + it "does not raise error on valid options" do valid_options = {with: Class, scope: :sorted, context: {}} @@ -261,7 +275,8 @@ module Draper Decorator.decorates_association :children, options decorator = Decorator.new(Model.new) - DecoratedAssociation.should_receive(:new).with(decorator, :children, options).and_return(->{}) + expect(DecoratedAssociation).to receive(:new).with(decorator, :children, options).and_return(->{}) + # DecoratedAssociation.should_receive(:new).with(decorator, :children, options).and_return(->{}) decorator.children end @@ -269,7 +284,8 @@ module Draper Decorator.decorates_association :children decorator = Decorator.new(Model.new) - DecoratedAssociation.should_receive(:new).once.and_return(->{}) + expect(DecoratedAssociation).to receive(:new).once.and_return(->{}) + # DecoratedAssociation.should_receive(:new).once.and_return(->{}) decorator.children decorator.children end @@ -278,9 +294,10 @@ module Draper Decorator.decorates_association :children decorator = Decorator.new(Model.new) decorated_association = ->{} - DecoratedAssociation.stub(:new).and_return(decorated_association) + allow(DecoratedAssociation).to receive(:new).and_return(decorated_association) - decorated_association.should_receive(:call).and_return(:decorated) + expect(decorated_association).to receive(:call).and_return(:decorated) + # decorated_association.should_receive(:call).and_return(:decorated) expect(decorator.children).to be :decorated end end @@ -290,16 +307,20 @@ module Draper protect_class Decorator it "decorates each of the associations" do - Decorator.should_receive(:decorates_association).with(:friends, {}) - Decorator.should_receive(:decorates_association).with(:enemies, {}) + expect(Decorator).to receive(:decorates_association).with(:friends, {}) + expect(Decorator).to receive(:decorates_association).with(:enemies, {}) + # Decorator.should_receive(:decorates_association).with(:friends, {}) + # Decorator.should_receive(:decorates_association).with(:enemies, {}) Decorator.decorates_associations :friends, :enemies end it "dispatches options" do options = {with: Class.new, scope: :foo, context: {}} - Decorator.should_receive(:decorates_association).with(:friends, options) - Decorator.should_receive(:decorates_association).with(:enemies, options) + expect(Decorator).to receive(:decorates_association).with(:friends, options) + expect(Decorator).to receive(:decorates_association).with(:enemies, options) + # Decorator.should_receive(:decorates_association).with(:friends, options) + # Decorator.should_receive(:decorates_association).with(:enemies, options) Decorator.decorates_associations :friends, :enemies, options end end @@ -480,7 +501,8 @@ module Draper describe "#attributes" do it "returns only the object's attributes that are implemented by the decorator" do decorator = Decorator.new(double(attributes: {foo: "bar", baz: "qux"})) - decorator.stub(:foo) + allow(decorator).to receive(:foo) + # decorator.stub(:foo) expect(decorator.attributes).to eq({foo: "bar"}) end @@ -488,7 +510,8 @@ module Draper describe ".model_name" do it "delegates to the source class" do - Decorator.stub object_class: double(model_name: :delegated) + allow(Decorator).to receive(:object_class) { double(model_name: :delegated) } + # Decorator.stub object_class: double(model_name: :delegated) expect(Decorator.model_name).to be :delegated end @@ -514,7 +537,7 @@ module Draper decorator = Decorator.new(object) other = double(object: Model.new) - object.should_receive(:==).with(other).and_return(true) + expect(object).to receive(:==).with(other).and_return(true) expect(decorator == other).to be_truthy end @@ -523,7 +546,7 @@ module Draper decorator = Decorator.new(object) other = double(object: Model.new) - object.should_receive(:==).with(other).and_return(false) + expect(object).to receive(:==).with(other).and_return(false) expect(decorator == other).to be_falsey end end @@ -538,7 +561,8 @@ module Draper it "is false when #== is false" do decorator = Decorator.new(Model.new) - decorator.stub(:==).with(:anything).and_return(false) + allow(decorator).to receive(:==).with(:anything).and_return(false) + # decorator.stub(:==).with(:anything).and_return(false) expect(decorator === :anything).to be_falsey end @@ -574,12 +598,12 @@ module Draper protect_class Decorator it "defaults the :to option to :object" do - Object.should_receive(:delegate).with(:foo, :bar, to: :object) + expect(Object).to receive(:delegate).with(:foo, :bar, to: :object) Decorator.delegate :foo, :bar end it "does not overwrite the :to option if supplied" do - Object.should_receive(:delegate).with(:foo, :bar, to: :baz) + expect(Object).to receive(:delegate).with(:foo, :bar, to: :baz) Decorator.delegate :foo, :bar, to: :baz end end @@ -606,7 +630,7 @@ module Draper it "passes blocks to delegated methods" do object = Model.new - object.stub(:hello_world) { |*args, &block| block.call } + allow(object).to receive(:hello_world) { |*args, &block| block.call } decorator = Decorator.new(object) expect(decorator.hello_world{:yielded}).to be :yielded @@ -620,7 +644,8 @@ module Draper it "delegates already-delegated methods" do object = Class.new{ delegate :bar, to: :foo }.new - object.stub foo: double(bar: :delegated) + allow(object).to receive(:foo) { double(bar: :delegated) } + # object.stub foo: double(bar: :delegated) decorator = Decorator.new(object) expect(decorator.bar).to be :delegated @@ -652,14 +677,16 @@ module Draper context "with a source class" do it "delegates methods that exist on the source class" do object_class = Class.new - object_class.stub hello_world: :delegated - Decorator.stub object_class: object_class + allow(object_class).to receive(:hello_world).and_return(:delegated) + # object_class.stub hello_world: :delegated + allow(Decorator).to receive(:object_class).and_return(object_class) expect(Decorator.hello_world).to be :delegated end it "does not delegate methods that do not exist on the source class" do - Decorator.stub object_class: Class.new + allow(Decorator).to receive(:object_class) { Class.new } + # Decorator.stub object_class: Class.new expect{Decorator.hello_world}.to raise_error NoMethodError end @@ -713,13 +740,13 @@ module Draper context "with a source class" do it "returns true for its own class methods" do Decorator.class_eval{def self.hello_world; end} - Decorator.stub object_class: Class.new + allow(Decorator).to receive(:object_class) { Class.new } expect(Decorator).to respond_to :hello_world end it "returns true for the source's class methods" do - Decorator.stub object_class: double(hello_world: :delegated) + allow(Decorator).to receive(:object_class) { double(hello_world: :delegated) } expect(Decorator).to respond_to :hello_world end @@ -737,7 +764,8 @@ module Draper describe ".respond_to_missing?" do it "allows .method to be called on delegated class methods" do - Decorator.stub object_class: double(hello_world: :delegated) + allow(Decorator).to receive(:object_class) { double(hello_world: :delegated) } + # Decorator.stub object_class: double(hello_world: :delegated) expect(Decorator.method(:hello_world)).not_to be_nil end diff --git a/spec/draper/factory_spec.rb b/spec/draper/factory_spec.rb index 03a4296d..3a096743 100644 --- a/spec/draper/factory_spec.rb +++ b/spec/draper/factory_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' module Draper - describe Factory do + RSpec.describe Factory do describe "#initialize" do it "accepts valid options" do @@ -27,7 +27,7 @@ module Draper factory = Factory.new worker = ->(*){ :decorated } - Factory::Worker.should_receive(:new).and_return(worker) + allow(Factory::Worker).to receive(:new).and_return(worker) expect(factory.decorate(double)).to be :decorated end @@ -35,7 +35,7 @@ module Draper factory = Factory.new object = double - Factory::Worker.should_receive(:new).with(anything(), object).and_return(->(*){}) + allow(Factory::Worker).to receive(:new).with(anything(), object).and_return(->(*){}) factory.decorate(object) end @@ -44,7 +44,7 @@ module Draper decorator_class = double factory = Factory.new(with: decorator_class) - Factory::Worker.should_receive(:new).with(decorator_class, anything()).and_return(->(*){}) + allow(Factory::Worker).to receive(:new).with(decorator_class, anything).and_return(->(*){}) factory.decorate(double) end end @@ -53,7 +53,7 @@ module Draper it "passes nil to the worker" do factory = Factory.new - Factory::Worker.should_receive(:new).with(nil, anything()).and_return(->(*){}) + allow(Factory::Worker).to receive(:new).with(nil, anything()).and_return(->(*){}) factory.decorate(double) end end @@ -61,10 +61,10 @@ module Draper it "passes options to the call" do factory = Factory.new worker = ->(*){} - Factory::Worker.stub new: worker + allow(Factory::Worker).to receive(:new).and_return(worker) options = {foo: "bar"} - worker.should_receive(:call).with(options) + expect(worker).to receive(:call).with(options) factory.decorate(double, options) end @@ -72,18 +72,18 @@ module Draper it "sets the passed context" do factory = Factory.new(context: {foo: "bar"}) worker = ->(*){} - Factory::Worker.stub new: worker + allow(Factory::Worker).to receive(:new).and_return(worker) - worker.should_receive(:call).with(baz: "qux", context: {foo: "bar"}) + expect(worker).to receive(:call).with(baz: 'qux', context: { foo: 'bar' }) factory.decorate(double, {baz: "qux"}) end it "is overridden by explicitly-specified context" do factory = Factory.new(context: {foo: "bar"}) worker = ->(*){} - Factory::Worker.stub new: worker + allow(Factory::Worker).to receive(:new) { worker } - worker.should_receive(:call).with(context: {baz: "qux"}) + expect(worker).to receive(:call).with(context: {baz: "qux"}) factory.decorate(double, context: {baz: "qux"}) end end @@ -91,7 +91,7 @@ module Draper end - describe Factory::Worker do + RSpec.describe Factory::Worker do describe "#call" do it "calls the decorator method" do @@ -99,9 +99,9 @@ module Draper options = {foo: "bar"} worker = Factory::Worker.new(double, object) decorator = ->(*){} - allow(worker).to receive(:decorator){ decorator } + allow(worker).to receive(:decorator){ decorator } - decorator.should_receive(:call).with(object, options).and_return(:decorated) + allow(decorator).to receive(:call).with(object, options).and_return(:decorated) expect(worker.call(options)).to be :decorated end @@ -109,29 +109,29 @@ module Draper it "calls it" do worker = Factory::Worker.new(double, double) decorator = ->(*){} - worker.stub decorator: decorator + allow(worker).to receive(:decorator) { decorator } context = {foo: "bar"} - decorator.should_receive(:call).with(anything(), context: context) + expect(decorator).to receive(:call).with(anything(), context: context) worker.call(context: ->{ context }) end it "receives arguments from the :context_args option" do worker = Factory::Worker.new(double, double) - worker.stub decorator: ->(*){} + allow(worker).to receive(:decorator) { ->(*){} } context = ->{} - context.should_receive(:call).with(:foo, :bar) + expect(context).to receive(:call).with(:foo, :bar) worker.call(context: context, context_args: [:foo, :bar]) end it "wraps non-arrays passed to :context_args" do worker = Factory::Worker.new(double, double) - worker.stub decorator: ->(*){} + allow(worker).to receive(:decorator) { ->(*){} } context = ->{} hash = {foo: "bar"} - context.should_receive(:call).with(hash) + expect(context).to receive(:call).with(hash) worker.call(context: context, context_args: hash) end end @@ -140,10 +140,10 @@ module Draper it "doesn't call it" do worker = Factory::Worker.new(double, double) decorator = ->(*){} - worker.stub decorator: decorator + allow(worker).to receive(:decorator) { decorator } context = {foo: "bar"} - decorator.should_receive(:call).with(anything(), context: context) + expect(decorator).to receive(:call).with(anything(), context: context) worker.call(context: context) end end @@ -151,9 +151,9 @@ module Draper it "does not pass the :context_args option to the decorator" do worker = Factory::Worker.new(double, double) decorator = ->(*){} - worker.stub decorator: decorator + allow(worker).to receive(:decorator) { decorator } - decorator.should_receive(:call).with(anything(), foo: "bar") + expect(decorator).to receive(:call).with(anything(), foo: "bar") worker.call(foo: "bar", context_args: []) end end @@ -176,7 +176,7 @@ module Draper options = {foo: "bar"} worker = Factory::Worker.new(nil, object) - object.should_receive(:decorate).with(options).and_return(:decorated) + allow(object).to receive(:decorate).with(options).and_return(:decorated) expect(worker.decorator.call(object, options)).to be :decorated end end @@ -231,7 +231,7 @@ module Draper allow(object).to receive(:decorate){ nil } worker = Factory::Worker.new(nil, object) - decorator_class.should_receive(:decorate_collection).with(object, foo: "bar", with: nil).and_return(:decorated) + allow(decorator_class).to receive(:decorate_collection).with(object, foo: "bar", with: nil).and_return(:decorated) expect(worker.decorator.call(object, foo: "bar")).to be :decorated end end diff --git a/spec/draper/finders_spec.rb b/spec/draper/finders_spec.rb index 5717d8e7..a483b996 100644 --- a/spec/draper/finders_spec.rb +++ b/spec/draper/finders_spec.rb @@ -1,26 +1,26 @@ require 'spec_helper' module Draper - describe Finders do + RSpec.describe Finders do protect_class ProductDecorator before { ProductDecorator.decorates_finders } describe ".find" do it "proxies to the model class" do - Product.should_receive(:find).with(1) + expect(Product).to receive(:find).with(1) ProductDecorator.find(1) end it "decorates the result" do found = Product.new - Product.stub(:find).and_return(found) + allow(Product).to receive(:find).and_return(found) decorator = ProductDecorator.find(1) expect(decorator).to be_a ProductDecorator expect(decorator.object).to be found end it "passes context to the decorator" do - Product.stub(:find) + allow(Product).to receive(:find) context = {some: "context"} decorator = ProductDecorator.find(1, context: context) @@ -30,40 +30,40 @@ module Draper describe ".find_by_(x)" do it "proxies to the model class" do - Product.should_receive(:find_by_name).with("apples") + expect(Product).to receive(:find_by_name).with("apples") ProductDecorator.find_by_name("apples") end it "decorates the result" do found = Product.new - Product.stub(:find_by_name).and_return(found) + allow(Product).to receive(:find_by_name).and_return(found) decorator = ProductDecorator.find_by_name("apples") expect(decorator).to be_a ProductDecorator expect(decorator.object).to be found end it "proxies complex ProductDecorators" do - Product.should_receive(:find_by_name_and_size).with("apples", "large") + expect(Product).to receive(:find_by_name_and_size).with("apples", "large") ProductDecorator.find_by_name_and_size("apples", "large") end it "proxies find_last_by_(x) ProductDecorators" do - Product.should_receive(:find_last_by_name_and_size).with("apples", "large") + expect(Product).to receive(:find_last_by_name_and_size).with("apples", "large") ProductDecorator.find_last_by_name_and_size("apples", "large") end it "proxies find_or_initialize_by_(x) ProductDecorators" do - Product.should_receive(:find_or_initialize_by_name_and_size).with("apples", "large") + expect(Product).to receive(:find_or_initialize_by_name_and_size).with("apples", "large") ProductDecorator.find_or_initialize_by_name_and_size("apples", "large") end it "proxies find_or_create_by_(x) ProductDecorators" do - Product.should_receive(:find_or_create_by_name_and_size).with("apples", "large") + expect(Product).to receive(:find_or_create_by_name_and_size).with("apples", "large") ProductDecorator.find_or_create_by_name_and_size("apples", "large") end it "passes context to the decorator" do - Product.stub(:find_by_name_and_size) + allow(Product).to receive(:find_by_name_and_size) context = {some: "context"} decorator = ProductDecorator.find_by_name_and_size("apples", "large", context: context) @@ -73,13 +73,13 @@ module Draper describe ".find_all_by_" do it "proxies to the model class" do - Product.should_receive(:find_all_by_name_and_size).with("apples", "large").and_return([]) + expect(Product).to receive(:find_all_by_name_and_size).with("apples", "large").and_return([]) ProductDecorator.find_all_by_name_and_size("apples", "large") end it "decorates the result" do found = [Product.new, Product.new] - Product.stub(:find_all_by_name).and_return(found) + allow(Product).to receive(:find_all_by_name).and_return(found) decorator = ProductDecorator.find_all_by_name("apples") expect(decorator).to be_a Draper::CollectionDecorator @@ -88,7 +88,7 @@ module Draper end it "passes context to the decorator" do - Product.stub(:find_all_by_name) + allow(Product).to receive(:find_all_by_name) context = {some: "context"} decorator = ProductDecorator.find_all_by_name("apples", context: context) @@ -99,7 +99,7 @@ module Draper describe ".all" do it "returns a decorated collection" do found = [Product.new, Product.new] - Product.stub all: found + allow(Product).to receive(:all).and_return(found) decorator = ProductDecorator.all expect(decorator).to be_a Draper::CollectionDecorator @@ -108,7 +108,7 @@ module Draper end it "passes context to the decorator" do - Product.stub(:all) + allow(Product).to receive(:all) context = {some: "context"} decorator = ProductDecorator.all(context: context) @@ -118,20 +118,20 @@ module Draper describe ".first" do it "proxies to the model class" do - Product.should_receive(:first) + expect(Product).to receive(:first) ProductDecorator.first end it "decorates the result" do first = Product.new - Product.stub(:first).and_return(first) + allow(Product).to receive(:first).and_return(first) decorator = ProductDecorator.first expect(decorator).to be_a ProductDecorator expect(decorator.object).to be first end it "passes context to the decorator" do - Product.stub(:first) + allow(Product).to receive(:first) context = {some: "context"} decorator = ProductDecorator.first(context: context) @@ -141,20 +141,20 @@ module Draper describe ".last" do it "proxies to the model class" do - Product.should_receive(:last) + expect(Product).to receive(:last) ProductDecorator.last end it "decorates the result" do last = Product.new - Product.stub(:last).and_return(last) + allow(Product).to receive(:last).and_return(last) decorator = ProductDecorator.last expect(decorator).to be_a ProductDecorator expect(decorator.object).to be last end it "passes context to the decorator" do - Product.stub(:last) + allow(Product).to receive(:last) context = {some: "context"} decorator = ProductDecorator.last(context: context) diff --git a/spec/draper/helper_proxy_spec.rb b/spec/draper/helper_proxy_spec.rb index ce5312b9..f6a93c45 100644 --- a/spec/draper/helper_proxy_spec.rb +++ b/spec/draper/helper_proxy_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' module Draper - describe HelperProxy do + RSpec.describe HelperProxy do describe "#initialize" do it "sets the view context" do view_context = double @@ -18,7 +18,8 @@ module Draper view_context = double helper_proxy = HelperProxy.new(view_context) - view_context.stub(:foo) { |arg| arg } + allow(view_context).to receive(:foo) { |arg| arg } + # view_context.stub(:foo) { |arg| arg } expect(helper_proxy.foo(:passed)).to be :passed end @@ -26,7 +27,8 @@ module Draper view_context = double helper_proxy = HelperProxy.new(view_context) - view_context.stub(:foo) { |&block| block.call } + allow(view_context).to receive(:foo) { |&block| block.call } + # view_context.stub(:foo) { |&block| block.call } expect(helper_proxy.foo{:yielded}).to be :yielded end diff --git a/spec/draper/lazy_helpers_spec.rb b/spec/draper/lazy_helpers_spec.rb index e1506029..f2354df3 100644 --- a/spec/draper/lazy_helpers_spec.rb +++ b/spec/draper/lazy_helpers_spec.rb @@ -1,19 +1,19 @@ require 'spec_helper' module Draper - describe LazyHelpers do + RSpec.describe LazyHelpers do describe "#method_missing" do let(:decorator) do Struct.new(:helpers){include Draper::LazyHelpers}.new(double) end it "proxies methods to #helpers" do - decorator.helpers.stub(:foo) { |arg| arg } + allow(decorator.helpers).to receive(:foo) { |arg| arg } expect(decorator.foo(:passed)).to be :passed end it "passes blocks" do - decorator.helpers.stub(:foo) { |&block| block.call } + allow(decorator.helpers).to receive(:foo) { |&block| block.call } expect(decorator.foo{:yielded}).to be :yielded end end diff --git a/spec/draper/undecorate_spec.rb b/spec/draper/undecorate_spec.rb index c0aff84f..e22da3e3 100644 --- a/spec/draper/undecorate_spec.rb +++ b/spec/draper/undecorate_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Draper, '.undecorate' do +RSpec.describe Draper, '.undecorate' do it 'undecorates a decorated object' do object = Model.new decorator = Draper::Decorator.new(object) diff --git a/spec/draper/view_context/build_strategy_spec.rb b/spec/draper/view_context/build_strategy_spec.rb index abb154c9..8ef8c964 100644 --- a/spec/draper/view_context/build_strategy_spec.rb +++ b/spec/draper/view_context/build_strategy_spec.rb @@ -9,12 +9,13 @@ def fake_controller(view_context = fake_view_context) end module Draper - describe ViewContext::BuildStrategy::Full do + RSpec.describe ViewContext::BuildStrategy::Full do describe "#call" do context "when a current controller is set" do it "returns the controller's view context" do view_context = fake_view_context - ViewContext.stub controller: fake_controller(view_context) + allow(ViewContext).to receive(:controller) { fake_controller(view_context) } + # ViewContext.stub controller: fake_controller(view_context) strategy = ViewContext::BuildStrategy::Full.new expect(strategy.call).to be view_context @@ -33,7 +34,7 @@ module Draper it "adds a request if one is not defined" do controller = Class.new(ActionController::Base).new - ViewContext.stub controller: controller + allow(ViewContext).to receive(:controller) { controller } strategy = ViewContext::BuildStrategy::Full.new expect(controller.request).to be_nil @@ -47,7 +48,7 @@ module Draper end it "adds methods to the view context from the constructor block" do - ViewContext.stub controller: fake_controller + allow(ViewContext).to receive(:controller) { fake_controller } strategy = ViewContext::BuildStrategy::Full.new do def a_helper_method; end end @@ -57,7 +58,7 @@ def a_helper_method; end it "includes modules into the view context from the constructor block" do view_context = Object.new - ViewContext.stub controller: fake_controller(view_context) + allow(ViewContext).to receive(:controller) { fake_controller(view_context) } helpers = Module.new do def a_helper_method; end end @@ -70,7 +71,7 @@ def a_helper_method; end end end - describe ViewContext::BuildStrategy::Fast do + RSpec.describe ViewContext::BuildStrategy::Fast do describe "#call" do it "returns an instance of a subclass of ActionView::Base" do strategy = ViewContext::BuildStrategy::Fast.new diff --git a/spec/draper/view_context_spec.rb b/spec/draper/view_context_spec.rb index b1f06af8..6f6f2ee7 100644 --- a/spec/draper/view_context_spec.rb +++ b/spec/draper/view_context_spec.rb @@ -1,13 +1,13 @@ require 'spec_helper' module Draper - describe ViewContext do + RSpec.describe ViewContext do describe "#view_context" do let(:base) { Class.new { def view_context; :controller_view_context; end } } let(:controller) { Class.new(base) { include ViewContext } } it "saves the superclass's view context" do - ViewContext.should_receive(:current=).with(:controller_view_context) + expect(ViewContext).to receive(:current=).with(:controller_view_context) controller.new.view_context end @@ -18,7 +18,7 @@ module Draper describe ".controller" do it "returns the stored controller from RequestStore" do - RequestStore.stub store: {current_controller: :stored_controller} + allow(RequestStore).to receive(:store) { { current_controller: :stored_controller } } expect(ViewContext.controller).to be :stored_controller end @@ -27,7 +27,7 @@ module Draper describe ".controller=" do it "stores a controller in RequestStore" do store = {} - RequestStore.stub store: store + allow(RequestStore).to receive(:store).and_return(store) ViewContext.controller = :stored_controller expect(store[:current_controller]).to be :stored_controller @@ -36,25 +36,25 @@ module Draper describe ".current" do it "returns the stored view context from RequestStore" do - RequestStore.stub store: {current_view_context: :stored_view_context} + allow(RequestStore).to receive(:store) { { current_view_context: :stored_view_context } } expect(ViewContext.current).to be :stored_view_context end context "when no view context is stored" do it "builds a view context" do - RequestStore.stub store: {} - ViewContext.stub build_strategy: ->{ :new_view_context } - HelperProxy.stub(:new).with(:new_view_context).and_return(:new_helper_proxy) + allow(RequestStore).to receive(:store).and_return({}) + allow(ViewContext).to receive(:build_strategy).and_return( ->{ :new_view_context }) + allow(HelperProxy).to receive(:new).with(:new_view_context).and_return(:new_helper_proxy) expect(ViewContext.current).to be :new_helper_proxy end it "stores the built view context" do store = {} - RequestStore.stub store: store - ViewContext.stub build_strategy: ->{ :new_view_context } - HelperProxy.stub(:new).with(:new_view_context).and_return(:new_helper_proxy) + allow(RequestStore).to receive(:store).and_return(store) + allow(ViewContext).to receive(:build_strategy).and_return( ->{ :new_view_context }) + allow(HelperProxy).to receive(:new).with(:new_view_context).and_return(:new_helper_proxy) ViewContext.current expect(store[:current_view_context]).to be :new_helper_proxy @@ -65,8 +65,8 @@ module Draper describe ".current=" do it "stores a helper proxy for the view context in RequestStore" do store = {} - RequestStore.stub store: store - HelperProxy.stub(:new).with(:stored_view_context).and_return(:stored_helper_proxy) + allow(RequestStore).to receive(:store).and_return(store) + allow(HelperProxy).to receive(:new).with(:stored_view_context).and_return(:stored_helper_proxy) ViewContext.current = :stored_view_context expect(store[:current_view_context]).to be :stored_helper_proxy @@ -76,7 +76,7 @@ module Draper describe ".clear!" do it "clears the stored controller and view controller" do store = {current_controller: :stored_controller, current_view_context: :stored_view_context} - RequestStore.stub store: store + allow(RequestStore).to receive(:store).and_return(store) ViewContext.clear! expect(store).not_to have_key :current_controller @@ -86,7 +86,7 @@ module Draper describe ".build" do it "returns a new view context using the build strategy" do - ViewContext.stub build_strategy: ->{ :new_view_context } + allow(ViewContext).to receive(:build_strategy).and_return( ->{ :new_view_context }) expect(ViewContext.build).to be :new_view_context end @@ -94,17 +94,17 @@ module Draper describe ".build!" do it "returns a helper proxy for the new view context" do - ViewContext.stub build_strategy: ->{ :new_view_context } - HelperProxy.stub(:new).with(:new_view_context).and_return(:new_helper_proxy) + allow(ViewContext).to receive(:build_strategy).and_return( ->{ :new_view_context }) + allow(HelperProxy).to receive(:new).with(:new_view_context).and_return(:new_helper_proxy) expect(ViewContext.build!).to be :new_helper_proxy end it "stores the helper proxy" do store = {} - RequestStore.stub store: store - ViewContext.stub build_strategy: ->{ :new_view_context } - HelperProxy.stub(:new).with(:new_view_context).and_return(:new_helper_proxy) + allow(RequestStore).to receive(:store) { store } + allow(ViewContext).to receive(:build_strategy).and_return( ->{ :new_view_context }) + allow(HelperProxy).to receive(:new).with(:new_view_context).and_return(:new_helper_proxy) ViewContext.build! expect(store[:current_view_context]).to be :new_helper_proxy @@ -131,7 +131,7 @@ module Draper end it "passes a block to the strategy" do - ViewContext::BuildStrategy::Fast.stub(:new) { |&block| block.call } + allow(ViewContext::BuildStrategy::Fast).to receive(:new) { |&block| block.call } expect(ViewContext.test_strategy(:fast){:passed}).to be :passed end @@ -144,7 +144,7 @@ module Draper end it "passes a block to the strategy" do - ViewContext::BuildStrategy::Full.stub(:new) { |&block| block.call } + allow(ViewContext::BuildStrategy::Full).to receive(:new) { |&block| block.call } expect(ViewContext.test_strategy(:full){:passed}).to be :passed end diff --git a/spec/draper/view_helpers_spec.rb b/spec/draper/view_helpers_spec.rb index cfa41e9a..8054508f 100644 --- a/spec/draper/view_helpers_spec.rb +++ b/spec/draper/view_helpers_spec.rb @@ -2,7 +2,7 @@ require 'support/shared_examples/view_helpers' module Draper - describe ViewHelpers do + RSpec.describe ViewHelpers do it_behaves_like "view helpers", Class.new{include ViewHelpers}.new end end From eb1a9d1bbf43fedf577cdea27172e02a39ec70dc Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 20:26:19 -0600 Subject: [PATCH 09/31] update to RSpec 3.0 syntax --- spec/generators/controller/controller_generator_spec.rb | 5 +++-- spec/generators/decorator/decorator_generator_spec.rb | 7 +++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/spec/generators/controller/controller_generator_spec.rb b/spec/generators/controller/controller_generator_spec.rb index af77c1fb..b6d5d802 100644 --- a/spec/generators/controller/controller_generator_spec.rb +++ b/spec/generators/controller/controller_generator_spec.rb @@ -1,10 +1,11 @@ require 'spec_helper' +require_relative '../../dummy/spec/rails_helper' require 'rails' require 'ammeter/init' require 'generators/controller_override' require 'generators/rails/decorator_generator' -describe Rails::Generators::ControllerGenerator do +RSpec.describe Rails::Generators::ControllerGenerator do destination File.expand_path("../tmp", __FILE__) before { prepare_destination } @@ -19,4 +20,4 @@ it { should contain "class YourModelDecorator" } end end -end \ No newline at end of file +end diff --git a/spec/generators/decorator/decorator_generator_spec.rb b/spec/generators/decorator/decorator_generator_spec.rb index 08a9601f..322d4064 100644 --- a/spec/generators/decorator/decorator_generator_spec.rb +++ b/spec/generators/decorator/decorator_generator_spec.rb @@ -1,9 +1,9 @@ require 'spec_helper' -require 'rails' +require_relative '../../dummy/spec/rails_helper' require 'ammeter/init' require 'generators/rails/decorator_generator' -describe Rails::Generators::DecoratorGenerator do +RSpec.describe Rails::Generators::DecoratorGenerator do destination File.expand_path("../tmp", __FILE__) before { prepare_destination } @@ -72,6 +72,9 @@ end context "with -t=test_unit" do + before do + allow(Rails::Generators).to receive(:require).with("rails/generators/rails/test_unit/test_unit_generators") + end describe "the generated test" do subject { file("test/decorators/your_model_decorator_test.rb") } From eebd2529bd46e54de4256242e3220b5c7edd76ab Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 20:27:03 -0600 Subject: [PATCH 10/31] Update to RSpec 3.0 syntax --- spec/support/dummy_app.rb | 2 +- spec/support/matchers/have_text.rb | 1 + .../shared_examples/decoratable_equality.rb | 4 ++- spec/support/shared_examples/view_helpers.rb | 29 +++++++++++++------ 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/spec/support/dummy_app.rb b/spec/support/dummy_app.rb index c205f86f..5ab6a90a 100644 --- a/spec/support/dummy_app.rb +++ b/spec/support/dummy_app.rb @@ -7,7 +7,7 @@ class DummyApp def initialize(environment) - raise ArgumentError, "Environment must be development or production" unless ["development", "production"].include?(environment.to_s) + raise ArgumentError, 'Environment must be development or production' unless ['development', 'production', 'test'].include?(environment.to_s) @environment = environment end diff --git a/spec/support/matchers/have_text.rb b/spec/support/matchers/have_text.rb index b5010af7..fdc98e6b 100644 --- a/spec/support/matchers/have_text.rb +++ b/spec/support/matchers/have_text.rb @@ -1,3 +1,4 @@ +require 'spec_helper' require 'capybara' module HaveTextMatcher diff --git a/spec/support/shared_examples/decoratable_equality.rb b/spec/support/shared_examples/decoratable_equality.rb index 6a7f3adf..d97f7c3c 100644 --- a/spec/support/shared_examples/decoratable_equality.rb +++ b/spec/support/shared_examples/decoratable_equality.rb @@ -1,4 +1,6 @@ -shared_examples_for "decoration-aware #==" do |subject| +require 'spec_helper' + +RSpec.shared_examples_for "decoration-aware #==" do |subject| it "is true for itself" do expect(subject == subject).to be_truthy end diff --git a/spec/support/shared_examples/view_helpers.rb b/spec/support/shared_examples/view_helpers.rb index f7d06213..6675cb2e 100644 --- a/spec/support/shared_examples/view_helpers.rb +++ b/spec/support/shared_examples/view_helpers.rb @@ -1,38 +1,49 @@ -shared_examples_for "view helpers" do |subject| +require 'spec_helper' + +RSpec.shared_examples_for "view helpers" do |subject| describe "#helpers" do it "returns the current view context" do - Draper::ViewContext.stub current: :current_view_context + allow(Draper::ViewContext).to receive(:current) { :current_view_context } + # Draper::ViewContext.stub current: :current_view_context expect(subject.helpers).to be :current_view_context end it "is aliased to #h" do - Draper::ViewContext.stub current: :current_view_context + # Draper::ViewContext.stub current: :current_view_context + allow(Draper::ViewContext).to receive(:current) { :current_view_context } expect(subject.h).to be :current_view_context end end describe "#localize" do + let(:helpers) { double } + + before :each do + allow(subject).to receive(:helpers) { helpers } + allow(helpers).to receive(:localize) + end + it "delegates to #helpers" do - subject.stub helpers: double - subject.helpers.should_receive(:localize).with(:an_object, some: "parameter") + expect(helpers).to receive(:localize).with(:an_object, some: "parameter") subject.localize(:an_object, some: "parameter") end it "is aliased to #l" do - subject.stub helpers: double - subject.helpers.should_receive(:localize).with(:an_object, some: "parameter") + expect(helpers).to receive(:localize).with(:an_object, some: 'parameter') subject.l(:an_object, some: "parameter") end end describe ".helpers" do it "returns the current view context" do - Draper::ViewContext.stub current: :current_view_context + allow(Draper::ViewContext).to receive(:current) { :current_view_context } + # Draper::ViewContext.stub current: :current_view_context expect(subject.class.helpers).to be :current_view_context end it "is aliased to .h" do - Draper::ViewContext.stub current: :current_view_context + allow(Draper::ViewContext).to receive(:current) { :current_view_context } + # Draper::ViewContext.stub current: :current_view_context expect(subject.class.h).to be :current_view_context end end From bd806b56f81daade3fd1fdf18fd3fd36cd1e9224 Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 20:27:37 -0600 Subject: [PATCH 11/31] Update spec_helper to disable monkey_patching, other RSpec 3.0 updates --- spec/spec_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index cccdb8ac..d30942e4 100755 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,7 +10,7 @@ end config.mock_with :rspec do |mocks| - mocks.verify_partial_doubles = true + # mocks.verify_partial_doubles = true mocks.yield_receiver_to_any_instance_implementation_blocks = true end From 1ce287d3673da69ab81733fe0029eb6edb49b9e7 Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 20:28:24 -0600 Subject: [PATCH 12/31] Update RSpec decorator spec generator to use RSpec::Core::Version, because RSpec::Rails::Version doesn't exist. --- lib/generators/rspec/templates/decorator_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/generators/rspec/templates/decorator_spec.rb b/lib/generators/rspec/templates/decorator_spec.rb index 72ba11ad..0e0a4eee 100644 --- a/lib/generators/rspec/templates/decorator_spec.rb +++ b/lib/generators/rspec/templates/decorator_spec.rb @@ -1,9 +1,9 @@ -<% if RSpec::Rails::Version::STRING.match(/\A3\.[^10]/) %> +<% if RSpec::Core::Version::STRING.match(/\A3\.[^10]/) %> require 'rails_helper' <% else %> require 'spec_helper' <% end %> RSpec.describe <%= class_name %>Decorator, type: :decorator do - + end From 67e4ebb462517aa72e02f30b1c052fc2b3d06844 Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 20:40:21 -0600 Subject: [PATCH 13/31] update dummy fast_spec to RSpec 3.0 syntax --- spec/dummy/fast_spec/post_decorator_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/dummy/fast_spec/post_decorator_spec.rb b/spec/dummy/fast_spec/post_decorator_spec.rb index ff6abb1a..9ea89942 100644 --- a/spec/dummy/fast_spec/post_decorator_spec.rb +++ b/spec/dummy/fast_spec/post_decorator_spec.rb @@ -7,7 +7,7 @@ Post = Struct.new(:id) { extend ActiveModel::Naming } -describe PostDecorator do +RSpec.describe PostDecorator do let(:decorator) { PostDecorator.new(object) } let(:object) { Post.new(42) } @@ -32,6 +32,6 @@ end it "can't be passed implicitly to url_for" do - expect{decorator.link}.to raise_error + expect{decorator.link}.to raise_error ArgumentError, /url_for/ end end From e90d55325589fba61c5813453eb5bcc943cb4803 Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 20:40:35 -0600 Subject: [PATCH 14/31] ignore rspec_results files --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 99cf8ff8..eca02a8d 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,5 @@ spec/dummy/db/*.sqlite3 .ruby-version* .ruby-gemset* .rake_tasks +rspec_errors.txt +rspec_results.html From aa5d239cf50d5af591a634f9955a0110e96a70db Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 20:41:06 -0600 Subject: [PATCH 15/31] Add pry to development group gemfile, update Guardfile fore new guard-rspec syntax --- Gemfile | 6 ++++++ Guardfile | 15 +++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Gemfile b/Gemfile index b3e3fc49..d1190ad2 100644 --- a/Gemfile +++ b/Gemfile @@ -11,6 +11,12 @@ platforms :jruby do gem "activerecord-jdbcsqlite3-adapter", ">= 1.3.0.beta2" end +group :development, :test do + gem 'guard-rspec', require: false + gem 'ruby_gntp' + gem 'colorize' +end + version = ENV["RAILS_VERSION"] || "4.1" eval_gemfile File.expand_path("../gemfiles/#{version}.gemfile", __FILE__) diff --git a/Guardfile b/Guardfile index 2e6c3d74..ac973cd4 100644 --- a/Guardfile +++ b/Guardfile @@ -1,10 +1,11 @@ +notification :gntp, host: '127.0.0.1' + def rspec_guard(options = {}, &block) - options = { - :version => 2, - :notification => false + opts = { + :cmd => 'rspec' }.merge(options) - guard 'rspec', options, &block + guard 'rspec', opts, &block end rspec_guard :spec_paths => %w{spec/draper spec/generators} do @@ -13,14 +14,16 @@ rspec_guard :spec_paths => %w{spec/draper spec/generators} do watch('spec/spec_helper.rb') { "spec" } end -rspec_guard :spec_paths => 'spec/integration', :env => {'RAILS_ENV' => 'development'} do +rspec_guard :spec_paths => ['spec/integration'], cmd: 'RAILS_ENV=development rspec' do watch(%r{^spec/.+_spec\.rb$}) watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } watch('spec/spec_helper.rb') { "spec" } end -rspec_guard :spec_paths => 'spec/integration', :env => {'RAILS_ENV' => 'production'} do +rspec_guard :spec_paths => ['spec/integration'], cmd: 'RAILS_ENV=production rspec' do watch(%r{^spec/.+_spec\.rb$}) watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } watch('spec/spec_helper.rb') { "spec" } end + +# vim: set ts=8 sw=2 tw=0 ft=ruby et : From c4c7fdfde63f9e4916959d7c1535ec39f85f17cd Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 20:44:49 -0600 Subject: [PATCH 16/31] update integration spec for RSpec 3.0 syntax --- spec/integration/integration_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/integration/integration_spec.rb b/spec/integration/integration_spec.rb index 3175b3c4..24dc3a35 100644 --- a/spec/integration/integration_spec.rb +++ b/spec/integration/integration_spec.rb @@ -12,7 +12,7 @@ spec_types.each do |type, (path, controller)| page = app.get(path) - describe "in a #{type}" do + RSpec.describe "in a #{type}" do it "runs in the correct environment" do expect(page).to have_text(app.environment).in("#environment") end From 85ff30ebcbb585199bf695577106d538946679d0 Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 20:49:32 -0600 Subject: [PATCH 17/31] Update .travis.yml for more ruby versions --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 0591b046..ce213261 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,8 @@ services: rvm: - 2.1.5 - 2.2.1 + - 2.2.2 + - 2.2.3 env: - "RAILS_VERSION=4.0" From a4b87c2c0f14cf6737a8b773bfd1402bc3946834 Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 21:43:34 -0600 Subject: [PATCH 18/31] Ignore binstubs --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index eca02a8d..be4fe15d 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,5 @@ spec/dummy/db/*.sqlite3 .rake_tasks rspec_errors.txt rspec_results.html +bin/ +bin/ From 4a6d3eca0f476277a5b2768b9ff96c1db248b3a3 Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 21:43:50 -0600 Subject: [PATCH 19/31] update to RSpec 3.3 --- draper.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/draper.gemspec b/draper.gemspec index abfd2eba..23b8e62b 100644 --- a/draper.gemspec +++ b/draper.gemspec @@ -24,7 +24,7 @@ Gem::Specification.new do |s| s.add_development_dependency 'ammeter' s.add_development_dependency 'rake', '>= 0.9.2' - s.add_development_dependency 'rspec-rails', '~> 3.2' + s.add_development_dependency 'rspec-rails', '~> 3.3' s.add_development_dependency 'minitest-rails', '>= 1.0' s.add_development_dependency 'capybara' s.add_development_dependency 'active_model_serializers' From 80db11011abb74da3f379b6916825abfcad74158 Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 21:44:14 -0600 Subject: [PATCH 20/31] should -> is_expected.to --- .../decorator/decorator_generator_spec.rb | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/spec/generators/decorator/decorator_generator_spec.rb b/spec/generators/decorator/decorator_generator_spec.rb index 322d4064..189f98c3 100644 --- a/spec/generators/decorator/decorator_generator_spec.rb +++ b/spec/generators/decorator/decorator_generator_spec.rb @@ -15,27 +15,27 @@ describe "naming" do before { run_generator %w(YourModel) } - it { should contain "class YourModelDecorator" } + it { is_expected.to contain "class YourModelDecorator" } end describe "namespacing" do subject { file("app/decorators/namespace/your_model_decorator.rb") } before { run_generator %w(Namespace::YourModel) } - it { should contain "class Namespace::YourModelDecorator" } + it { is_expected.to contain "class Namespace::YourModelDecorator" } end describe "inheritance" do context "by default" do before { run_generator %w(YourModel) } - it { should contain "class YourModelDecorator < Draper::Decorator" } + it { is_expected.to contain "class YourModelDecorator < Draper::Decorator" } end context "with the --parent option" do before { run_generator %w(YourModel --parent=FooDecorator) } - it { should contain "class YourModelDecorator < FooDecorator" } + it { is_expected.to contain "class YourModelDecorator < FooDecorator" } end context "with an ApplicationDecorator" do @@ -47,7 +47,7 @@ before { run_generator %w(YourModel) } - it { should contain "class YourModelDecorator < ApplicationDecorator" } + it { is_expected.to contain "class YourModelDecorator < ApplicationDecorator" } end end end @@ -59,36 +59,33 @@ describe "naming" do before { run_generator %w(YourModel -t=rspec) } - it { should contain "describe YourModelDecorator" } + it { is_expected.to contain "describe YourModelDecorator" } end describe "namespacing" do subject { file("spec/decorators/namespace/your_model_decorator_spec.rb") } before { run_generator %w(Namespace::YourModel -t=rspec) } - it { should contain "describe Namespace::YourModelDecorator" } + it { is_expected.to contain "describe Namespace::YourModelDecorator" } end end end context "with -t=test_unit" do - before do - allow(Rails::Generators).to receive(:require).with("rails/generators/rails/test_unit/test_unit_generators") - end describe "the generated test" do subject { file("test/decorators/your_model_decorator_test.rb") } describe "naming" do before { run_generator %w(YourModel -t=test_unit) } - it { should contain "class YourModelDecoratorTest < Draper::TestCase" } + it { is_expected.to contain "class YourModelDecoratorTest < Draper::TestCase" } end describe "namespacing" do subject { file("test/decorators/namespace/your_model_decorator_test.rb") } before { run_generator %w(Namespace::YourModel -t=test_unit) } - it { should contain "class Namespace::YourModelDecoratorTest < Draper::TestCase" } + it { is_expected.to contain "class Namespace::YourModelDecoratorTest < Draper::TestCase" } end end end From e9d44fda3c323586bb09f16916daff331e56f2a4 Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 21:50:21 -0600 Subject: [PATCH 21/31] Modify spec/rails_helper. ActiveRecord::Migration.maintain_test_schema! is undefined in Rails 4.0, so I made that method conditional upon ENV['RAILS_VERSION'] --- spec/dummy/spec/rails_helper.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/dummy/spec/rails_helper.rb b/spec/dummy/spec/rails_helper.rb index 34e083f5..e6a4ed10 100644 --- a/spec/dummy/spec/rails_helper.rb +++ b/spec/dummy/spec/rails_helper.rb @@ -23,7 +23,9 @@ # Checks for pending migrations before tests are run. # If you are not using ActiveRecord, you can remove this line. -ActiveRecord::Migration.maintain_test_schema! +unless ENV['RAILS_VERSION'] == '4.0' + ActiveRecord::Migration.maintain_test_schema! +end RSpec.configure do |config| config.fixture_path = "#{::Rails.root}/spec/fixtures" From 84a236564456c681c7d67606b9ab029a2e1b0e85 Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Tue, 25 Aug 2015 14:16:53 -0600 Subject: [PATCH 22/31] RSpec decorator_spec generator. Fixes #668 Fix Spec spec generator because RSpec no longer prefers monkey-patching. Refactor PR #675 to be more concise re RSpec Version check, and to use RSpec::Core::Version rather than RSpec::Rails::Version, because the latter does not exist. --- lib/generators/rspec/templates/decorator_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/generators/rspec/templates/decorator_spec.rb b/lib/generators/rspec/templates/decorator_spec.rb index a44ddbd3..0e0a4eee 100644 --- a/lib/generators/rspec/templates/decorator_spec.rb +++ b/lib/generators/rspec/templates/decorator_spec.rb @@ -1,9 +1,9 @@ -<% if RSpec::Rails::Version::STRING.match(/\A3\.[^10]/) %> +<% if RSpec::Core::Version::STRING.match(/\A3\.[^10]/) %> require 'rails_helper' <% else %> require 'spec_helper' <% end %> -describe <%= class_name %>Decorator, type: :decorator do - +RSpec.describe <%= class_name %>Decorator, type: :decorator do + end From 7db14de0f0a53ca0a0e31e68d4fd7dd765f37be6 Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 15:19:08 -0600 Subject: [PATCH 23/31] Update .gitignore. Ignore all .ruby-version, .ruby-gemset, and .rake-tasks files. Ignore files generated by rspec_results or coverage reports. Ignore binstubs. --- .gitignore | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 995bc6ef..010ce165 100644 --- a/.gitignore +++ b/.gitignore @@ -14,5 +14,9 @@ vendor/bundle *.DS_Store spec/dummy/log/* spec/dummy/db/*.sqlite3 -.ruby-version -.ruby-gemset +.ruby-version* +.ruby-gemset* +.rake_tasks +rspec_errors.txt +rspec_results.html +bin/ From 89ca9307eeee740a27228bb83eb6140ce0c22f7e Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 21:43:50 -0600 Subject: [PATCH 24/31] Update to RSpec 3.3 Update gemspec to depend on rspec ~>3.3. Generate standard rails_helper and spec_helper. Make trivial configuration edits to both spec_helpers. Update spec_helper to disable monkey patching, and explicitly use new RSpec 3.0 syntax ActiveRecord::Migration.maintain_test_schema! is undefined in Rails 4.0, so I made that method conditional upon ENV['RAILS_VERSION'] --- draper.gemspec | 2 +- spec/dummy/spec/rails_helper.rb | 34 ++++++++++++++++++ spec/dummy/spec/spec_helper.rb | 62 ++++++++++++++++++++++++++++++--- spec/spec_helper.rb | 20 +++++++++-- 4 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 spec/dummy/spec/rails_helper.rb diff --git a/draper.gemspec b/draper.gemspec index abfd2eba..23b8e62b 100644 --- a/draper.gemspec +++ b/draper.gemspec @@ -24,7 +24,7 @@ Gem::Specification.new do |s| s.add_development_dependency 'ammeter' s.add_development_dependency 'rake', '>= 0.9.2' - s.add_development_dependency 'rspec-rails', '~> 3.2' + s.add_development_dependency 'rspec-rails', '~> 3.3' s.add_development_dependency 'minitest-rails', '>= 1.0' s.add_development_dependency 'capybara' s.add_development_dependency 'active_model_serializers' diff --git a/spec/dummy/spec/rails_helper.rb b/spec/dummy/spec/rails_helper.rb new file mode 100644 index 00000000..e6a4ed10 --- /dev/null +++ b/spec/dummy/spec/rails_helper.rb @@ -0,0 +1,34 @@ +ENV['RAILS_ENV'] ||= 'test' +require File.expand_path('../../config/environment', __FILE__) +# Prevent database truncation if the environment is production +abort('Rails is running in production mode!') if Rails.env.production? +require 'spec_helper' +require 'rspec/rails' +# Add additional requires below this line. Rails is not loaded until this point! + +# Requires supporting ruby files with custom matchers and macros, etc, in +# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are +# run as spec files by default. This means that files in spec/support that end +# in _spec.rb will both be required and run as specs, causing the specs to be +# run twice. It is recommended that you do not name files matching this glob to +# end with _spec.rb. You can configure this pattern with the --pattern +# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. +# +# The following line is provided for convenience purposes. It has the downside +# of increasing the boot-up time by auto-requiring all files in the support +# directory. Alternatively, in the individual `*_spec.rb` files, manually +# require only the support files necessary. +# +# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } + +# Checks for pending migrations before tests are run. +# If you are not using ActiveRecord, you can remove this line. +unless ENV['RAILS_VERSION'] == '4.0' + ActiveRecord::Migration.maintain_test_schema! +end + +RSpec.configure do |config| + config.fixture_path = "#{::Rails.root}/spec/fixtures" + config.use_transactional_fixtures = true + config.infer_spec_type_from_file_location! +end diff --git a/spec/dummy/spec/spec_helper.rb b/spec/dummy/spec/spec_helper.rb index aa3282b2..32edd580 100644 --- a/spec/dummy/spec/spec_helper.rb +++ b/spec/dummy/spec/spec_helper.rb @@ -1,8 +1,60 @@ -ENV['RAILS_ENV'] ||= 'test' -require File.expand_path('../../config/environment', __FILE__) -require 'rspec/rails' - RSpec.configure do |config| - config.expect_with(:rspec) {|c| c.syntax = :expect} + config.expect_with :rspec do |expectations| + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + config.mock_with :rspec do |mocks| + mocks.verify_partial_doubles = true + end + + config.disable_monkey_patching! + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # These two settings work together to allow you to limit a spec run + # to individual examples or groups you care about by tagging them with + # `:focus` metadata. When nothing is tagged with `:focus`, all examples + # get run. + config.filter_run :focus + config.run_all_when_everything_filtered = true + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax + # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = 'doc' + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index df3aad9e..d30942e4 100755 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,11 +5,27 @@ require 'action_controller/test_case' RSpec.configure do |config| - config.expect_with(:rspec) {|c| c.syntax = :expect} - config.order = :random + config.expect_with :rspec do |expectations| + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + config.mock_with :rspec do |mocks| + # mocks.verify_partial_doubles = true mocks.yield_receiver_to_any_instance_implementation_blocks = true end + + config.filter_run :focus + config.run_all_when_everything_filtered = true + config.disable_monkey_patching! + + if config.files_to_run.one? + config.default_formatter = 'doc' + else + config.default_formatter = 'progress' + end + + config.order = :random + Kernel.srand config.seed end class Model; include Draper::Decoratable; end From f4bb26bd534b4616fb89c428f115dd49fa7f8ca2 Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 20:24:34 -0600 Subject: [PATCH 25/31] Updated all specs to RSpec 3.0 syntax Change specs to use synax `expect(OBJECT).to EXPECTATION` rather that `OBJECT.should`. Changed all `OBJECT.stub(METHOD)` to `allow(OBJECT).to receive(:method)` Change one-liners to use syntax: `it { is_expected.to XXX }` rather than `it { should }`. --- spec/draper/collection_decorator_spec.rb | 20 ++-- spec/draper/decoratable/equality_spec.rb | 2 +- spec/draper/decoratable_spec.rb | 25 ++--- spec/draper/decorated_association_spec.rb | 22 ++-- spec/draper/decorates_assigned_spec.rb | 16 +-- spec/draper/decorator_spec.rb | 102 +++++++++++------- spec/draper/factory_spec.rb | 52 ++++----- spec/draper/finders_spec.rb | 44 ++++---- spec/draper/helper_proxy_spec.rb | 8 +- spec/draper/lazy_helpers_spec.rb | 6 +- spec/draper/undecorate_spec.rb | 2 +- .../view_context/build_strategy_spec.rb | 13 +-- spec/draper/view_context_spec.rb | 44 ++++---- spec/draper/view_helpers_spec.rb | 2 +- spec/dummy/fast_spec/post_decorator_spec.rb | 4 +- .../active_model_serializers_spec.rb | 4 +- spec/dummy/spec/decorators/devise_spec.rb | 4 +- spec/dummy/spec/decorators/helpers_spec.rb | 4 +- .../spec/decorators/post_decorator_spec.rb | 4 +- spec/dummy/spec/decorators/spec_type_spec.rb | 4 +- .../spec/decorators/view_context_spec.rb | 8 +- spec/dummy/spec/mailers/post_mailer_spec.rb | 4 +- spec/dummy/spec/models/mongoid_post_spec.rb | 6 +- spec/dummy/spec/models/post_spec.rb | 6 +- .../dummy/spec/shared_examples/decoratable.rb | 2 +- .../controller/controller_generator_spec.rb | 5 +- .../decorator/decorator_generator_spec.rb | 22 ++-- spec/integration/integration_spec.rb | 2 +- spec/spec_helper.rb | 2 +- spec/support/dummy_app.rb | 2 +- spec/support/matchers/have_text.rb | 1 + .../shared_examples/decoratable_equality.rb | 4 +- spec/support/shared_examples/view_helpers.rb | 29 +++-- 33 files changed, 262 insertions(+), 213 deletions(-) diff --git a/spec/draper/collection_decorator_spec.rb b/spec/draper/collection_decorator_spec.rb index d2e256ca..de3b7dd1 100644 --- a/spec/draper/collection_decorator_spec.rb +++ b/spec/draper/collection_decorator_spec.rb @@ -2,7 +2,7 @@ require 'support/shared_examples/view_helpers' module Draper - describe CollectionDecorator do + RSpec.describe CollectionDecorator do it_behaves_like "view helpers", CollectionDecorator.new([]) describe "#initialize" do @@ -63,7 +63,7 @@ module Draper it "does not trigger decoration" do decorator = CollectionDecorator.new([]) - decorator.should_not_receive(:decorated_collection) + expect(decorator).to_not receive(:decorated_collection) decorator.context = {other: "context"} end @@ -110,12 +110,12 @@ module Draper protect_class ProductsDecorator it "defaults the :to option to :object" do - Object.should_receive(:delegate).with(:foo, :bar, to: :object) + expect(Object).to receive(:delegate).with(:foo, :bar, to: :object) ProductsDecorator.delegate :foo, :bar end it "does not overwrite the :to option if supplied" do - Object.should_receive(:delegate).with(:foo, :bar, to: :baz) + expect(Object).to receive(:delegate).with(:foo, :bar, to: :baz) ProductsDecorator.delegate :foo, :bar, to: :baz end end @@ -125,7 +125,7 @@ module Draper it "decorates Enumerable#find" do decorator = CollectionDecorator.new([]) - decorator.decorated_collection.should_receive(:find).and_return(:delegated) + expect(decorator.decorated_collection).to receive(:find).and_return(:delegated) expect(decorator.find{|p| p.title == "title"}).to be :delegated end end @@ -136,7 +136,7 @@ module Draper found = double(decorate: :decorated) decorator = CollectionDecorator.new(object) - object.should_receive(:find).and_return(found) + expect(object).to receive(:find).and_return(found) ActiveSupport::Deprecation.silence do expect(decorator.find(1)).to be :decorated end @@ -149,7 +149,7 @@ module Draper it "delegates to the decorated collection" do decorator = CollectionDecorator.new([]) - decorator.decorated_collection.should_receive(:to_ary).and_return(:delegated) + expect(decorator.decorated_collection).to receive(:to_ary).and_return(:delegated) expect(decorator.to_ary).to be :delegated end end @@ -157,7 +157,7 @@ module Draper it "delegates array methods to the decorated collection" do decorator = CollectionDecorator.new([]) - decorator.decorated_collection.should_receive(:[]).with(42).and_return(:delegated) + expect(decorator.decorated_collection).to receive(:[]).with(42).and_return(:delegated) expect(decorator[42]).to be :delegated end @@ -267,14 +267,14 @@ module Draper describe '#kind_of?' do it 'asks the kind of its decorated collection' do decorator = ProductsDecorator.new([]) - decorator.decorated_collection.should_receive(:kind_of?).with(Array).and_return("true") + expect(decorator.decorated_collection).to receive(:kind_of?).with(Array).and_return("true") expect(decorator.kind_of?(Array)).to eq "true" end context 'when asking the underlying collection returns false' do it 'asks the CollectionDecorator instance itself' do decorator = ProductsDecorator.new([]) - decorator.decorated_collection.stub(:kind_of?).with(::Draper::CollectionDecorator).and_return(false) + allow(decorator.decorated_collection).to receive(:kind_of?).with(::Draper::CollectionDecorator).and_return(false) expect(decorator.kind_of?(::Draper::CollectionDecorator)).to be true end end diff --git a/spec/draper/decoratable/equality_spec.rb b/spec/draper/decoratable/equality_spec.rb index c2d96627..e244c1c2 100644 --- a/spec/draper/decoratable/equality_spec.rb +++ b/spec/draper/decoratable/equality_spec.rb @@ -2,7 +2,7 @@ require 'support/shared_examples/decoratable_equality' module Draper - describe Decoratable::Equality do + RSpec.describe Decoratable::Equality do describe "#==" do it_behaves_like "decoration-aware #==", Object.new.extend(Decoratable::Equality) end diff --git a/spec/draper/decoratable_spec.rb b/spec/draper/decoratable_spec.rb index 6f608f46..a6cf9497 100644 --- a/spec/draper/decoratable_spec.rb +++ b/spec/draper/decoratable_spec.rb @@ -2,7 +2,7 @@ require 'support/shared_examples/decoratable_equality' module Draper - describe Decoratable do + RSpec.describe Decoratable do describe "#decorate" do it "returns a decorator for self" do @@ -22,7 +22,8 @@ module Draper it "uses the #decorator_class" do product = Product.new - product.stub decorator_class: OtherDecorator + allow(product).to receive(:decorator_class) { OtherDecorator } + # product.stub decorator_class: OtherDecorator expect(product.decorate).to be_an_instance_of OtherDecorator end @@ -70,7 +71,7 @@ module Draper it "delegates to .decorator_class" do product = Product.new - Product.should_receive(:decorator_class).and_return(:some_decorator) + expect(Product).to receive(:decorator_class).and_return(:some_decorator) expect(product.decorator_class).to be :some_decorator end end @@ -83,14 +84,14 @@ module Draper it "is true when #== is true" do product = Product.new - product.should_receive(:==).and_return(true) + expect(product).to receive(:==).and_return(true) expect(product === :anything).to be_truthy end it "is false when #== is false" do product = Product.new - product.should_receive(:==).and_return(false) + expect(product).to receive(:==).and_return(false) expect(product === :anything).to be_falsey end end @@ -132,17 +133,18 @@ module Draper it "calls #decorate_collection on .decorator_class" do scoped = [Product.new] - Product.stub scoping_method => scoped + allow(Product).to receive(scoping_method).and_return(scoped) + # Product.stub scoping_method => scoped - Product.decorator_class.should_receive(:decorate_collection).with(scoped, with: nil).and_return(:decorated_collection) + expect(Product.decorator_class).to receive(:decorate_collection).with(scoped, with: nil).and_return(:decorated_collection) expect(Product.decorate).to be :decorated_collection end it "accepts options" do options = {with: ProductDecorator, context: {some: "context"}} - Product.stub scoping_method => [] + allow(Product).to receive(scoping_method).and_return([]) - Product.decorator_class.should_receive(:decorate_collection).with([], options) + expect(Product.decorator_class).to receive(:decorate_collection).with([], options) Product.decorate(options) end end @@ -177,7 +179,7 @@ module Draper context "for ActiveModel classes" do it "infers the decorator from the model name" do - Namespaced::Product.stub(:model_name).and_return("Namespaced::Other") + allow(Namespaced::Product).to receive(:model_name).and_return("Namespaced::Other") expect(Namespaced::Product.decorator_class).to be Namespaced::OtherDecorator end @@ -192,11 +194,10 @@ module Draper context "when an unrelated NameError is thrown" do it "re-raises that error" do - String.any_instance.stub(:constantize) { Draper::Base } + allow_any_instance_of(String).to receive(:constantize) { Draper::Base } expect{Product.decorator_class}.to raise_error NameError, /Draper::Base/ end end end - end end diff --git a/spec/draper/decorated_association_spec.rb b/spec/draper/decorated_association_spec.rb index 3bd2de68..ad70e3be 100644 --- a/spec/draper/decorated_association_spec.rb +++ b/spec/draper/decorated_association_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' module Draper - describe DecoratedAssociation do + RSpec.describe DecoratedAssociation do describe "#initialize" do it "accepts valid options" do @@ -16,20 +16,22 @@ module Draper it "creates a factory" do options = {with: Decorator, context: {foo: "bar"}} - Factory.should_receive(:new).with(options) + expect(Factory).to receive(:new).with(options) + # Factory.should_receive(:new).with(options) DecoratedAssociation.new(double, :association, options) end describe ":with option" do it "defaults to nil" do - Factory.should_receive(:new).with(with: nil, context: anything()) + expect(Factory).to receive(:new).with(with: nil, context: anything()) + # Factory.should_receive(:new).with(with: nil, context: anything()) DecoratedAssociation.new(double, :association, {}) end end describe ":context option" do it "defaults to the identity function" do - Factory.should_receive(:new) do |options| + expect(Factory).to receive(:new) do |options| options[:context].call(:anything) == :anything end DecoratedAssociation.new(double, :association, {}) @@ -40,7 +42,7 @@ module Draper describe "#call" do it "calls the factory" do factory = double - Factory.stub new: factory + allow(Factory).to receive(:new).and_return(factory) associated = double owner_context = {foo: "bar"} object = double(association: associated) @@ -48,18 +50,18 @@ module Draper decorated_association = DecoratedAssociation.new(owner, :association, {}) decorated = double - factory.should_receive(:decorate).with(associated, context_args: owner_context).and_return(decorated) + expect(factory).to receive(:decorate).with(associated, context_args: owner_context).and_return(decorated) expect(decorated_association.call).to be decorated end it "memoizes" do factory = double - Factory.stub new: factory + allow(Factory).to receive(:new).and_return(factory) owner = double(object: double(association: double), context: {}) decorated_association = DecoratedAssociation.new(owner, :association, {}) decorated = double - factory.should_receive(:decorate).once.and_return(decorated) + allow(factory).to receive(:decorate).once.and_return(decorated) expect(decorated_association.call).to be decorated expect(decorated_association.call).to be decorated end @@ -67,14 +69,14 @@ module Draper context "when the :scope option was given" do it "applies the scope before decoration" do factory = double - Factory.stub new: factory + allow(Factory).to receive(:new).and_return(factory) scoped = double object = double(association: double(applied_scope: scoped)) owner = double(object: object, context: {}) decorated_association = DecoratedAssociation.new(owner, :association, scope: :applied_scope) decorated = double - factory.should_receive(:decorate).with(scoped, anything()).and_return(decorated) + expect(factory).to receive(:decorate).with(scoped, anything()).and_return(decorated) expect(decorated_association.call).to be decorated end end diff --git a/spec/draper/decorates_assigned_spec.rb b/spec/draper/decorates_assigned_spec.rb index 9694ca53..a7c40c1a 100644 --- a/spec/draper/decorates_assigned_spec.rb +++ b/spec/draper/decorates_assigned_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' module Draper - describe DecoratesAssigned do + RSpec.describe DecoratesAssigned do let(:controller_class) do Class.new do extend DecoratesAssigned @@ -28,14 +28,15 @@ def self.helper_methods end it "creates a factory" do - Factory.should_receive(:new).once + expect(Factory).to receive(:new).once + # Factory.should_receive(:new).once controller_class.decorates_assigned :article, :author end it "passes options to the factory" do options = {foo: "bar"} - Factory.should_receive(:new).with(options) + expect(Factory).to receive(:new).with(options) controller_class.decorates_assigned :article, :author, options end @@ -43,29 +44,28 @@ def self.helper_methods it "decorates the instance variable" do object = double factory = double - Factory.stub new: factory + allow(Factory).to receive(:new).and_return(factory) controller_class.decorates_assigned :article controller = controller_class.new controller.instance_variable_set "@article", object - factory.should_receive(:decorate).with(object, context_args: controller).and_return(:decorated) + allow(factory).to receive(:decorate).with(object, context_args: controller).and_return(:decorated) expect(controller.article).to be :decorated end it "memoizes" do factory = double - Factory.stub new: factory + allow(Factory).to receive(:new).and_return(factory) controller_class.decorates_assigned :article controller = controller_class.new - factory.should_receive(:decorate).once + expect(factory).to receive(:decorate).once controller.article controller.article end end end - end end diff --git a/spec/draper/decorator_spec.rb b/spec/draper/decorator_spec.rb index 36eb83eb..53126fe7 100755 --- a/spec/draper/decorator_spec.rb +++ b/spec/draper/decorator_spec.rb @@ -2,7 +2,7 @@ require 'support/shared_examples/view_helpers' module Draper - describe Decorator do + RSpec.describe Decorator do it_behaves_like "view helpers", Decorator.new(Model.new) describe "#initialize" do @@ -73,7 +73,7 @@ module Draper decorated = OtherDecorator.new(Decorator.new(Model.new)) warning_message = nil - Object.any_instance.stub(:warn) { |instance, message| warning_message = message } + allow_any_instance_of(Object).to receive(:warn) { |instance, message| warning_message = message } expect{Decorator.new(decorated)}.to change{warning_message} expect(warning_message).to start_with "Reapplying Draper::Decorator" @@ -82,7 +82,9 @@ module Draper it "decorates anyway" do decorated = OtherDecorator.new(Decorator.new(Model.new)) - Object.any_instance.stub(:warn) + + allow_any_instance_of(Object).to receive(:warn) + # Object.any_instance.stub(:warn) redecorated = Decorator.decorate(decorated) expect(redecorated.object).to be decorated @@ -102,7 +104,10 @@ module Draper describe ".decorate_collection" do describe "options validation" do - before { CollectionDecorator.stub(:new) } + before do + allow(CollectionDecorator).to receive(:new) + end + # before { CollectionDecorator.stub(:new) } it "does not raise error on valid options" do valid_options = {with: OtherDecorator, context: {}} @@ -118,14 +123,15 @@ module Draper it "creates a CollectionDecorator using itself for each item" do object = [Model.new] - CollectionDecorator.should_receive(:new).with(object, with: Decorator) + expect(CollectionDecorator).to receive(:new).with(object, with: Decorator) + # CollectionDecorator.should_receive(:new).with(object, with: Decorator) Decorator.decorate_collection(object) end it "passes options to the collection decorator" do options = {with: OtherDecorator, context: {some: "context"}} - CollectionDecorator.should_receive(:new).with([], options) + expect(CollectionDecorator).to receive(:new).with([], options) Decorator.decorate_collection([], options) end end @@ -134,21 +140,22 @@ module Draper it "creates a custom collection decorator using itself for each item" do object = [Model.new] - ProductsDecorator.should_receive(:new).with(object, with: ProductDecorator) + expect(ProductsDecorator).to receive(:new).with(object, with: ProductDecorator) ProductDecorator.decorate_collection(object) end it "passes options to the collection decorator" do options = {with: OtherDecorator, context: {some: "context"}} - ProductsDecorator.should_receive(:new).with([], options) + expect(ProductsDecorator).to receive(:new).with([], options) ProductDecorator.decorate_collection([], options) end end context "when a NameError is thrown" do it "re-raises that error" do - String.any_instance.stub(:constantize) { Draper::DecoratedEnumerableProxy } + allow_any_instance_of(String).to receive(:constantize) { Draper::DecoratedEnumerableProxy } + # String.any_instance.stub(:constantize) { Draper::DecoratedEnumerableProxy } expect{ProductDecorator.decorate_collection([])}.to raise_error NameError, /Draper::DecoratedEnumerableProxy/ end end @@ -208,7 +215,8 @@ module Draper context "when an unrelated NameError is thrown" do it "re-raises that error" do - String.any_instance.stub(:constantize) { SomethingThatDoesntExist } + allow_any_instance_of(String).to receive(:constantize) { SomethingThatDoesntExist } + # String.any_instance.stub(:constantize) { SomethingThatDoesntExist } expect{ProductDecorator.object_class}.to raise_error NameError, /SomethingThatDoesntExist/ end end @@ -221,19 +229,22 @@ module Draper describe ".object_class?" do it "returns truthy when .object_class is set" do - Decorator.stub(:object_class).and_return(Model) + allow(Decorator).to receive(:object_class).and_return(Model) + # Decorator.stub(:object_class).and_return(Model) expect(Decorator.object_class?).to be_truthy end it "returns false when .object_class is not inferrable" do - Decorator.stub(:object_class).and_raise(UninferrableSourceError.new(Decorator)) + allow(Decorator).to receive(:object_class).and_raise(UninferrableSourceError.new(Decorator)) + # Decorator.stub(:object_class).and_raise(UninferrableSourceError.new(Decorator)) expect(Decorator.object_class?).to be_falsey end it "is aliased to .source_class?" do - Decorator.stub(:object_class).and_return(Model) + allow(Decorator).to receive(:object_class).and_return(Model) + # Decorator.stub(:object_class).and_return(Model) expect(Decorator.source_class?).to be_truthy end @@ -243,7 +254,10 @@ module Draper protect_class Decorator describe "options validation" do - before { DecoratedAssociation.stub(:new).and_return(->{}) } + before do + allow(DecoratedAssociation).to receive(:new).and_return(->{}) + end + it "does not raise error on valid options" do valid_options = {with: Class, scope: :sorted, context: {}} @@ -261,7 +275,8 @@ module Draper Decorator.decorates_association :children, options decorator = Decorator.new(Model.new) - DecoratedAssociation.should_receive(:new).with(decorator, :children, options).and_return(->{}) + expect(DecoratedAssociation).to receive(:new).with(decorator, :children, options).and_return(->{}) + # DecoratedAssociation.should_receive(:new).with(decorator, :children, options).and_return(->{}) decorator.children end @@ -269,7 +284,8 @@ module Draper Decorator.decorates_association :children decorator = Decorator.new(Model.new) - DecoratedAssociation.should_receive(:new).once.and_return(->{}) + expect(DecoratedAssociation).to receive(:new).once.and_return(->{}) + # DecoratedAssociation.should_receive(:new).once.and_return(->{}) decorator.children decorator.children end @@ -278,9 +294,10 @@ module Draper Decorator.decorates_association :children decorator = Decorator.new(Model.new) decorated_association = ->{} - DecoratedAssociation.stub(:new).and_return(decorated_association) + allow(DecoratedAssociation).to receive(:new).and_return(decorated_association) - decorated_association.should_receive(:call).and_return(:decorated) + expect(decorated_association).to receive(:call).and_return(:decorated) + # decorated_association.should_receive(:call).and_return(:decorated) expect(decorator.children).to be :decorated end end @@ -290,16 +307,20 @@ module Draper protect_class Decorator it "decorates each of the associations" do - Decorator.should_receive(:decorates_association).with(:friends, {}) - Decorator.should_receive(:decorates_association).with(:enemies, {}) + expect(Decorator).to receive(:decorates_association).with(:friends, {}) + expect(Decorator).to receive(:decorates_association).with(:enemies, {}) + # Decorator.should_receive(:decorates_association).with(:friends, {}) + # Decorator.should_receive(:decorates_association).with(:enemies, {}) Decorator.decorates_associations :friends, :enemies end it "dispatches options" do options = {with: Class.new, scope: :foo, context: {}} - Decorator.should_receive(:decorates_association).with(:friends, options) - Decorator.should_receive(:decorates_association).with(:enemies, options) + expect(Decorator).to receive(:decorates_association).with(:friends, options) + expect(Decorator).to receive(:decorates_association).with(:enemies, options) + # Decorator.should_receive(:decorates_association).with(:friends, options) + # Decorator.should_receive(:decorates_association).with(:enemies, options) Decorator.decorates_associations :friends, :enemies, options end end @@ -480,7 +501,8 @@ module Draper describe "#attributes" do it "returns only the object's attributes that are implemented by the decorator" do decorator = Decorator.new(double(attributes: {foo: "bar", baz: "qux"})) - decorator.stub(:foo) + allow(decorator).to receive(:foo) + # decorator.stub(:foo) expect(decorator.attributes).to eq({foo: "bar"}) end @@ -488,7 +510,8 @@ module Draper describe ".model_name" do it "delegates to the source class" do - Decorator.stub object_class: double(model_name: :delegated) + allow(Decorator).to receive(:object_class) { double(model_name: :delegated) } + # Decorator.stub object_class: double(model_name: :delegated) expect(Decorator.model_name).to be :delegated end @@ -514,7 +537,7 @@ module Draper decorator = Decorator.new(object) other = double(object: Model.new) - object.should_receive(:==).with(other).and_return(true) + expect(object).to receive(:==).with(other).and_return(true) expect(decorator == other).to be_truthy end @@ -523,7 +546,7 @@ module Draper decorator = Decorator.new(object) other = double(object: Model.new) - object.should_receive(:==).with(other).and_return(false) + expect(object).to receive(:==).with(other).and_return(false) expect(decorator == other).to be_falsey end end @@ -538,7 +561,8 @@ module Draper it "is false when #== is false" do decorator = Decorator.new(Model.new) - decorator.stub(:==).with(:anything).and_return(false) + allow(decorator).to receive(:==).with(:anything).and_return(false) + # decorator.stub(:==).with(:anything).and_return(false) expect(decorator === :anything).to be_falsey end @@ -574,12 +598,12 @@ module Draper protect_class Decorator it "defaults the :to option to :object" do - Object.should_receive(:delegate).with(:foo, :bar, to: :object) + expect(Object).to receive(:delegate).with(:foo, :bar, to: :object) Decorator.delegate :foo, :bar end it "does not overwrite the :to option if supplied" do - Object.should_receive(:delegate).with(:foo, :bar, to: :baz) + expect(Object).to receive(:delegate).with(:foo, :bar, to: :baz) Decorator.delegate :foo, :bar, to: :baz end end @@ -606,7 +630,7 @@ module Draper it "passes blocks to delegated methods" do object = Model.new - object.stub(:hello_world) { |*args, &block| block.call } + allow(object).to receive(:hello_world) { |*args, &block| block.call } decorator = Decorator.new(object) expect(decorator.hello_world{:yielded}).to be :yielded @@ -620,7 +644,8 @@ module Draper it "delegates already-delegated methods" do object = Class.new{ delegate :bar, to: :foo }.new - object.stub foo: double(bar: :delegated) + allow(object).to receive(:foo) { double(bar: :delegated) } + # object.stub foo: double(bar: :delegated) decorator = Decorator.new(object) expect(decorator.bar).to be :delegated @@ -652,14 +677,16 @@ module Draper context "with a source class" do it "delegates methods that exist on the source class" do object_class = Class.new - object_class.stub hello_world: :delegated - Decorator.stub object_class: object_class + allow(object_class).to receive(:hello_world).and_return(:delegated) + # object_class.stub hello_world: :delegated + allow(Decorator).to receive(:object_class).and_return(object_class) expect(Decorator.hello_world).to be :delegated end it "does not delegate methods that do not exist on the source class" do - Decorator.stub object_class: Class.new + allow(Decorator).to receive(:object_class) { Class.new } + # Decorator.stub object_class: Class.new expect{Decorator.hello_world}.to raise_error NoMethodError end @@ -713,13 +740,13 @@ module Draper context "with a source class" do it "returns true for its own class methods" do Decorator.class_eval{def self.hello_world; end} - Decorator.stub object_class: Class.new + allow(Decorator).to receive(:object_class) { Class.new } expect(Decorator).to respond_to :hello_world end it "returns true for the source's class methods" do - Decorator.stub object_class: double(hello_world: :delegated) + allow(Decorator).to receive(:object_class) { double(hello_world: :delegated) } expect(Decorator).to respond_to :hello_world end @@ -737,7 +764,8 @@ module Draper describe ".respond_to_missing?" do it "allows .method to be called on delegated class methods" do - Decorator.stub object_class: double(hello_world: :delegated) + allow(Decorator).to receive(:object_class) { double(hello_world: :delegated) } + # Decorator.stub object_class: double(hello_world: :delegated) expect(Decorator.method(:hello_world)).not_to be_nil end diff --git a/spec/draper/factory_spec.rb b/spec/draper/factory_spec.rb index 03a4296d..3a096743 100644 --- a/spec/draper/factory_spec.rb +++ b/spec/draper/factory_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' module Draper - describe Factory do + RSpec.describe Factory do describe "#initialize" do it "accepts valid options" do @@ -27,7 +27,7 @@ module Draper factory = Factory.new worker = ->(*){ :decorated } - Factory::Worker.should_receive(:new).and_return(worker) + allow(Factory::Worker).to receive(:new).and_return(worker) expect(factory.decorate(double)).to be :decorated end @@ -35,7 +35,7 @@ module Draper factory = Factory.new object = double - Factory::Worker.should_receive(:new).with(anything(), object).and_return(->(*){}) + allow(Factory::Worker).to receive(:new).with(anything(), object).and_return(->(*){}) factory.decorate(object) end @@ -44,7 +44,7 @@ module Draper decorator_class = double factory = Factory.new(with: decorator_class) - Factory::Worker.should_receive(:new).with(decorator_class, anything()).and_return(->(*){}) + allow(Factory::Worker).to receive(:new).with(decorator_class, anything).and_return(->(*){}) factory.decorate(double) end end @@ -53,7 +53,7 @@ module Draper it "passes nil to the worker" do factory = Factory.new - Factory::Worker.should_receive(:new).with(nil, anything()).and_return(->(*){}) + allow(Factory::Worker).to receive(:new).with(nil, anything()).and_return(->(*){}) factory.decorate(double) end end @@ -61,10 +61,10 @@ module Draper it "passes options to the call" do factory = Factory.new worker = ->(*){} - Factory::Worker.stub new: worker + allow(Factory::Worker).to receive(:new).and_return(worker) options = {foo: "bar"} - worker.should_receive(:call).with(options) + expect(worker).to receive(:call).with(options) factory.decorate(double, options) end @@ -72,18 +72,18 @@ module Draper it "sets the passed context" do factory = Factory.new(context: {foo: "bar"}) worker = ->(*){} - Factory::Worker.stub new: worker + allow(Factory::Worker).to receive(:new).and_return(worker) - worker.should_receive(:call).with(baz: "qux", context: {foo: "bar"}) + expect(worker).to receive(:call).with(baz: 'qux', context: { foo: 'bar' }) factory.decorate(double, {baz: "qux"}) end it "is overridden by explicitly-specified context" do factory = Factory.new(context: {foo: "bar"}) worker = ->(*){} - Factory::Worker.stub new: worker + allow(Factory::Worker).to receive(:new) { worker } - worker.should_receive(:call).with(context: {baz: "qux"}) + expect(worker).to receive(:call).with(context: {baz: "qux"}) factory.decorate(double, context: {baz: "qux"}) end end @@ -91,7 +91,7 @@ module Draper end - describe Factory::Worker do + RSpec.describe Factory::Worker do describe "#call" do it "calls the decorator method" do @@ -99,9 +99,9 @@ module Draper options = {foo: "bar"} worker = Factory::Worker.new(double, object) decorator = ->(*){} - allow(worker).to receive(:decorator){ decorator } + allow(worker).to receive(:decorator){ decorator } - decorator.should_receive(:call).with(object, options).and_return(:decorated) + allow(decorator).to receive(:call).with(object, options).and_return(:decorated) expect(worker.call(options)).to be :decorated end @@ -109,29 +109,29 @@ module Draper it "calls it" do worker = Factory::Worker.new(double, double) decorator = ->(*){} - worker.stub decorator: decorator + allow(worker).to receive(:decorator) { decorator } context = {foo: "bar"} - decorator.should_receive(:call).with(anything(), context: context) + expect(decorator).to receive(:call).with(anything(), context: context) worker.call(context: ->{ context }) end it "receives arguments from the :context_args option" do worker = Factory::Worker.new(double, double) - worker.stub decorator: ->(*){} + allow(worker).to receive(:decorator) { ->(*){} } context = ->{} - context.should_receive(:call).with(:foo, :bar) + expect(context).to receive(:call).with(:foo, :bar) worker.call(context: context, context_args: [:foo, :bar]) end it "wraps non-arrays passed to :context_args" do worker = Factory::Worker.new(double, double) - worker.stub decorator: ->(*){} + allow(worker).to receive(:decorator) { ->(*){} } context = ->{} hash = {foo: "bar"} - context.should_receive(:call).with(hash) + expect(context).to receive(:call).with(hash) worker.call(context: context, context_args: hash) end end @@ -140,10 +140,10 @@ module Draper it "doesn't call it" do worker = Factory::Worker.new(double, double) decorator = ->(*){} - worker.stub decorator: decorator + allow(worker).to receive(:decorator) { decorator } context = {foo: "bar"} - decorator.should_receive(:call).with(anything(), context: context) + expect(decorator).to receive(:call).with(anything(), context: context) worker.call(context: context) end end @@ -151,9 +151,9 @@ module Draper it "does not pass the :context_args option to the decorator" do worker = Factory::Worker.new(double, double) decorator = ->(*){} - worker.stub decorator: decorator + allow(worker).to receive(:decorator) { decorator } - decorator.should_receive(:call).with(anything(), foo: "bar") + expect(decorator).to receive(:call).with(anything(), foo: "bar") worker.call(foo: "bar", context_args: []) end end @@ -176,7 +176,7 @@ module Draper options = {foo: "bar"} worker = Factory::Worker.new(nil, object) - object.should_receive(:decorate).with(options).and_return(:decorated) + allow(object).to receive(:decorate).with(options).and_return(:decorated) expect(worker.decorator.call(object, options)).to be :decorated end end @@ -231,7 +231,7 @@ module Draper allow(object).to receive(:decorate){ nil } worker = Factory::Worker.new(nil, object) - decorator_class.should_receive(:decorate_collection).with(object, foo: "bar", with: nil).and_return(:decorated) + allow(decorator_class).to receive(:decorate_collection).with(object, foo: "bar", with: nil).and_return(:decorated) expect(worker.decorator.call(object, foo: "bar")).to be :decorated end end diff --git a/spec/draper/finders_spec.rb b/spec/draper/finders_spec.rb index 5717d8e7..a483b996 100644 --- a/spec/draper/finders_spec.rb +++ b/spec/draper/finders_spec.rb @@ -1,26 +1,26 @@ require 'spec_helper' module Draper - describe Finders do + RSpec.describe Finders do protect_class ProductDecorator before { ProductDecorator.decorates_finders } describe ".find" do it "proxies to the model class" do - Product.should_receive(:find).with(1) + expect(Product).to receive(:find).with(1) ProductDecorator.find(1) end it "decorates the result" do found = Product.new - Product.stub(:find).and_return(found) + allow(Product).to receive(:find).and_return(found) decorator = ProductDecorator.find(1) expect(decorator).to be_a ProductDecorator expect(decorator.object).to be found end it "passes context to the decorator" do - Product.stub(:find) + allow(Product).to receive(:find) context = {some: "context"} decorator = ProductDecorator.find(1, context: context) @@ -30,40 +30,40 @@ module Draper describe ".find_by_(x)" do it "proxies to the model class" do - Product.should_receive(:find_by_name).with("apples") + expect(Product).to receive(:find_by_name).with("apples") ProductDecorator.find_by_name("apples") end it "decorates the result" do found = Product.new - Product.stub(:find_by_name).and_return(found) + allow(Product).to receive(:find_by_name).and_return(found) decorator = ProductDecorator.find_by_name("apples") expect(decorator).to be_a ProductDecorator expect(decorator.object).to be found end it "proxies complex ProductDecorators" do - Product.should_receive(:find_by_name_and_size).with("apples", "large") + expect(Product).to receive(:find_by_name_and_size).with("apples", "large") ProductDecorator.find_by_name_and_size("apples", "large") end it "proxies find_last_by_(x) ProductDecorators" do - Product.should_receive(:find_last_by_name_and_size).with("apples", "large") + expect(Product).to receive(:find_last_by_name_and_size).with("apples", "large") ProductDecorator.find_last_by_name_and_size("apples", "large") end it "proxies find_or_initialize_by_(x) ProductDecorators" do - Product.should_receive(:find_or_initialize_by_name_and_size).with("apples", "large") + expect(Product).to receive(:find_or_initialize_by_name_and_size).with("apples", "large") ProductDecorator.find_or_initialize_by_name_and_size("apples", "large") end it "proxies find_or_create_by_(x) ProductDecorators" do - Product.should_receive(:find_or_create_by_name_and_size).with("apples", "large") + expect(Product).to receive(:find_or_create_by_name_and_size).with("apples", "large") ProductDecorator.find_or_create_by_name_and_size("apples", "large") end it "passes context to the decorator" do - Product.stub(:find_by_name_and_size) + allow(Product).to receive(:find_by_name_and_size) context = {some: "context"} decorator = ProductDecorator.find_by_name_and_size("apples", "large", context: context) @@ -73,13 +73,13 @@ module Draper describe ".find_all_by_" do it "proxies to the model class" do - Product.should_receive(:find_all_by_name_and_size).with("apples", "large").and_return([]) + expect(Product).to receive(:find_all_by_name_and_size).with("apples", "large").and_return([]) ProductDecorator.find_all_by_name_and_size("apples", "large") end it "decorates the result" do found = [Product.new, Product.new] - Product.stub(:find_all_by_name).and_return(found) + allow(Product).to receive(:find_all_by_name).and_return(found) decorator = ProductDecorator.find_all_by_name("apples") expect(decorator).to be_a Draper::CollectionDecorator @@ -88,7 +88,7 @@ module Draper end it "passes context to the decorator" do - Product.stub(:find_all_by_name) + allow(Product).to receive(:find_all_by_name) context = {some: "context"} decorator = ProductDecorator.find_all_by_name("apples", context: context) @@ -99,7 +99,7 @@ module Draper describe ".all" do it "returns a decorated collection" do found = [Product.new, Product.new] - Product.stub all: found + allow(Product).to receive(:all).and_return(found) decorator = ProductDecorator.all expect(decorator).to be_a Draper::CollectionDecorator @@ -108,7 +108,7 @@ module Draper end it "passes context to the decorator" do - Product.stub(:all) + allow(Product).to receive(:all) context = {some: "context"} decorator = ProductDecorator.all(context: context) @@ -118,20 +118,20 @@ module Draper describe ".first" do it "proxies to the model class" do - Product.should_receive(:first) + expect(Product).to receive(:first) ProductDecorator.first end it "decorates the result" do first = Product.new - Product.stub(:first).and_return(first) + allow(Product).to receive(:first).and_return(first) decorator = ProductDecorator.first expect(decorator).to be_a ProductDecorator expect(decorator.object).to be first end it "passes context to the decorator" do - Product.stub(:first) + allow(Product).to receive(:first) context = {some: "context"} decorator = ProductDecorator.first(context: context) @@ -141,20 +141,20 @@ module Draper describe ".last" do it "proxies to the model class" do - Product.should_receive(:last) + expect(Product).to receive(:last) ProductDecorator.last end it "decorates the result" do last = Product.new - Product.stub(:last).and_return(last) + allow(Product).to receive(:last).and_return(last) decorator = ProductDecorator.last expect(decorator).to be_a ProductDecorator expect(decorator.object).to be last end it "passes context to the decorator" do - Product.stub(:last) + allow(Product).to receive(:last) context = {some: "context"} decorator = ProductDecorator.last(context: context) diff --git a/spec/draper/helper_proxy_spec.rb b/spec/draper/helper_proxy_spec.rb index ce5312b9..f6a93c45 100644 --- a/spec/draper/helper_proxy_spec.rb +++ b/spec/draper/helper_proxy_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' module Draper - describe HelperProxy do + RSpec.describe HelperProxy do describe "#initialize" do it "sets the view context" do view_context = double @@ -18,7 +18,8 @@ module Draper view_context = double helper_proxy = HelperProxy.new(view_context) - view_context.stub(:foo) { |arg| arg } + allow(view_context).to receive(:foo) { |arg| arg } + # view_context.stub(:foo) { |arg| arg } expect(helper_proxy.foo(:passed)).to be :passed end @@ -26,7 +27,8 @@ module Draper view_context = double helper_proxy = HelperProxy.new(view_context) - view_context.stub(:foo) { |&block| block.call } + allow(view_context).to receive(:foo) { |&block| block.call } + # view_context.stub(:foo) { |&block| block.call } expect(helper_proxy.foo{:yielded}).to be :yielded end diff --git a/spec/draper/lazy_helpers_spec.rb b/spec/draper/lazy_helpers_spec.rb index e1506029..f2354df3 100644 --- a/spec/draper/lazy_helpers_spec.rb +++ b/spec/draper/lazy_helpers_spec.rb @@ -1,19 +1,19 @@ require 'spec_helper' module Draper - describe LazyHelpers do + RSpec.describe LazyHelpers do describe "#method_missing" do let(:decorator) do Struct.new(:helpers){include Draper::LazyHelpers}.new(double) end it "proxies methods to #helpers" do - decorator.helpers.stub(:foo) { |arg| arg } + allow(decorator.helpers).to receive(:foo) { |arg| arg } expect(decorator.foo(:passed)).to be :passed end it "passes blocks" do - decorator.helpers.stub(:foo) { |&block| block.call } + allow(decorator.helpers).to receive(:foo) { |&block| block.call } expect(decorator.foo{:yielded}).to be :yielded end end diff --git a/spec/draper/undecorate_spec.rb b/spec/draper/undecorate_spec.rb index c0aff84f..e22da3e3 100644 --- a/spec/draper/undecorate_spec.rb +++ b/spec/draper/undecorate_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Draper, '.undecorate' do +RSpec.describe Draper, '.undecorate' do it 'undecorates a decorated object' do object = Model.new decorator = Draper::Decorator.new(object) diff --git a/spec/draper/view_context/build_strategy_spec.rb b/spec/draper/view_context/build_strategy_spec.rb index abb154c9..8ef8c964 100644 --- a/spec/draper/view_context/build_strategy_spec.rb +++ b/spec/draper/view_context/build_strategy_spec.rb @@ -9,12 +9,13 @@ def fake_controller(view_context = fake_view_context) end module Draper - describe ViewContext::BuildStrategy::Full do + RSpec.describe ViewContext::BuildStrategy::Full do describe "#call" do context "when a current controller is set" do it "returns the controller's view context" do view_context = fake_view_context - ViewContext.stub controller: fake_controller(view_context) + allow(ViewContext).to receive(:controller) { fake_controller(view_context) } + # ViewContext.stub controller: fake_controller(view_context) strategy = ViewContext::BuildStrategy::Full.new expect(strategy.call).to be view_context @@ -33,7 +34,7 @@ module Draper it "adds a request if one is not defined" do controller = Class.new(ActionController::Base).new - ViewContext.stub controller: controller + allow(ViewContext).to receive(:controller) { controller } strategy = ViewContext::BuildStrategy::Full.new expect(controller.request).to be_nil @@ -47,7 +48,7 @@ module Draper end it "adds methods to the view context from the constructor block" do - ViewContext.stub controller: fake_controller + allow(ViewContext).to receive(:controller) { fake_controller } strategy = ViewContext::BuildStrategy::Full.new do def a_helper_method; end end @@ -57,7 +58,7 @@ def a_helper_method; end it "includes modules into the view context from the constructor block" do view_context = Object.new - ViewContext.stub controller: fake_controller(view_context) + allow(ViewContext).to receive(:controller) { fake_controller(view_context) } helpers = Module.new do def a_helper_method; end end @@ -70,7 +71,7 @@ def a_helper_method; end end end - describe ViewContext::BuildStrategy::Fast do + RSpec.describe ViewContext::BuildStrategy::Fast do describe "#call" do it "returns an instance of a subclass of ActionView::Base" do strategy = ViewContext::BuildStrategy::Fast.new diff --git a/spec/draper/view_context_spec.rb b/spec/draper/view_context_spec.rb index b1f06af8..6f6f2ee7 100644 --- a/spec/draper/view_context_spec.rb +++ b/spec/draper/view_context_spec.rb @@ -1,13 +1,13 @@ require 'spec_helper' module Draper - describe ViewContext do + RSpec.describe ViewContext do describe "#view_context" do let(:base) { Class.new { def view_context; :controller_view_context; end } } let(:controller) { Class.new(base) { include ViewContext } } it "saves the superclass's view context" do - ViewContext.should_receive(:current=).with(:controller_view_context) + expect(ViewContext).to receive(:current=).with(:controller_view_context) controller.new.view_context end @@ -18,7 +18,7 @@ module Draper describe ".controller" do it "returns the stored controller from RequestStore" do - RequestStore.stub store: {current_controller: :stored_controller} + allow(RequestStore).to receive(:store) { { current_controller: :stored_controller } } expect(ViewContext.controller).to be :stored_controller end @@ -27,7 +27,7 @@ module Draper describe ".controller=" do it "stores a controller in RequestStore" do store = {} - RequestStore.stub store: store + allow(RequestStore).to receive(:store).and_return(store) ViewContext.controller = :stored_controller expect(store[:current_controller]).to be :stored_controller @@ -36,25 +36,25 @@ module Draper describe ".current" do it "returns the stored view context from RequestStore" do - RequestStore.stub store: {current_view_context: :stored_view_context} + allow(RequestStore).to receive(:store) { { current_view_context: :stored_view_context } } expect(ViewContext.current).to be :stored_view_context end context "when no view context is stored" do it "builds a view context" do - RequestStore.stub store: {} - ViewContext.stub build_strategy: ->{ :new_view_context } - HelperProxy.stub(:new).with(:new_view_context).and_return(:new_helper_proxy) + allow(RequestStore).to receive(:store).and_return({}) + allow(ViewContext).to receive(:build_strategy).and_return( ->{ :new_view_context }) + allow(HelperProxy).to receive(:new).with(:new_view_context).and_return(:new_helper_proxy) expect(ViewContext.current).to be :new_helper_proxy end it "stores the built view context" do store = {} - RequestStore.stub store: store - ViewContext.stub build_strategy: ->{ :new_view_context } - HelperProxy.stub(:new).with(:new_view_context).and_return(:new_helper_proxy) + allow(RequestStore).to receive(:store).and_return(store) + allow(ViewContext).to receive(:build_strategy).and_return( ->{ :new_view_context }) + allow(HelperProxy).to receive(:new).with(:new_view_context).and_return(:new_helper_proxy) ViewContext.current expect(store[:current_view_context]).to be :new_helper_proxy @@ -65,8 +65,8 @@ module Draper describe ".current=" do it "stores a helper proxy for the view context in RequestStore" do store = {} - RequestStore.stub store: store - HelperProxy.stub(:new).with(:stored_view_context).and_return(:stored_helper_proxy) + allow(RequestStore).to receive(:store).and_return(store) + allow(HelperProxy).to receive(:new).with(:stored_view_context).and_return(:stored_helper_proxy) ViewContext.current = :stored_view_context expect(store[:current_view_context]).to be :stored_helper_proxy @@ -76,7 +76,7 @@ module Draper describe ".clear!" do it "clears the stored controller and view controller" do store = {current_controller: :stored_controller, current_view_context: :stored_view_context} - RequestStore.stub store: store + allow(RequestStore).to receive(:store).and_return(store) ViewContext.clear! expect(store).not_to have_key :current_controller @@ -86,7 +86,7 @@ module Draper describe ".build" do it "returns a new view context using the build strategy" do - ViewContext.stub build_strategy: ->{ :new_view_context } + allow(ViewContext).to receive(:build_strategy).and_return( ->{ :new_view_context }) expect(ViewContext.build).to be :new_view_context end @@ -94,17 +94,17 @@ module Draper describe ".build!" do it "returns a helper proxy for the new view context" do - ViewContext.stub build_strategy: ->{ :new_view_context } - HelperProxy.stub(:new).with(:new_view_context).and_return(:new_helper_proxy) + allow(ViewContext).to receive(:build_strategy).and_return( ->{ :new_view_context }) + allow(HelperProxy).to receive(:new).with(:new_view_context).and_return(:new_helper_proxy) expect(ViewContext.build!).to be :new_helper_proxy end it "stores the helper proxy" do store = {} - RequestStore.stub store: store - ViewContext.stub build_strategy: ->{ :new_view_context } - HelperProxy.stub(:new).with(:new_view_context).and_return(:new_helper_proxy) + allow(RequestStore).to receive(:store) { store } + allow(ViewContext).to receive(:build_strategy).and_return( ->{ :new_view_context }) + allow(HelperProxy).to receive(:new).with(:new_view_context).and_return(:new_helper_proxy) ViewContext.build! expect(store[:current_view_context]).to be :new_helper_proxy @@ -131,7 +131,7 @@ module Draper end it "passes a block to the strategy" do - ViewContext::BuildStrategy::Fast.stub(:new) { |&block| block.call } + allow(ViewContext::BuildStrategy::Fast).to receive(:new) { |&block| block.call } expect(ViewContext.test_strategy(:fast){:passed}).to be :passed end @@ -144,7 +144,7 @@ module Draper end it "passes a block to the strategy" do - ViewContext::BuildStrategy::Full.stub(:new) { |&block| block.call } + allow(ViewContext::BuildStrategy::Full).to receive(:new) { |&block| block.call } expect(ViewContext.test_strategy(:full){:passed}).to be :passed end diff --git a/spec/draper/view_helpers_spec.rb b/spec/draper/view_helpers_spec.rb index cfa41e9a..8054508f 100644 --- a/spec/draper/view_helpers_spec.rb +++ b/spec/draper/view_helpers_spec.rb @@ -2,7 +2,7 @@ require 'support/shared_examples/view_helpers' module Draper - describe ViewHelpers do + RSpec.describe ViewHelpers do it_behaves_like "view helpers", Class.new{include ViewHelpers}.new end end diff --git a/spec/dummy/fast_spec/post_decorator_spec.rb b/spec/dummy/fast_spec/post_decorator_spec.rb index ff6abb1a..9ea89942 100644 --- a/spec/dummy/fast_spec/post_decorator_spec.rb +++ b/spec/dummy/fast_spec/post_decorator_spec.rb @@ -7,7 +7,7 @@ Post = Struct.new(:id) { extend ActiveModel::Naming } -describe PostDecorator do +RSpec.describe PostDecorator do let(:decorator) { PostDecorator.new(object) } let(:object) { Post.new(42) } @@ -32,6 +32,6 @@ end it "can't be passed implicitly to url_for" do - expect{decorator.link}.to raise_error + expect{decorator.link}.to raise_error ArgumentError, /url_for/ end end diff --git a/spec/dummy/spec/decorators/active_model_serializers_spec.rb b/spec/dummy/spec/decorators/active_model_serializers_spec.rb index 7de50532..e4d00a8f 100644 --- a/spec/dummy/spec/decorators/active_model_serializers_spec.rb +++ b/spec/dummy/spec/decorators/active_model_serializers_spec.rb @@ -1,6 +1,6 @@ -require 'spec_helper' +require_relative '../rails_helper' -describe Draper::CollectionDecorator do +RSpec.describe Draper::CollectionDecorator do describe "#active_model_serializer" do it "returns ActiveModel::ArraySerializer" do collection_decorator = Draper::CollectionDecorator.new([]) diff --git a/spec/dummy/spec/decorators/devise_spec.rb b/spec/dummy/spec/decorators/devise_spec.rb index 6480751f..540a04d9 100644 --- a/spec/dummy/spec/decorators/devise_spec.rb +++ b/spec/dummy/spec/decorators/devise_spec.rb @@ -1,7 +1,7 @@ -require 'spec_helper' +require_relative '../rails_helper' if defined?(Devise) - describe "A decorator spec" do + RSpec.describe "A decorator spec" do it "can sign in a real user" do user = User.new sign_in user diff --git a/spec/dummy/spec/decorators/helpers_spec.rb b/spec/dummy/spec/decorators/helpers_spec.rb index eb171f89..8d32a545 100644 --- a/spec/dummy/spec/decorators/helpers_spec.rb +++ b/spec/dummy/spec/decorators/helpers_spec.rb @@ -1,6 +1,6 @@ -require 'spec_helper' +require_relative '../rails_helper' -describe "A decorator spec" do +RSpec.describe "A decorator spec" do it "can access helpers through `helper`" do expect(helper.content_tag(:p, "Help!")).to eq "

Help!

" end diff --git a/spec/dummy/spec/decorators/post_decorator_spec.rb b/spec/dummy/spec/decorators/post_decorator_spec.rb index 75aaf41d..a7635990 100755 --- a/spec/dummy/spec/decorators/post_decorator_spec.rb +++ b/spec/dummy/spec/decorators/post_decorator_spec.rb @@ -1,6 +1,6 @@ -require 'spec_helper' +require_relative '../rails_helper' -describe PostDecorator do +RSpec.describe PostDecorator do let(:decorator) { PostDecorator.new(object) } let(:object) { Post.create } diff --git a/spec/dummy/spec/decorators/spec_type_spec.rb b/spec/dummy/spec/decorators/spec_type_spec.rb index 2c52c03a..d0ac746f 100644 --- a/spec/dummy/spec/decorators/spec_type_spec.rb +++ b/spec/dummy/spec/decorators/spec_type_spec.rb @@ -1,6 +1,6 @@ -require 'spec_helper' +require_relative '../rails_helper' -describe "A spec in this folder" do +RSpec.describe "A spec in this folder" do it "is a decorator spec" do expect(RSpec.current_example.metadata[:type]).to be :decorator end diff --git a/spec/dummy/spec/decorators/view_context_spec.rb b/spec/dummy/spec/decorators/view_context_spec.rb index 2ba93a95..13498cf6 100644 --- a/spec/dummy/spec/decorators/view_context_spec.rb +++ b/spec/dummy/spec/decorators/view_context_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require_relative '../rails_helper' def it_does_not_leak_view_context 2.times do @@ -9,14 +9,14 @@ def it_does_not_leak_view_context end end -describe "A decorator spec", type: :decorator do +RSpec.describe "A decorator spec", type: :decorator do it_does_not_leak_view_context end -describe "A controller spec", type: :controller do +RSpec.describe "A controller spec", type: :controller do it_does_not_leak_view_context end -describe "A mailer spec", type: :mailer do +RSpec.describe "A mailer spec", type: :mailer do it_does_not_leak_view_context end diff --git a/spec/dummy/spec/mailers/post_mailer_spec.rb b/spec/dummy/spec/mailers/post_mailer_spec.rb index 3d6699ad..1838fef3 100644 --- a/spec/dummy/spec/mailers/post_mailer_spec.rb +++ b/spec/dummy/spec/mailers/post_mailer_spec.rb @@ -1,6 +1,6 @@ -require 'spec_helper' +require_relative '../rails_helper' -describe PostMailer do +RSpec.describe PostMailer do describe "#decorated_email" do let(:email_body) { Capybara.string(email.body.to_s) } let(:email) { PostMailer.decorated_email(post).deliver } diff --git a/spec/dummy/spec/models/mongoid_post_spec.rb b/spec/dummy/spec/models/mongoid_post_spec.rb index 1707bd1a..ea139585 100644 --- a/spec/dummy/spec/models/mongoid_post_spec.rb +++ b/spec/dummy/spec/models/mongoid_post_spec.rb @@ -1,8 +1,8 @@ -require 'spec_helper' -require 'shared_examples/decoratable' +require_relative '../spec_helper' +require_relative '../shared_examples/decoratable' if defined?(Mongoid) - describe MongoidPost do + RSpec.describe MongoidPost do it_behaves_like "a decoratable model" end end diff --git a/spec/dummy/spec/models/post_spec.rb b/spec/dummy/spec/models/post_spec.rb index 8bdd1926..4c453fed 100644 --- a/spec/dummy/spec/models/post_spec.rb +++ b/spec/dummy/spec/models/post_spec.rb @@ -1,6 +1,6 @@ -require 'spec_helper' -require 'shared_examples/decoratable' +require_relative '../spec_helper' +require_relative '../shared_examples/decoratable' -describe Post do +RSpec.describe Post do it_behaves_like "a decoratable model" end diff --git a/spec/dummy/spec/shared_examples/decoratable.rb b/spec/dummy/spec/shared_examples/decoratable.rb index 90863bcb..331b4ad9 100644 --- a/spec/dummy/spec/shared_examples/decoratable.rb +++ b/spec/dummy/spec/shared_examples/decoratable.rb @@ -1,4 +1,4 @@ -shared_examples_for "a decoratable model" do +RSpec.shared_examples_for "a decoratable model" do describe ".decorate" do it "applies a collection decorator to a scope" do described_class.create diff --git a/spec/generators/controller/controller_generator_spec.rb b/spec/generators/controller/controller_generator_spec.rb index af77c1fb..b6d5d802 100644 --- a/spec/generators/controller/controller_generator_spec.rb +++ b/spec/generators/controller/controller_generator_spec.rb @@ -1,10 +1,11 @@ require 'spec_helper' +require_relative '../../dummy/spec/rails_helper' require 'rails' require 'ammeter/init' require 'generators/controller_override' require 'generators/rails/decorator_generator' -describe Rails::Generators::ControllerGenerator do +RSpec.describe Rails::Generators::ControllerGenerator do destination File.expand_path("../tmp", __FILE__) before { prepare_destination } @@ -19,4 +20,4 @@ it { should contain "class YourModelDecorator" } end end -end \ No newline at end of file +end diff --git a/spec/generators/decorator/decorator_generator_spec.rb b/spec/generators/decorator/decorator_generator_spec.rb index 08a9601f..189f98c3 100644 --- a/spec/generators/decorator/decorator_generator_spec.rb +++ b/spec/generators/decorator/decorator_generator_spec.rb @@ -1,9 +1,9 @@ require 'spec_helper' -require 'rails' +require_relative '../../dummy/spec/rails_helper' require 'ammeter/init' require 'generators/rails/decorator_generator' -describe Rails::Generators::DecoratorGenerator do +RSpec.describe Rails::Generators::DecoratorGenerator do destination File.expand_path("../tmp", __FILE__) before { prepare_destination } @@ -15,27 +15,27 @@ describe "naming" do before { run_generator %w(YourModel) } - it { should contain "class YourModelDecorator" } + it { is_expected.to contain "class YourModelDecorator" } end describe "namespacing" do subject { file("app/decorators/namespace/your_model_decorator.rb") } before { run_generator %w(Namespace::YourModel) } - it { should contain "class Namespace::YourModelDecorator" } + it { is_expected.to contain "class Namespace::YourModelDecorator" } end describe "inheritance" do context "by default" do before { run_generator %w(YourModel) } - it { should contain "class YourModelDecorator < Draper::Decorator" } + it { is_expected.to contain "class YourModelDecorator < Draper::Decorator" } end context "with the --parent option" do before { run_generator %w(YourModel --parent=FooDecorator) } - it { should contain "class YourModelDecorator < FooDecorator" } + it { is_expected.to contain "class YourModelDecorator < FooDecorator" } end context "with an ApplicationDecorator" do @@ -47,7 +47,7 @@ before { run_generator %w(YourModel) } - it { should contain "class YourModelDecorator < ApplicationDecorator" } + it { is_expected.to contain "class YourModelDecorator < ApplicationDecorator" } end end end @@ -59,14 +59,14 @@ describe "naming" do before { run_generator %w(YourModel -t=rspec) } - it { should contain "describe YourModelDecorator" } + it { is_expected.to contain "describe YourModelDecorator" } end describe "namespacing" do subject { file("spec/decorators/namespace/your_model_decorator_spec.rb") } before { run_generator %w(Namespace::YourModel -t=rspec) } - it { should contain "describe Namespace::YourModelDecorator" } + it { is_expected.to contain "describe Namespace::YourModelDecorator" } end end end @@ -78,14 +78,14 @@ describe "naming" do before { run_generator %w(YourModel -t=test_unit) } - it { should contain "class YourModelDecoratorTest < Draper::TestCase" } + it { is_expected.to contain "class YourModelDecoratorTest < Draper::TestCase" } end describe "namespacing" do subject { file("test/decorators/namespace/your_model_decorator_test.rb") } before { run_generator %w(Namespace::YourModel -t=test_unit) } - it { should contain "class Namespace::YourModelDecoratorTest < Draper::TestCase" } + it { is_expected.to contain "class Namespace::YourModelDecoratorTest < Draper::TestCase" } end end end diff --git a/spec/integration/integration_spec.rb b/spec/integration/integration_spec.rb index 3175b3c4..24dc3a35 100644 --- a/spec/integration/integration_spec.rb +++ b/spec/integration/integration_spec.rb @@ -12,7 +12,7 @@ spec_types.each do |type, (path, controller)| page = app.get(path) - describe "in a #{type}" do + RSpec.describe "in a #{type}" do it "runs in the correct environment" do expect(page).to have_text(app.environment).in("#environment") end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d30942e4..cccdb8ac 100755 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,7 +10,7 @@ end config.mock_with :rspec do |mocks| - # mocks.verify_partial_doubles = true + mocks.verify_partial_doubles = true mocks.yield_receiver_to_any_instance_implementation_blocks = true end diff --git a/spec/support/dummy_app.rb b/spec/support/dummy_app.rb index c205f86f..5ab6a90a 100644 --- a/spec/support/dummy_app.rb +++ b/spec/support/dummy_app.rb @@ -7,7 +7,7 @@ class DummyApp def initialize(environment) - raise ArgumentError, "Environment must be development or production" unless ["development", "production"].include?(environment.to_s) + raise ArgumentError, 'Environment must be development or production' unless ['development', 'production', 'test'].include?(environment.to_s) @environment = environment end diff --git a/spec/support/matchers/have_text.rb b/spec/support/matchers/have_text.rb index b5010af7..fdc98e6b 100644 --- a/spec/support/matchers/have_text.rb +++ b/spec/support/matchers/have_text.rb @@ -1,3 +1,4 @@ +require 'spec_helper' require 'capybara' module HaveTextMatcher diff --git a/spec/support/shared_examples/decoratable_equality.rb b/spec/support/shared_examples/decoratable_equality.rb index 6a7f3adf..d97f7c3c 100644 --- a/spec/support/shared_examples/decoratable_equality.rb +++ b/spec/support/shared_examples/decoratable_equality.rb @@ -1,4 +1,6 @@ -shared_examples_for "decoration-aware #==" do |subject| +require 'spec_helper' + +RSpec.shared_examples_for "decoration-aware #==" do |subject| it "is true for itself" do expect(subject == subject).to be_truthy end diff --git a/spec/support/shared_examples/view_helpers.rb b/spec/support/shared_examples/view_helpers.rb index f7d06213..6675cb2e 100644 --- a/spec/support/shared_examples/view_helpers.rb +++ b/spec/support/shared_examples/view_helpers.rb @@ -1,38 +1,49 @@ -shared_examples_for "view helpers" do |subject| +require 'spec_helper' + +RSpec.shared_examples_for "view helpers" do |subject| describe "#helpers" do it "returns the current view context" do - Draper::ViewContext.stub current: :current_view_context + allow(Draper::ViewContext).to receive(:current) { :current_view_context } + # Draper::ViewContext.stub current: :current_view_context expect(subject.helpers).to be :current_view_context end it "is aliased to #h" do - Draper::ViewContext.stub current: :current_view_context + # Draper::ViewContext.stub current: :current_view_context + allow(Draper::ViewContext).to receive(:current) { :current_view_context } expect(subject.h).to be :current_view_context end end describe "#localize" do + let(:helpers) { double } + + before :each do + allow(subject).to receive(:helpers) { helpers } + allow(helpers).to receive(:localize) + end + it "delegates to #helpers" do - subject.stub helpers: double - subject.helpers.should_receive(:localize).with(:an_object, some: "parameter") + expect(helpers).to receive(:localize).with(:an_object, some: "parameter") subject.localize(:an_object, some: "parameter") end it "is aliased to #l" do - subject.stub helpers: double - subject.helpers.should_receive(:localize).with(:an_object, some: "parameter") + expect(helpers).to receive(:localize).with(:an_object, some: 'parameter') subject.l(:an_object, some: "parameter") end end describe ".helpers" do it "returns the current view context" do - Draper::ViewContext.stub current: :current_view_context + allow(Draper::ViewContext).to receive(:current) { :current_view_context } + # Draper::ViewContext.stub current: :current_view_context expect(subject.class.helpers).to be :current_view_context end it "is aliased to .h" do - Draper::ViewContext.stub current: :current_view_context + allow(Draper::ViewContext).to receive(:current) { :current_view_context } + # Draper::ViewContext.stub current: :current_view_context expect(subject.class.h).to be :current_view_context end end From 406f96204bcc6907007444079ff7da397c6df0e5 Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 20:41:06 -0600 Subject: [PATCH 26/31] Gemfile and Guardfile Edits Add pry, guard to development group, update Guardfile for new guard-rspec syntax. --- Gemfile | 6 ++++++ Guardfile | 15 +++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Gemfile b/Gemfile index b3e3fc49..d1190ad2 100644 --- a/Gemfile +++ b/Gemfile @@ -11,6 +11,12 @@ platforms :jruby do gem "activerecord-jdbcsqlite3-adapter", ">= 1.3.0.beta2" end +group :development, :test do + gem 'guard-rspec', require: false + gem 'ruby_gntp' + gem 'colorize' +end + version = ENV["RAILS_VERSION"] || "4.1" eval_gemfile File.expand_path("../gemfiles/#{version}.gemfile", __FILE__) diff --git a/Guardfile b/Guardfile index 2e6c3d74..ac973cd4 100644 --- a/Guardfile +++ b/Guardfile @@ -1,10 +1,11 @@ +notification :gntp, host: '127.0.0.1' + def rspec_guard(options = {}, &block) - options = { - :version => 2, - :notification => false + opts = { + :cmd => 'rspec' }.merge(options) - guard 'rspec', options, &block + guard 'rspec', opts, &block end rspec_guard :spec_paths => %w{spec/draper spec/generators} do @@ -13,14 +14,16 @@ rspec_guard :spec_paths => %w{spec/draper spec/generators} do watch('spec/spec_helper.rb') { "spec" } end -rspec_guard :spec_paths => 'spec/integration', :env => {'RAILS_ENV' => 'development'} do +rspec_guard :spec_paths => ['spec/integration'], cmd: 'RAILS_ENV=development rspec' do watch(%r{^spec/.+_spec\.rb$}) watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } watch('spec/spec_helper.rb') { "spec" } end -rspec_guard :spec_paths => 'spec/integration', :env => {'RAILS_ENV' => 'production'} do +rspec_guard :spec_paths => ['spec/integration'], cmd: 'RAILS_ENV=production rspec' do watch(%r{^spec/.+_spec\.rb$}) watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } watch('spec/spec_helper.rb') { "spec" } end + +# vim: set ts=8 sw=2 tw=0 ft=ruby et : From 7063cd6385e925f60f610c06b9e276ab0b750412 Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Wed, 26 Aug 2015 20:49:32 -0600 Subject: [PATCH 27/31] Travis.yml Update .travis.yml for more ruby versions --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 0591b046..ce213261 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,8 @@ services: rvm: - 2.1.5 - 2.2.1 + - 2.2.2 + - 2.2.3 env: - "RAILS_VERSION=4.0" From 4109534c121c8abe19c88404927b0945b5dc44eb Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Thu, 27 Aug 2015 01:43:17 -0600 Subject: [PATCH 28/31] Don't verify partial doubles because it breaks some specs --- spec/spec_helper.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index cccdb8ac..a9c2b353 100755 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,7 +10,6 @@ end config.mock_with :rspec do |mocks| - mocks.verify_partial_doubles = true mocks.yield_receiver_to_any_instance_implementation_blocks = true end From d358537d12396d58c4fa6644ad2ce593a77a253f Mon Sep 17 00:00:00 2001 From: Kurtis Rainbolt-Greene Date: Sat, 23 May 2015 20:24:23 -0500 Subject: [PATCH 29/31] rspec-rails ~> 3.3. Fixes #668. Update Spec Synax As of rspec-rails 3.3 they generate two files: - spec/spec_helper.rb - spec/rails_helper.rb This allows the user to either do pure ruby unit tests or include the entire rails environment. Draper really only works in a rails application if it has the rails environment. This matches ~> 3.2 and uses the rails_helper instead of spec_helper for the dummy app specs, but the standard spec_helper for the draper specs. Fix Spec spec generator because RSpec no longer prefers monkey-patching. Refactor PR #675 to be more concise re RSpec Version check, and to use RSpec::Core::Version rather than RSpec::Rails::Version, in case the latter does not exist. Updated all specs to RSpec 3.0 syntax ===================================== Change specs to use synax `expect(OBJECT).to EXPECTATION` rather that `OBJECT.should`. Changed all `OBJECT.stub(METHOD)` to `allow(OBJECT).to receive(:method)` Change one-liners to use syntax: `it { is_expected.to XXX }` rather than `it { should }`. Merge PR #674 from @mcasper and fix all trivial conflicts (i.e `allow(OBJ).to receive(:message)` vs `allow(OBJ).to receive_messages`) Gitignore ========= Ignore .ruby_version and .ruby_gemset Spec Helper =========== ActiveRecord::Migration.maintain_test_schema! is undefined in Rails 4.0, so I made that method conditional upon ENV['RAILS_VERSION'] --- .../rspec/templates/decorator_spec.rb | 1 - .../active_model_serializers_spec.rb | 16 +++++ spec/decorators/devise_spec.rb | 64 ++++++++++++++++++ spec/decorators/helpers_spec.rb | 21 ++++++ spec/decorators/post_decorator_spec.rb | 66 +++++++++++++++++++ spec/decorators/spec_type_spec.rb | 7 ++ spec/decorators/view_context_spec.rb | 22 +++++++ spec/draper/decoratable_spec.rb | 1 - spec/draper/decorator_spec.rb | 3 - spec/draper/factory_spec.rb | 1 - ...5c3c6... Refactored spec setup and helpers | 1 + ...3c6... Refactored spec setup and helpers_0 | 1 + ...3c6... Refactored spec setup and helpers_1 | 1 + spec/mailers/post_mailer_spec.rb | 33 ++++++++++ spec/models/mongoid_post_spec.rb | 8 +++ spec/models/post_spec.rb | 6 ++ spec/rails_helper.rb | 32 +++++++++ spec/shared_examples/decoratable.rb | 24 +++++++ 18 files changed, 302 insertions(+), 6 deletions(-) create mode 100644 spec/decorators/active_model_serializers_spec.rb create mode 100644 spec/decorators/devise_spec.rb create mode 100644 spec/decorators/helpers_spec.rb create mode 100755 spec/decorators/post_decorator_spec.rb create mode 100644 spec/decorators/spec_type_spec.rb create mode 100644 spec/decorators/view_context_spec.rb create mode 120000 spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers create mode 120000 spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers_0 create mode 120000 spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers_1 create mode 100644 spec/mailers/post_mailer_spec.rb create mode 100644 spec/models/mongoid_post_spec.rb create mode 100644 spec/models/post_spec.rb create mode 100644 spec/rails_helper.rb create mode 100644 spec/shared_examples/decoratable.rb diff --git a/lib/generators/rspec/templates/decorator_spec.rb b/lib/generators/rspec/templates/decorator_spec.rb index 0e0a4eee..98b907e8 100644 --- a/lib/generators/rspec/templates/decorator_spec.rb +++ b/lib/generators/rspec/templates/decorator_spec.rb @@ -5,5 +5,4 @@ <% end %> RSpec.describe <%= class_name %>Decorator, type: :decorator do - end diff --git a/spec/decorators/active_model_serializers_spec.rb b/spec/decorators/active_model_serializers_spec.rb new file mode 100644 index 00000000..e4d00a8f --- /dev/null +++ b/spec/decorators/active_model_serializers_spec.rb @@ -0,0 +1,16 @@ +require_relative '../rails_helper' + +RSpec.describe Draper::CollectionDecorator do + describe "#active_model_serializer" do + it "returns ActiveModel::ArraySerializer" do + collection_decorator = Draper::CollectionDecorator.new([]) + if defined?(ActiveModel::ArraySerializerSupport) + collection_serializer = collection_decorator.active_model_serializer + else + collection_serializer = ActiveModel::Serializer.serializer_for(collection_decorator) + end + + expect(collection_serializer).to be ActiveModel::ArraySerializer + end + end +end diff --git a/spec/decorators/devise_spec.rb b/spec/decorators/devise_spec.rb new file mode 100644 index 00000000..540a04d9 --- /dev/null +++ b/spec/decorators/devise_spec.rb @@ -0,0 +1,64 @@ +require_relative '../rails_helper' + +if defined?(Devise) + RSpec.describe "A decorator spec" do + it "can sign in a real user" do + user = User.new + sign_in user + + expect(helper.current_user).to be user + end + + it "can sign in a mock user" do + user = double("User") + sign_in :user, user + + expect(helper.current_user).to be user + end + + it "can sign in a real admin" do + admin = Admin.new + sign_in admin + + expect(helper.current_admin).to be admin + end + + it "can sign in a mock admin" do + admin = double("Admin") + sign_in :admin, admin + + expect(helper.current_admin).to be admin + end + + it "can sign out a real user" do + user = User.new + sign_in user + sign_out user + + expect(helper.current_user).to be_nil + end + + it "can sign out a mock user" do + user = double("User") + sign_in :user, user + sign_out :user + + expect(helper.current_user).to be_nil + end + + it "can sign out without a user" do + sign_out :user + + expect(helper.current_user).to be_nil + end + + it "is backwards-compatible" do + user = double("User") + ActiveSupport::Deprecation.silence do + sign_in user + end + + expect(helper.current_user).to be user + end + end +end diff --git a/spec/decorators/helpers_spec.rb b/spec/decorators/helpers_spec.rb new file mode 100644 index 00000000..8d32a545 --- /dev/null +++ b/spec/decorators/helpers_spec.rb @@ -0,0 +1,21 @@ +require_relative '../rails_helper' + +RSpec.describe "A decorator spec" do + it "can access helpers through `helper`" do + expect(helper.content_tag(:p, "Help!")).to eq "

Help!

" + end + + it "can access helpers through `helpers`" do + expect(helpers.content_tag(:p, "Help!")).to eq "

Help!

" + end + + it "can access helpers through `h`" do + expect(h.content_tag(:p, "Help!")).to eq "

Help!

" + end + + it "gets the same helper object as a decorator" do + decorator = Draper::Decorator.new(Object.new) + + expect(helpers).to be decorator.helpers + end +end diff --git a/spec/decorators/post_decorator_spec.rb b/spec/decorators/post_decorator_spec.rb new file mode 100755 index 00000000..a7635990 --- /dev/null +++ b/spec/decorators/post_decorator_spec.rb @@ -0,0 +1,66 @@ +require_relative '../rails_helper' + +RSpec.describe PostDecorator do + let(:decorator) { PostDecorator.new(object) } + let(:object) { Post.create } + + it "can use built-in helpers" do + expect(decorator.truncated).to eq "Once upon a..." + end + + it "can use built-in private helpers" do + expect(decorator.html_escaped).to eq "<script>danger</script>" + end + + it "can use user-defined helpers from app/helpers" do + expect(decorator.hello_world).to eq "Hello, world!" + end + + it "can be passed to path helpers" do + expect(helpers.post_path(decorator)).to eq "/en/posts/#{object.id}" + end + + it "can use path helpers with its model" do + expect(decorator.path_with_model).to eq "/en/posts/#{object.id}" + end + + it "can use path helpers with its id" do + expect(decorator.path_with_id).to eq "/en/posts/#{object.id}" + end + + it "can be passed to url helpers" do + expect(helpers.post_url(decorator)).to eq "http://www.example.com:12345/en/posts/#{object.id}" + end + + it "can use url helpers with its model" do + expect(decorator.url_with_model).to eq "http://www.example.com:12345/en/posts/#{object.id}" + end + + it "can use url helpers with its id" do + expect(decorator.url_with_id).to eq "http://www.example.com:12345/en/posts/#{object.id}" + end + + it "can be passed implicitly to url_for" do + expect(decorator.link).to eq "#{object.id}" + end + + it "serializes overriden attributes" do + expect(decorator.serializable_hash["updated_at"]).to be :overridden + end + + it "serializes to JSON" do + json = decorator.to_json + expect(json).to match /"updated_at":"overridden"/ + end + + it "serializes to XML" do + pending("Rails < 3.2 does not use `serializable_hash` in `to_xml`") if Rails.version.to_f < 3.2 + + xml = Capybara.string(decorator.to_xml) + expect(xml).to have_css "post > updated-at", text: "overridden" + end + + it "uses a test view context from ApplicationController" do + expect(Draper::ViewContext.current.controller).to be_an ApplicationController + end +end diff --git a/spec/decorators/spec_type_spec.rb b/spec/decorators/spec_type_spec.rb new file mode 100644 index 00000000..d0ac746f --- /dev/null +++ b/spec/decorators/spec_type_spec.rb @@ -0,0 +1,7 @@ +require_relative '../rails_helper' + +RSpec.describe "A spec in this folder" do + it "is a decorator spec" do + expect(RSpec.current_example.metadata[:type]).to be :decorator + end +end diff --git a/spec/decorators/view_context_spec.rb b/spec/decorators/view_context_spec.rb new file mode 100644 index 00000000..13498cf6 --- /dev/null +++ b/spec/decorators/view_context_spec.rb @@ -0,0 +1,22 @@ +require_relative '../rails_helper' + +def it_does_not_leak_view_context + 2.times do + it "has an independent view context" do + expect(Draper::ViewContext.current).not_to be :leaked + Draper::ViewContext.current = :leaked + end + end +end + +RSpec.describe "A decorator spec", type: :decorator do + it_does_not_leak_view_context +end + +RSpec.describe "A controller spec", type: :controller do + it_does_not_leak_view_context +end + +RSpec.describe "A mailer spec", type: :mailer do + it_does_not_leak_view_context +end diff --git a/spec/draper/decoratable_spec.rb b/spec/draper/decoratable_spec.rb index 899e6b88..63f99927 100644 --- a/spec/draper/decoratable_spec.rb +++ b/spec/draper/decoratable_spec.rb @@ -132,7 +132,6 @@ module Draper it "calls #decorate_collection on .decorator_class" do scoped = [Product.new] allow(Product).to receive(scoping_method).and_return(scoped) - expect(Product.decorator_class).to receive(:decorate_collection).with(scoped, with: nil).and_return(:decorated_collection) expect(Product.decorate).to be :decorated_collection end diff --git a/spec/draper/decorator_spec.rb b/spec/draper/decorator_spec.rb index da4a5828..42cc5a99 100755 --- a/spec/draper/decorator_spec.rb +++ b/spec/draper/decorator_spec.rb @@ -228,13 +228,11 @@ module Draper it "returns false when .object_class is not inferrable" do allow(Decorator).to receive(:object_class).and_raise(UninferrableSourceError.new(Decorator)) - expect(Decorator.object_class?).to be_falsey end it "is aliased to .source_class?" do allow(Decorator).to receive(:object_class).and_return(Model) - expect(Decorator.source_class?).to be_truthy end end @@ -488,7 +486,6 @@ module Draper describe ".model_name" do it "delegates to the source class" do allow(Decorator).to receive(:object_class) { double(model_name: :delegated) } - expect(Decorator.model_name).to be :delegated end end diff --git a/spec/draper/factory_spec.rb b/spec/draper/factory_spec.rb index a2495685..6831a544 100644 --- a/spec/draper/factory_spec.rb +++ b/spec/draper/factory_spec.rb @@ -246,6 +246,5 @@ module Draper end end end - end end diff --git a/spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers b/spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers new file mode 120000 index 00000000..b870225a --- /dev/null +++ b/spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers @@ -0,0 +1 @@ +../ \ No newline at end of file diff --git a/spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers_0 b/spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers_0 new file mode 120000 index 00000000..b870225a --- /dev/null +++ b/spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers_0 @@ -0,0 +1 @@ +../ \ No newline at end of file diff --git a/spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers_1 b/spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers_1 new file mode 120000 index 00000000..b870225a --- /dev/null +++ b/spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers_1 @@ -0,0 +1 @@ +../ \ No newline at end of file diff --git a/spec/mailers/post_mailer_spec.rb b/spec/mailers/post_mailer_spec.rb new file mode 100644 index 00000000..1838fef3 --- /dev/null +++ b/spec/mailers/post_mailer_spec.rb @@ -0,0 +1,33 @@ +require_relative '../rails_helper' + +RSpec.describe PostMailer do + describe "#decorated_email" do + let(:email_body) { Capybara.string(email.body.to_s) } + let(:email) { PostMailer.decorated_email(post).deliver } + let(:post) { Post.create } + + it "decorates" do + expect(email_body).to have_content "Today" + end + + it "can use path helpers with a model" do + expect(email_body).to have_css "#path_with_model", text: "/en/posts/#{post.id}" + end + + it "can use path helpers with an id" do + expect(email_body).to have_css "#path_with_id", text: "/en/posts/#{post.id}" + end + + it "can use url helpers with a model" do + expect(email_body).to have_css "#url_with_model", text: "http://www.example.com:12345/en/posts/#{post.id}" + end + + it "can use url helpers with an id" do + expect(email_body).to have_css "#url_with_id", text: "http://www.example.com:12345/en/posts/#{post.id}" + end + + it "uses the correct view context controller" do + expect(email_body).to have_css "#controller", text: "PostMailer" + end + end +end diff --git a/spec/models/mongoid_post_spec.rb b/spec/models/mongoid_post_spec.rb new file mode 100644 index 00000000..ea139585 --- /dev/null +++ b/spec/models/mongoid_post_spec.rb @@ -0,0 +1,8 @@ +require_relative '../spec_helper' +require_relative '../shared_examples/decoratable' + +if defined?(Mongoid) + RSpec.describe MongoidPost do + it_behaves_like "a decoratable model" + end +end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb new file mode 100644 index 00000000..4c453fed --- /dev/null +++ b/spec/models/post_spec.rb @@ -0,0 +1,6 @@ +require_relative '../spec_helper' +require_relative '../shared_examples/decoratable' + +RSpec.describe Post do + it_behaves_like "a decoratable model" +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 00000000..34e083f5 --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,32 @@ +ENV['RAILS_ENV'] ||= 'test' +require File.expand_path('../../config/environment', __FILE__) +# Prevent database truncation if the environment is production +abort('Rails is running in production mode!') if Rails.env.production? +require 'spec_helper' +require 'rspec/rails' +# Add additional requires below this line. Rails is not loaded until this point! + +# Requires supporting ruby files with custom matchers and macros, etc, in +# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are +# run as spec files by default. This means that files in spec/support that end +# in _spec.rb will both be required and run as specs, causing the specs to be +# run twice. It is recommended that you do not name files matching this glob to +# end with _spec.rb. You can configure this pattern with the --pattern +# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. +# +# The following line is provided for convenience purposes. It has the downside +# of increasing the boot-up time by auto-requiring all files in the support +# directory. Alternatively, in the individual `*_spec.rb` files, manually +# require only the support files necessary. +# +# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } + +# Checks for pending migrations before tests are run. +# If you are not using ActiveRecord, you can remove this line. +ActiveRecord::Migration.maintain_test_schema! + +RSpec.configure do |config| + config.fixture_path = "#{::Rails.root}/spec/fixtures" + config.use_transactional_fixtures = true + config.infer_spec_type_from_file_location! +end diff --git a/spec/shared_examples/decoratable.rb b/spec/shared_examples/decoratable.rb new file mode 100644 index 00000000..331b4ad9 --- /dev/null +++ b/spec/shared_examples/decoratable.rb @@ -0,0 +1,24 @@ +RSpec.shared_examples_for "a decoratable model" do + describe ".decorate" do + it "applies a collection decorator to a scope" do + described_class.create + decorated = described_class.limit(1).decorate + + expect(decorated.size).to eq(1) + expect(decorated).to be_decorated + end + end + + describe "#==" do + it "is true for other instances' decorators" do + pending "Mongoid < 3.1 overrides `#==`" if defined?(Mongoid) && Mongoid::VERSION.to_f < 3.1 && described_class < Mongoid::Document + + described_class.create + one = described_class.first + other = described_class.first + + expect(one).not_to be other + expect(one == other.decorate).to be_truthy + end + end +end From 978fad058ce0b01aba6ccf6998533fd79b021f9e Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Thu, 27 Aug 2015 14:48:25 -0600 Subject: [PATCH 30/31] moved all the specs to the right place --- .../active_model_serializers_spec.rb | 16 ----- spec/decorators/devise_spec.rb | 64 ------------------ spec/decorators/helpers_spec.rb | 21 ------ spec/decorators/post_decorator_spec.rb | 66 ------------------- spec/decorators/spec_type_spec.rb | 7 -- spec/decorators/view_context_spec.rb | 22 ------- ...5c3c6... Refactored spec setup and helpers | 1 - ...3c6... Refactored spec setup and helpers_0 | 1 - ...3c6... Refactored spec setup and helpers_1 | 1 - .../decorator/decorator_generator_spec.rb | 3 +- spec/mailers/post_mailer_spec.rb | 33 ---------- spec/models/mongoid_post_spec.rb | 8 --- spec/models/post_spec.rb | 6 -- spec/rails_helper.rb | 32 --------- spec/shared_examples/decoratable.rb | 24 ------- 15 files changed, 2 insertions(+), 303 deletions(-) delete mode 100644 spec/decorators/active_model_serializers_spec.rb delete mode 100644 spec/decorators/devise_spec.rb delete mode 100644 spec/decorators/helpers_spec.rb delete mode 100755 spec/decorators/post_decorator_spec.rb delete mode 100644 spec/decorators/spec_type_spec.rb delete mode 100644 spec/decorators/view_context_spec.rb delete mode 120000 spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers delete mode 120000 spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers_0 delete mode 120000 spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers_1 delete mode 100644 spec/mailers/post_mailer_spec.rb delete mode 100644 spec/models/mongoid_post_spec.rb delete mode 100644 spec/models/post_spec.rb delete mode 100644 spec/rails_helper.rb delete mode 100644 spec/shared_examples/decoratable.rb diff --git a/spec/decorators/active_model_serializers_spec.rb b/spec/decorators/active_model_serializers_spec.rb deleted file mode 100644 index e4d00a8f..00000000 --- a/spec/decorators/active_model_serializers_spec.rb +++ /dev/null @@ -1,16 +0,0 @@ -require_relative '../rails_helper' - -RSpec.describe Draper::CollectionDecorator do - describe "#active_model_serializer" do - it "returns ActiveModel::ArraySerializer" do - collection_decorator = Draper::CollectionDecorator.new([]) - if defined?(ActiveModel::ArraySerializerSupport) - collection_serializer = collection_decorator.active_model_serializer - else - collection_serializer = ActiveModel::Serializer.serializer_for(collection_decorator) - end - - expect(collection_serializer).to be ActiveModel::ArraySerializer - end - end -end diff --git a/spec/decorators/devise_spec.rb b/spec/decorators/devise_spec.rb deleted file mode 100644 index 540a04d9..00000000 --- a/spec/decorators/devise_spec.rb +++ /dev/null @@ -1,64 +0,0 @@ -require_relative '../rails_helper' - -if defined?(Devise) - RSpec.describe "A decorator spec" do - it "can sign in a real user" do - user = User.new - sign_in user - - expect(helper.current_user).to be user - end - - it "can sign in a mock user" do - user = double("User") - sign_in :user, user - - expect(helper.current_user).to be user - end - - it "can sign in a real admin" do - admin = Admin.new - sign_in admin - - expect(helper.current_admin).to be admin - end - - it "can sign in a mock admin" do - admin = double("Admin") - sign_in :admin, admin - - expect(helper.current_admin).to be admin - end - - it "can sign out a real user" do - user = User.new - sign_in user - sign_out user - - expect(helper.current_user).to be_nil - end - - it "can sign out a mock user" do - user = double("User") - sign_in :user, user - sign_out :user - - expect(helper.current_user).to be_nil - end - - it "can sign out without a user" do - sign_out :user - - expect(helper.current_user).to be_nil - end - - it "is backwards-compatible" do - user = double("User") - ActiveSupport::Deprecation.silence do - sign_in user - end - - expect(helper.current_user).to be user - end - end -end diff --git a/spec/decorators/helpers_spec.rb b/spec/decorators/helpers_spec.rb deleted file mode 100644 index 8d32a545..00000000 --- a/spec/decorators/helpers_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -require_relative '../rails_helper' - -RSpec.describe "A decorator spec" do - it "can access helpers through `helper`" do - expect(helper.content_tag(:p, "Help!")).to eq "

Help!

" - end - - it "can access helpers through `helpers`" do - expect(helpers.content_tag(:p, "Help!")).to eq "

Help!

" - end - - it "can access helpers through `h`" do - expect(h.content_tag(:p, "Help!")).to eq "

Help!

" - end - - it "gets the same helper object as a decorator" do - decorator = Draper::Decorator.new(Object.new) - - expect(helpers).to be decorator.helpers - end -end diff --git a/spec/decorators/post_decorator_spec.rb b/spec/decorators/post_decorator_spec.rb deleted file mode 100755 index a7635990..00000000 --- a/spec/decorators/post_decorator_spec.rb +++ /dev/null @@ -1,66 +0,0 @@ -require_relative '../rails_helper' - -RSpec.describe PostDecorator do - let(:decorator) { PostDecorator.new(object) } - let(:object) { Post.create } - - it "can use built-in helpers" do - expect(decorator.truncated).to eq "Once upon a..." - end - - it "can use built-in private helpers" do - expect(decorator.html_escaped).to eq "<script>danger</script>" - end - - it "can use user-defined helpers from app/helpers" do - expect(decorator.hello_world).to eq "Hello, world!" - end - - it "can be passed to path helpers" do - expect(helpers.post_path(decorator)).to eq "/en/posts/#{object.id}" - end - - it "can use path helpers with its model" do - expect(decorator.path_with_model).to eq "/en/posts/#{object.id}" - end - - it "can use path helpers with its id" do - expect(decorator.path_with_id).to eq "/en/posts/#{object.id}" - end - - it "can be passed to url helpers" do - expect(helpers.post_url(decorator)).to eq "http://www.example.com:12345/en/posts/#{object.id}" - end - - it "can use url helpers with its model" do - expect(decorator.url_with_model).to eq "http://www.example.com:12345/en/posts/#{object.id}" - end - - it "can use url helpers with its id" do - expect(decorator.url_with_id).to eq "http://www.example.com:12345/en/posts/#{object.id}" - end - - it "can be passed implicitly to url_for" do - expect(decorator.link).to eq "#{object.id}" - end - - it "serializes overriden attributes" do - expect(decorator.serializable_hash["updated_at"]).to be :overridden - end - - it "serializes to JSON" do - json = decorator.to_json - expect(json).to match /"updated_at":"overridden"/ - end - - it "serializes to XML" do - pending("Rails < 3.2 does not use `serializable_hash` in `to_xml`") if Rails.version.to_f < 3.2 - - xml = Capybara.string(decorator.to_xml) - expect(xml).to have_css "post > updated-at", text: "overridden" - end - - it "uses a test view context from ApplicationController" do - expect(Draper::ViewContext.current.controller).to be_an ApplicationController - end -end diff --git a/spec/decorators/spec_type_spec.rb b/spec/decorators/spec_type_spec.rb deleted file mode 100644 index d0ac746f..00000000 --- a/spec/decorators/spec_type_spec.rb +++ /dev/null @@ -1,7 +0,0 @@ -require_relative '../rails_helper' - -RSpec.describe "A spec in this folder" do - it "is a decorator spec" do - expect(RSpec.current_example.metadata[:type]).to be :decorator - end -end diff --git a/spec/decorators/view_context_spec.rb b/spec/decorators/view_context_spec.rb deleted file mode 100644 index 13498cf6..00000000 --- a/spec/decorators/view_context_spec.rb +++ /dev/null @@ -1,22 +0,0 @@ -require_relative '../rails_helper' - -def it_does_not_leak_view_context - 2.times do - it "has an independent view context" do - expect(Draper::ViewContext.current).not_to be :leaked - Draper::ViewContext.current = :leaked - end - end -end - -RSpec.describe "A decorator spec", type: :decorator do - it_does_not_leak_view_context -end - -RSpec.describe "A controller spec", type: :controller do - it_does_not_leak_view_context -end - -RSpec.describe "A mailer spec", type: :mailer do - it_does_not_leak_view_context -end diff --git a/spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers b/spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers deleted file mode 120000 index b870225a..00000000 --- a/spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers +++ /dev/null @@ -1 +0,0 @@ -../ \ No newline at end of file diff --git a/spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers_0 b/spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers_0 deleted file mode 120000 index b870225a..00000000 --- a/spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers_0 +++ /dev/null @@ -1 +0,0 @@ -../ \ No newline at end of file diff --git a/spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers_1 b/spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers_1 deleted file mode 120000 index b870225a..00000000 --- a/spec/dummy/spec~ed5c3c6... Refactored spec setup and helpers_1 +++ /dev/null @@ -1 +0,0 @@ -../ \ No newline at end of file diff --git a/spec/generators/decorator/decorator_generator_spec.rb b/spec/generators/decorator/decorator_generator_spec.rb index 189f98c3..00af75df 100644 --- a/spec/generators/decorator/decorator_generator_spec.rb +++ b/spec/generators/decorator/decorator_generator_spec.rb @@ -1,5 +1,6 @@ require 'spec_helper' -require_relative '../../dummy/spec/rails_helper' +require 'rspec/rails' +# require_relative '../../dummy/spec/rails_helper' require 'ammeter/init' require 'generators/rails/decorator_generator' diff --git a/spec/mailers/post_mailer_spec.rb b/spec/mailers/post_mailer_spec.rb deleted file mode 100644 index 1838fef3..00000000 --- a/spec/mailers/post_mailer_spec.rb +++ /dev/null @@ -1,33 +0,0 @@ -require_relative '../rails_helper' - -RSpec.describe PostMailer do - describe "#decorated_email" do - let(:email_body) { Capybara.string(email.body.to_s) } - let(:email) { PostMailer.decorated_email(post).deliver } - let(:post) { Post.create } - - it "decorates" do - expect(email_body).to have_content "Today" - end - - it "can use path helpers with a model" do - expect(email_body).to have_css "#path_with_model", text: "/en/posts/#{post.id}" - end - - it "can use path helpers with an id" do - expect(email_body).to have_css "#path_with_id", text: "/en/posts/#{post.id}" - end - - it "can use url helpers with a model" do - expect(email_body).to have_css "#url_with_model", text: "http://www.example.com:12345/en/posts/#{post.id}" - end - - it "can use url helpers with an id" do - expect(email_body).to have_css "#url_with_id", text: "http://www.example.com:12345/en/posts/#{post.id}" - end - - it "uses the correct view context controller" do - expect(email_body).to have_css "#controller", text: "PostMailer" - end - end -end diff --git a/spec/models/mongoid_post_spec.rb b/spec/models/mongoid_post_spec.rb deleted file mode 100644 index ea139585..00000000 --- a/spec/models/mongoid_post_spec.rb +++ /dev/null @@ -1,8 +0,0 @@ -require_relative '../spec_helper' -require_relative '../shared_examples/decoratable' - -if defined?(Mongoid) - RSpec.describe MongoidPost do - it_behaves_like "a decoratable model" - end -end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb deleted file mode 100644 index 4c453fed..00000000 --- a/spec/models/post_spec.rb +++ /dev/null @@ -1,6 +0,0 @@ -require_relative '../spec_helper' -require_relative '../shared_examples/decoratable' - -RSpec.describe Post do - it_behaves_like "a decoratable model" -end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb deleted file mode 100644 index 34e083f5..00000000 --- a/spec/rails_helper.rb +++ /dev/null @@ -1,32 +0,0 @@ -ENV['RAILS_ENV'] ||= 'test' -require File.expand_path('../../config/environment', __FILE__) -# Prevent database truncation if the environment is production -abort('Rails is running in production mode!') if Rails.env.production? -require 'spec_helper' -require 'rspec/rails' -# Add additional requires below this line. Rails is not loaded until this point! - -# Requires supporting ruby files with custom matchers and macros, etc, in -# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are -# run as spec files by default. This means that files in spec/support that end -# in _spec.rb will both be required and run as specs, causing the specs to be -# run twice. It is recommended that you do not name files matching this glob to -# end with _spec.rb. You can configure this pattern with the --pattern -# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. -# -# The following line is provided for convenience purposes. It has the downside -# of increasing the boot-up time by auto-requiring all files in the support -# directory. Alternatively, in the individual `*_spec.rb` files, manually -# require only the support files necessary. -# -# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } - -# Checks for pending migrations before tests are run. -# If you are not using ActiveRecord, you can remove this line. -ActiveRecord::Migration.maintain_test_schema! - -RSpec.configure do |config| - config.fixture_path = "#{::Rails.root}/spec/fixtures" - config.use_transactional_fixtures = true - config.infer_spec_type_from_file_location! -end diff --git a/spec/shared_examples/decoratable.rb b/spec/shared_examples/decoratable.rb deleted file mode 100644 index 331b4ad9..00000000 --- a/spec/shared_examples/decoratable.rb +++ /dev/null @@ -1,24 +0,0 @@ -RSpec.shared_examples_for "a decoratable model" do - describe ".decorate" do - it "applies a collection decorator to a scope" do - described_class.create - decorated = described_class.limit(1).decorate - - expect(decorated.size).to eq(1) - expect(decorated).to be_decorated - end - end - - describe "#==" do - it "is true for other instances' decorators" do - pending "Mongoid < 3.1 overrides `#==`" if defined?(Mongoid) && Mongoid::VERSION.to_f < 3.1 && described_class < Mongoid::Document - - described_class.create - one = described_class.first - other = described_class.first - - expect(one).not_to be other - expect(one == other.decorate).to be_truthy - end - end -end From deaef7287f3dda40a3685deb84b58254311ded32 Mon Sep 17 00:00:00 2001 From: Morgan Lieberthal Date: Thu, 17 Sep 2015 12:07:04 -0600 Subject: [PATCH 31/31] Fixed Issue with RSpec Version Check While the original spec passed, the generator did not work in practice because RSpec is typically not required in full in the development environment. The generator now explicitly requires RSpec::Version to perform the check. --- lib/generators/rspec/templates/decorator_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/generators/rspec/templates/decorator_spec.rb b/lib/generators/rspec/templates/decorator_spec.rb index 98b907e8..6c6cf086 100644 --- a/lib/generators/rspec/templates/decorator_spec.rb +++ b/lib/generators/rspec/templates/decorator_spec.rb @@ -1,4 +1,5 @@ -<% if RSpec::Core::Version::STRING.match(/\A3\.[^10]/) %> +<% require 'rspec/version' %> +<% if RSpec::Version::STRING.match(/\A3\.[^10]/) %> require 'rails_helper' <% else %> require 'spec_helper'