diff --git a/.gitignore b/.gitignore index 5c99549c..010ce165 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,9 @@ vendor/bundle *.DS_Store spec/dummy/log/* spec/dummy/db/*.sqlite3 +.ruby-version* +.ruby-gemset* +.rake_tasks +rspec_errors.txt +rspec_results.html +bin/ 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" 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 : 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/lib/generators/rspec/templates/decorator_spec.rb b/lib/generators/rspec/templates/decorator_spec.rb index 6acbd52f..6c6cf086 100644 --- a/lib/generators/rspec/templates/decorator_spec.rb +++ b/lib/generators/rspec/templates/decorator_spec.rb @@ -1,4 +1,9 @@ -require 'spec_helper' +<% require 'rspec/version' %> +<% if RSpec::Version::STRING.match(/\A3\.[^10]/) %> + require 'rails_helper' +<% else %> + require 'spec_helper' +<% end %> -describe <%= class_name %>Decorator do +RSpec.describe <%= class_name %>Decorator, type: :decorator do end diff --git a/spec/draper/collection_decorator_spec.rb b/spec/draper/collection_decorator_spec.rb index d2e256ca..173ef26a 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).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/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..63f99927 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,8 +22,7 @@ module Draper it "uses the #decorator_class" do product = Product.new - product.stub decorator_class: OtherDecorator - + allow(product).to receive(:decorator_class) { OtherDecorator } expect(product.decorate).to be_an_instance_of OtherDecorator end end @@ -70,7 +69,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 +82,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 +131,16 @@ module Draper it "calls #decorate_collection on .decorator_class" do scoped = [Product.new] - Product.stub scoping_method => scoped - - Product.decorator_class.should_receive(:decorate_collection).with(scoped, with: nil).and_return(:decorated_collection) + 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 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 +175,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 +190,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..4deeb8a9 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,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, {}) @@ -40,7 +40,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 +48,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) + expect(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 +67,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..ff2f7c53 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,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 @@ -43,29 +43,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) + expect(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..42cc5a99 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,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,20 +221,18 @@ 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 end @@ -243,7 +241,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 +259,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 +267,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 +276,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 +288,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,16 +478,14 @@ 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 end 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) } expect(Decorator.model_name).to be :delegated end end @@ -514,7 +510,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 +519,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 +534,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 +570,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 +602,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 +616,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(:foo) { double(bar: :delegated) } decorator = Decorator.new(object) expect(decorator.bar).to be :delegated @@ -652,15 +648,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(:hello_world).and_return(: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 } expect{Decorator.hello_world}.to raise_error NoMethodError end end @@ -713,13 +708,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 +732,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(: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..6831a544 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) + 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(: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) + 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(: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) + 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 @@ -246,6 +246,5 @@ module Draper end end end - 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..3a9ded00 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,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..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..cc7520d7 100644 --- a/spec/draper/view_context/build_strategy_spec.rb +++ b/spec/draper/view_context/build_strategy_spec.rb @@ -9,12 +9,12 @@ 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) } 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(: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(: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(:controller) { fake_controller(view_context) } helpers = Module.new do def a_helper_method; end end @@ -70,7 +70,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/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/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/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/generators/controller/controller_generator_spec.rb b/spec/generators/controller/controller_generator_spec.rb index af77c1fb..a42f2706 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 } @@ -16,7 +17,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 +end diff --git a/spec/generators/decorator/decorator_generator_spec.rb b/spec/generators/decorator/decorator_generator_spec.rb index 08a9601f..00af75df 100644 --- a/spec/generators/decorator/decorator_generator_spec.rb +++ b/spec/generators/decorator/decorator_generator_spec.rb @@ -1,9 +1,10 @@ require 'spec_helper' -require 'rails' +require 'rspec/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 +16,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 +48,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 +60,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 +79,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 df3aad9e..a9c2b353 100755 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,11 +5,26 @@ 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.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 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..05cd6404 100644 --- a/spec/support/shared_examples/view_helpers.rb +++ b/spec/support/shared_examples/view_helpers.rb @@ -1,38 +1,45 @@ -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 } 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(: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 } 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 } expect(subject.class.h).to be :current_view_context end end