Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rspec3 Update - Spec Syntax, Generators, and Monkey-Patching #692

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
036be08
Update the rest of the specs to rspec3 syntax
mcasper May 21, 2015
46f97aa
rspec-rails ~> 3.2 uses rails_helper fixes #668
May 24, 2015
59bdeb1
RSpec is now into the whole type based tagging
Jun 1, 2015
66c5b39
Merge PR #668 #675 from drapergem/master
baberthal Aug 25, 2015
2ae00ef
Update .gitignore.
baberthal Aug 26, 2015
ed5c3c6
Refactored spec setup and helpers
baberthal Aug 26, 2015
354b7e9
Moved back to separated spec & spec/dummy/spec
baberthal Aug 27, 2015
adfa1a4
Updated spec/draper to RSpec 3.0 syntax
baberthal Aug 27, 2015
eb1a9d1
update to RSpec 3.0 syntax
baberthal Aug 27, 2015
eebd252
Update to RSpec 3.0 syntax
baberthal Aug 27, 2015
bd806b5
Update spec_helper to disable monkey_patching, other RSpec 3.0 updates
baberthal Aug 27, 2015
1ce287d
Update RSpec decorator spec generator to use RSpec::Core::Version, be…
baberthal Aug 27, 2015
67e4ebb
update dummy fast_spec to RSpec 3.0 syntax
baberthal Aug 27, 2015
e90d553
ignore rspec_results files
baberthal Aug 27, 2015
aa5d239
Add pry to development group gemfile, update Guardfile fore new guard…
baberthal Aug 27, 2015
c4c7fdf
update integration spec for RSpec 3.0 syntax
baberthal Aug 27, 2015
85ff30e
Update .travis.yml for more ruby versions
baberthal Aug 27, 2015
a4b87c2
Ignore binstubs
baberthal Aug 27, 2015
4a6d3ec
update to RSpec 3.3
baberthal Aug 27, 2015
80db110
should -> is_expected.to
baberthal Aug 27, 2015
e9d44fd
Modify spec/rails_helper.
baberthal Aug 27, 2015
84a2365
RSpec decorator_spec generator. Fixes #668
baberthal Aug 25, 2015
7db14de
Update .gitignore.
baberthal Aug 26, 2015
89ca930
Update to RSpec 3.3
baberthal Aug 27, 2015
f4bb26b
Updated all specs to RSpec 3.0 syntax
baberthal Aug 27, 2015
406f962
Gemfile and Guardfile Edits
baberthal Aug 27, 2015
7063cd6
Travis.yml
baberthal Aug 27, 2015
b709770
Merge branch 'master' of https://github.com/baberthal/draper
baberthal Aug 27, 2015
4109534
Don't verify partial doubles because it breaks some specs
baberthal Aug 27, 2015
66bc7ad
Merge branch 'mcasper/update_to_rspec_3_syntax'
baberthal Aug 27, 2015
d358537
rspec-rails ~> 3.3. Fixes #668. Update Spec Synax
May 24, 2015
978fad0
moved all the specs to the right place
baberthal Aug 27, 2015
deaef72
Fixed Issue with RSpec Version Check
baberthal Sep 17, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ services:
rvm:
- 2.1.5
- 2.2.1
- 2.2.2
- 2.2.3

env:
- "RAILS_VERSION=4.0"
Expand Down
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
15 changes: 9 additions & 6 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 :
2 changes: 1 addition & 1 deletion draper.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
9 changes: 7 additions & 2 deletions lib/generators/rspec/templates/decorator_spec.rb
Original file line number Diff line number Diff line change
@@ -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
20 changes: 10 additions & 10 deletions spec/draper/collection_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -149,15 +149,15 @@ 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

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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/draper/decoratable/equality_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 11 additions & 14 deletions spec/draper/decoratable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
20 changes: 10 additions & 10 deletions spec/draper/decorated_association_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

module Draper
describe DecoratedAssociation do
RSpec.describe DecoratedAssociation do

describe "#initialize" do
it "accepts valid options" do
Expand All @@ -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, {})
Expand All @@ -40,41 +40,41 @@ 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)
owner = double(object: object, context: owner_context)
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

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
Expand Down
Loading