mirror of
https://github.com/drapergem/draper
synced 2023-03-27 23:21:17 -04:00
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 }`.
This commit is contained in:
parent
89ca9307ee
commit
f4bb26bd53
33 changed files with 262 additions and 213 deletions
|
@ -2,7 +2,7 @@ require 'spec_helper'
|
|||
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
|
||||
|
|
|
@ -2,7 +2,7 @@ require 'spec_helper'
|
|||
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
|
||||
|
|
|
@ -2,7 +2,7 @@ require 'spec_helper'
|
|||
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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 @@ module Draper
|
|||
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 @@ module Draper
|
|||
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
|
||||
|
|
|
@ -2,7 +2,7 @@ require 'spec_helper'
|
|||
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
|
||||
|
|
|
@ -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
|
||||
|
@ -101,7 +101,7 @@ module Draper
|
|||
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
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 @@ module Draper
|
|||
|
||||
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 @@ module Draper
|
|||
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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -2,7 +2,7 @@ require 'spec_helper'
|
|||
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
|
||||
|
|
|
@ -7,7 +7,7 @@ Draper::ViewContext.test_strategy :fast
|
|||
|
||||
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 @@ describe PostDecorator do
|
|||
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
|
||||
|
|
|
@ -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([])
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 "<p>Help!</p>"
|
||||
end
|
||||
|
|
|
@ -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 }
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 }
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 }
|
||||
|
|
|
@ -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 Rails::Generators::DecoratorGenerator do
|
|||
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 @@ describe Rails::Generators::DecoratorGenerator do
|
|||
|
||||
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 Rails::Generators::DecoratorGenerator do
|
|||
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 Rails::Generators::DecoratorGenerator do
|
|||
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
|
||||
|
|
|
@ -12,7 +12,7 @@ app.start_server do
|
|||
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
|
||||
|
|
|
@ -10,7 +10,7 @@ RSpec.configure do |config|
|
|||
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
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ require 'net/http'
|
|||
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
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
require 'spec_helper'
|
||||
require 'capybara'
|
||||
|
||||
module HaveTextMatcher
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue