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 Updates: Spec Syntax, Generators, and Monkey-Patching (continued) #853

Draft
wants to merge 34 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 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
cb9af6c
Merge remote-tracking branch 'origin/master' into feature/rspec3-updates
codebycliff Mar 20, 2019
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 @@ -16,3 +16,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/
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 'rails_helper'
<% require 'rspec/version' %>
<% if RSpec::Version::STRING.match(/\A3\.[^10]/) %>
require 'rails_helper'
Copy link
Contributor

@jrochkind jrochkind May 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What rspec versions are you trying to test for here with 3 not followed by a 1 or a 0? Are you trying to do rspec version greater than 3.2? That should prob be doc'd in comment, but also, that regexp will fail if rspec gets to 3.10, which it's not far from.

You may be able to use this instead?

if Gem::Version.new(RSpec::Version::STRING) >= Gem::Version.new("3.2")

Which is self-evident enough not to require a comment doc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the code you have provided is a lot less brittle. This work was originally done by someone else, I just fixed the conflicts and re-opened the pull request. I can only assume they were trying to achieve what you stated above. I haven't had a lot of time lately to work on this. If you are interested in cleaning this up and getting it pushed through, you are more than welcome to take it over.

Copy link
Contributor

@jrochkind jrochkind May 31, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think the highest priority is actually improving the template for the decorator specs that Draper generates -- this file we're commenting on now.

The rest of the PR seems to be about changing Draper's own specs -- but if Draper's specs currently pass, that doesn't seem to be as much of a priority, as it doesn't really effect the developer users of Draper, like the Draper generator for specs in your own app does.

So if I can find the time, I might open another PR just focusing on that? It sounds like you are a Draper committer/maintainer, so I have some sense that if I can find time to do that, it'll have some chance of being reviewed/merged and my work won't be in vain? :) Thanks!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that makes perfect sense. I'd love to see that get improved. And yes, I will be able to review and merge. Sometimes I'm slower to review that I'd personally like, so if that becomes the case, just bug me. Thanks for the help!

<% else %>
require 'spec_helper'
<% end %>

RSpec.describe <%= class_name %>Decorator do
RSpec.describe <%= class_name %>Decorator, type: :decorator do
end
4 changes: 2 additions & 2 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 @@ -141,7 +141,7 @@ module Draper
it "delegates array methods to the decorated collection" do
decorator = CollectionDecorator.new([])

allow(decorator).to receive_message_chain(:decorated_collection, :[]).with(42).and_return(:delegated)
allow(decorator.decorated_collection).to receive(:[]).with(42).and_return(:delegated)
expect(decorator[42]).to be :delegated
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
3 changes: 2 additions & 1 deletion spec/draper/decoratable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
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
product = Product.new
Expand Down
2 changes: 1 addition & 1 deletion 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
valid_options = {with: Decorator, scope: :foo, context: {}}
Expand Down
2 changes: 1 addition & 1 deletion spec/draper/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 Decorator do
RSpec.describe Decorator do
it_behaves_like "view helpers", Decorator.new(Model.new)

describe "#initialize" do
Expand Down
4 changes: 2 additions & 2 deletions spec/draper/factory_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

module Draper
describe Factory do
Rspec.describe Factory do
describe "#initialize" do
it "accepts valid options" do
valid_options = {with: Decorator, context: {foo: "bar"}}
Expand Down Expand Up @@ -89,7 +89,7 @@ module Draper
end
end

describe Factory::Worker do
Rspec.describe Factory::Worker do
describe "#call" do
it "calls the decorator method" do
object = double
Expand Down
2 changes: 1 addition & 1 deletion spec/draper/finders_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

module Draper
describe Finders do
RSpec.describe Finders do
protect_class ProductDecorator
before { ProductDecorator.decorates_finders }

Expand Down
2 changes: 1 addition & 1 deletion spec/draper/helper_proxy_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/draper/lazy_helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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)
Expand Down
2 changes: 1 addition & 1 deletion spec/draper/undecorate_spec.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
4 changes: 2 additions & 2 deletions spec/draper/view_context/build_strategy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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
Expand Down Expand Up @@ -86,7 +86,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
Expand Down
2 changes: 1 addition & 1 deletion spec/draper/view_context_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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 } }
Expand Down
2 changes: 1 addition & 1 deletion spec/draper/view_helpers_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 ViewHelpers do
RSpec.describe ViewHelpers do
it_behaves_like "view helpers", Class.new{include ViewHelpers}.new
end
end
4 changes: 2 additions & 2 deletions spec/dummy/fast_spec/post_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand All @@ -32,6 +32,6 @@
end

it "can't be passed implicitly to url_for" do
expect{decorator.link}.to raise_error ArgumentError
expect{decorator.link}.to raise_error ArgumentError, /url_for/
end
end
4 changes: 2 additions & 2 deletions spec/dummy/spec/decorators/active_model_serializers_spec.rb
Original file line number Diff line number Diff line change
@@ -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::Serializer::CollectionSerializer" do
collection_decorator = Draper::CollectionDecorator.new([])
Expand Down
4 changes: 2 additions & 2 deletions spec/dummy/spec/decorators/devise_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/dummy/spec/decorators/helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/dummy/spec/decorators/post_decorator_spec.rb
Original file line number Diff line number Diff line change
@@ -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 }

Expand Down
4 changes: 2 additions & 2 deletions spec/dummy/spec/decorators/spec_type_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions spec/dummy/spec/decorators/view_context_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'spec_helper'
require_relative '../rails_helper'

def it_does_not_leak_view_context
2.times do
Expand All @@ -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
4 changes: 2 additions & 2 deletions spec/dummy/spec/mailers/post_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down
6 changes: 3 additions & 3 deletions spec/dummy/spec/models/mongoid_post_spec.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions spec/dummy/spec/models/post_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'spec_helper'
require 'shared_examples/decoratable'
require_relative '../spec_helper'
require_relative '../shared_examples/decoratable'

RSpec.describe Post do
it_behaves_like 'a decoratable model'
Expand Down
34 changes: 34 additions & 0 deletions spec/dummy/spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/dummy/spec/shared_examples/decoratable.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
62 changes: 57 additions & 5 deletions spec/dummy/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/integration/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,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
Expand Down
Loading