Rspec.should fail (outside of describe / its block) in Ruby 2?

In Ruby 2, with gem rspec 2.14.1 (the latest Ubuntu), with Rails it is not installed, why is this failing?

require 'rubygems' require 'rspec' 3 .should == 3 NoMethodError: undefined method `should' for 3:Fixnum 

For many years I relied on the convenient idiom x .should == y .

https://www.relishapp.com/rspec/rspec-expectations/v/2-14/docs/syntax-configuration and https://www.relishapp.com/rspec/rspec-expectations/docs/syntax-configuration say that this syntax is still supported by default.

Edit: Added β€œoutside describe / it block” to the title, as this is apparently the main reason.

+2
source share
3 answers

If you want to use it outside the describe/it block, it seems you will have to enable it first, although the documentation says that it is enabled by default. I believe that enable by default means only in the spec file [ source ]. For instance:

 require 'rubygems' require 'rspec' RSpec.configure do |config| config.expect_with :rspec do |c| c.syntax = :should end end p 3.should == 3 # true 
+4
source

rspec expectations are a gem that should added to every object. The rspec request rspec loads the rspec metacharacter (which exists solely as one gem install , which provides all rspec), but does not automatically load rspec expectations. rspec-core allows you to configure it to use something other than rspec expectations if you want (e.g. stdlib statements provided by minitest or incorrect), but it loads rspec expectations by default. To achieve this, if you have not explicitly configured it, it expects rspec-expectations to load before the first call to describe for historical reasons, as explained in my blog post .

So, if you want to do Object#should immediately, you just need to require rspec/expectations . Please note that we plan to change the default value in RSpec 4 so that it should not be automatically available without additional configuration. Also, as mentioned in @JonRowe, this use is not really intended use. You can call foo.should from any context, but matching methods designed to work with should not be available in all contexts. You must include RSpec::Matchers in your context to make them available. Consider also switching to expect syntax: this is a newer, non-monkeypatching syntax, which we recommend for a while .

+3
source

It is not supported outside of the RSpec example, for example. inside the it block inside the describe block. Do not use it that way.

+1
source

All Articles