Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Build a Todo List Application with Rails 4 Build a Todo List Application with Rails 4 Editing Todo Lists

Alan Khoury
Alan Khoury
6,127 Points

How do i removed these depreciation warnings?

when i run bin/rspec spec/features/todo_lists/edit_spec.rb

i get

.....

Deprecation Warnings:


RSpec::Core::ExampleGroup#example is deprecated and will be removed in RSpec 3. There are a few options for what you can use instead:

  • rspec-core's DSL methods (it, before, after, let, subject, etc) now yield the example as a block argument, and that is the recommended way to access the current example from those contexts.
  • The current example is now exposed via RSpec.current_example, which is accessible from any context.
  • If you can't update the code at this call site (e.g. because it is in an extension gem), you can use this snippet to continue making this method available in RSpec 2.99 and RSpec 3:

    RSpec.configure do |c| c.expose_current_running_example_as :example end

(Called from /Users/myusername/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/capybara-2.1.0/lib/capybara/rspec.rb:20:in `block (2 levels) in <top (required)>')


RSpec::Core::ExampleGroup#example is deprecated and will be removed in RSpec 3. There are a few options for what you can use instead:

  • rspec-core's DSL methods (it, before, after, let, subject, etc) now yield the example as a block argument, and that is the recommended way to access the current example from those contexts.
  • The current example is now exposed via RSpec.current_example, which is accessible from any context.
  • If you can't update the code at this call site (e.g. because it is in an extension gem), you can use this snippet to continue making this method available in RSpec 2.99 and RSpec 3:

    RSpec.configure do |c| c.expose_current_running_example_as :example end

(Called from /Users/myusername/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/capybara-2.1.0/lib/capybara/rspec.rb:21:in `block (2 levels) in <top (required)>')

If you need more of the backtrace for any of these deprecations to identify where to make the necessary changes, you can configure config.raise_errors_for_deprecations!, and it will turn the deprecation warnings into errors, giving you the full backtrace.

2 deprecation warnings total

Finished in 0.17866 seconds 5 examples, 0 failures

Randomized with seed 38251

What is it all about and what do i do to get rid of it? Thanks!

15 Answers

Anna Petry
Anna Petry
14,474 Points

I was getting those deprecation errors, too, and the snippet posted above by Jayphen Simpson worked for me. I was also getting the following deprecation error:


rspec-rails 3 will no longer automatically infer an example group's spec type
from the file location. You can explicitly opt-in to this feature using this
snippet:

RSpec.configure do |config|
  config.infer_spec_type_from_file_location!
end

If you wish to manually label spec types via metadata you can safely ignore
this warning and continue upgrading to RSpec 3 without addressing it.

Seems obvious, but I added the config.infer_spec_type_from_file_location! snippet from the error message to the spec_helper.rb file (within the 'RSpec configure do' block) and the final deprecation error went away.

Hope that might help someone.

Worked just fine, if only I have looked into the alert code first! Thanks!

Jamie Goodwin
Jamie Goodwin
14,251 Points

Thank you, this totally sorted it for me as well. Legendary.

I'm not really sure why they're happening (it's related to rspec 2.99), but you can get rid of them by adding the following to spec_helper.rb

config.expose_current_running_example_as :example

Mark Railton
Mark Railton
8,468 Points

Perfect, this was exactly what I was looking for.

Michelle Cannito
Michelle Cannito
8,992 Points

Wow, it worked! I was happy when I could run rspec without being in the bin subdirectory. I was thrilled when I got rid of the LoadError I was struggling with for two days. And now I am in a zen-like state that the run output is both correct and uncluttered. Coding is so cool. Thank you so much.

Worked like charm.

Perfect. Thanks for the fix. It worked.

Adding this bit to spec_helper.rb also helped me! Thanks a bunch!

Dylan Shine
Dylan Shine
17,565 Points
require 'spec_helper'

# Specs in this file have access to a helper object that includes
# the TodoListsHelper. For example:
#
# describe TodoListsHelper do
#   describe "string concat" do
#     it "concats two strings with spaces" do
#       expect(helper.concat_strings("this","that")).to eq("this that")
#     end
#   end
# end


describe TodoListsHelper do
  pending "add some examples to (or delete) #{__FILE__}"
end

RSpec.configure do |config|
  config.expose_current_running_example_as :example
  config.raise_errors_for_deprecations!
end

Still getting the errors, can you help me?

Crisoforo Gaspar Hernández
Crisoforo Gaspar Hernández
18,302 Points

The answer:

expose_current_running_example_as :example config option is meant to support users who are using gems that depend on the RSpec 2 API. It's not meant for library authors to use.

The source if you wan to check more.

Fábio Tavares da Costa
Fábio Tavares da Costa
11,985 Points

Cool!

It continues to work as Jul, 14, 2015.

I passed the following snippet at very end of odot/spec/spec_helper.rb. The log suggests a slightly different notation, so I used the following:

RSpec.configure do |c|
  # RSpec::Core::ExampleGroup#example is deprecated and will be removed in RSpec 3.
  # [...] snippet to continue making this method available in RSpec 2.99 and RSpec 3:
  c.expose_current_running_example_as :example
end

# VM spec
# uname --all
# Linux treehouse 3.8.0-33-generic #48~precise1-Ubuntu SMP Thu Oct 24 16:31:16 UTC 2013 i686 i686 i386 GNU/Linux

Everything works with a clean log after the addition.

treehouse:~/projects/odot (master *) $ bin/rspec spec/features/todo_lists/create_spec.rb 
..

Finished in 0.44521 seconds
2 examples, 0 failures

Randomized with seed 1999

Cheers!

This fixed issue for me! Thank you, that crazy long message was annoying... Cheers!

By running this command, it exposes the current running example via the named helper method. Since we're using extension gem Capybara in this tutorial, this config option is used to maintain compatibility between different versions of Rspec.

Mike Nagle
Mike Nagle
4,266 Points

There's some more information about depreciation warnings on upgrading to rspec3 here: http://rspec.info/upgrading-from-rspec-2/

I used the transpec tool to remove 7 warnings and the last two by adding the config line above

Jonathan Chua
Jonathan Chua
4,136 Points

The answer string on this question is a little convoluted. The specific solution seems to depend on the exact depreciation error that comes up and what the code in your odot/spec/spec_helper.rb file looks like.

If you see this:

rspec-rails 3 will no longer automatically infer an example group's spec type
from the file location. You can explicitly opt-in to this feature using this
snippet:

RSpec.configure do |config|
  config.infer_spec_type_from_file_location!
end

then add this to the Rspec.configure do |config| block just above the line that contains end.

config.infer_spec_type_from_file_location!

My spec_helper.rb file already contained that line of code. My depreciation warning looked like this:

RSpec::Core::ExampleGroup#example is deprecated and will be removed                                           
in RSpec 3. There are a few options for what you can use instead:                                             

  - rspec-core's DSL methods (`it`, `before`, `after`, `let`, `subject`, etc)                                 
    now yield the example as a block argument, and that is the recommended                                    
    way to access the current example from those contexts.                                                    
  - The current example is now exposed via `RSpec.current_example`,                                           
    which is accessible from any context.                                                                     
  - If you can't update the code at this call site (e.g. because it is in                                     
    an extension gem), you can use this snippet to continue making this                                       
    method available in RSpec 2.99 and RSpec 3:                                                               

      RSpec.configure do |c|                                                                                  
        c.expose_current_running_example_as :example                                                          
      end 

The solution is to add this to the Rspec.configure do |c| block just above the line that contains end.

c.expose_current_running_example_as :example 

However, my spec_helper.rb file does not contain a Rspec.configure do |c| block. Instead it has Rspec.configure do |config|.

In this case, the solution is to add this to the Rspec.configure do |config| block just above the line that contains end.

config.expose_current_running_example_as :example

What are deprecation warnings and why do these code snippets make them go away?

A depreciation warning tells you that in the next version of the software something will not work the way that it is working now in the current version. Adding these snippets explicitly tells rspec to continue behaving the same if and when you upgrade it to the next version. If a depreciation warning is ignored and the software is upgraded it will probably break your code and you'll have to rewrite a bunch of it to make it work again.

Sara Greer
Sara Greer
16,032 Points

Thank you for summing everything up, Jonathan!

Does anyone want to explain why and how this works? :)

I would also like an explanation as to what's happening here. Thanks.

Thank you for this, it worked!

Thank you for the tip, worked like a charm!

Thanks a million for this :)

Owen Tran
Owen Tran
6,822 Points

I had identical message.

I added : config.expose_current_running_example_as :example

to the spec_helper.rb

inside the RSpec.configure do |config| block at the bottom just before end.

Luke Van Lathum
Luke Van Lathum
462 Points

I had a larger message and I simply added

RSpec.configure do |c| c.expose_current_running_example_as :example end

to the spec_helper.rb file in the spec folder :)