Use RSpec's expect etc. outside a describe ... it block
include ::RSpec::Matchers
class A
include ::RSpec::Matchers
def test
expect('1'.to_i).to eq 1
end
def failed_test
expect('1'.to_i).to eq 2
end
end
A.new.test
# => true
A.new.failed_test
# RSpec::Expectations::ExpectationNotMetError:
# expected: 2
# got: 1
rspec's .should fails (outside describe/it block) in Ruby 2?
If you want to use it outside a describe/it
block, it seems you'd have to enable it first, although the documentation states it is enabled by default. I assume that by enable by default
means within an spec file [source] only. 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
How can I use rspec expectations and matchers outside of Rspec?
When you include
a module, it makes its methods available to instances of the class. Your test
method is a singleton method (a "class method"), not an instance method, and thus will never have access to methods provided by mixed-in modules. To fix it, you can do:
class BF
include ::RSpec::Matchers
def test
expect(1).to eq(1)
end
end
BF.new.test
If you want the RSpec::Matchers
methods to be available to singleton methods of BF
, you can instead extend
the module:
class BF
extend ::RSpec::Matchers
def self.test
expect(1).to eq(1)
end
end
BF.test
Can rspec's expect parse a block to confirm a nested array/grid?
The expect version of your spec is
subject.cell_grid.each do |row|
expect(row.is_a?(Array)).to be_truthy
end
(The be_true
matcher no longer exists)
Slightly more naturally you would write
subject.cell_grid.each do |row|
expect(row).to be_an(Array)
end
You could use all?
to have only one call to expect but this will lead to less helpful failure messages.
You wouldn't usually pass a block to expect just to make an assertion about a value - typically this is used to check for side effects (such as raising an exception).
How to call same methods inside and outside 'context' and 'it' block
Rather than using proc or scopes,
Simply use local variables outside describe block.
email_ids = [
'test1@example.com',
'test2@example.com',
'test3@example.com'
]
describe 'Emails' do
end
RSpec: how controller test should look like?
describe "delete" do
before(:each) do
@foo = Foo.create
end
it "should delete from the database" do
expect(delete :destroy, id: @foo).to change(Foo, :count).by(-1)
end
context do
before(:each) do
delete :destroy, id: @foo
end
it "should redirect" do
expect(response).to redirect_to some_url
end
it "should set the flash" do
expect(flash[:notice]).to_not be_nil
expect(flash[:error]).to be_nil
end
end
end
Yes, the delete action appears twice. Test code is often hard to DRY up. IMHO you shouldn't sacrifice clarity to save space when writing specs.
There should be one way to do one thing.
Sure, we can have best practices, but that doesn't mean all specs need to be written the same way if there is some compelling reason to do otherwise.
Rspec - access describe name in describe context, not in `it`
In your case
self.class
#=> Class
Thus, because you are in the context of the DescribeTitle
already, you need to just use self.description
instead of self.class.description
:
self.description
#=> DescribeTitle
Related Topics
Ruby Refuses to Divide Correctly
How to Read a Gzip File Line by Line
Rails: Wkhtmltopdf Runtimeerror (Location of Wkhtmltopdf Unknown)
Stack Level Too Deep When Using Carrierwave Versions
How to Disable All Form_For Input Fields in Ruby on Rails App
Duplicating a Ruby Array of Strings
Saml 2.0 Sso for Ruby on Rails
Ruby on Rails: How to Use Oauth2::Accesstoken.Post
How to Connect to Browser Using Ruby Selenium Webdriver
Check If Array of Integers Increments in Ruby
Is Getting Converted as "\U0092" by Nokogiri in Ruby on Rails
What Are Symbols and How Do We Use Them
Extending a Class Method in a Module
Get an Array from a Rails Form
Ransack, Postgres - Sort on Column from Associated Table with Distinct: True
What Is the Purpose of Redo and Retry Statements in Ruby