Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In RSpec, what's the difference between before(:suite) and before(:all)?

Tags:

rspec

rspec2

The before and after hook documentation on Relish only shows that before(:suite) is called prior to before(:all).

When should I use one over the other?

like image 848
Mark Rushakoff Avatar asked Mar 16 '13 04:03

Mark Rushakoff


People also ask

What does before do in RSpec?

The before(:each) method is where we define the setup code. When you pass the :each argument, you are instructing the before method to run before each example in your Example Group i.e. the two it blocks inside the describe block in the code above.

What is let in RSpec?

let generates a method whose return value is memoized after the first call. This is known as lazy loading because the value is not loaded into memory until the method is called. Here is an example of how let is used within an RSpec test. let will generate a method called thing which returns a new instance of Thing .

What is describe in RSpec?

The word describe is an RSpec keyword. It is used to define an “Example Group”. You can think of an “Example Group” as a collection of tests. The describe keyword can take a class name and/or string argument.

How do I set up RSpec?

Installing RSpecBoot up your terminal and punch in gem install rspec to install RSpec. Once that's done, you can verify your version of RSpec with rspec --version , which will output the current version of each of the packaged gems. Take a minute also to hit rspec --help and look through the various options available.


1 Answers

When a before(:all) is defined in the RSpec.configure block it is called before each top level example group, while a before(:suite) code block is only called once.

Here's an example:

RSpec.configure do |config|   config.before(:all) { puts 'Before :all' }   config.after(:all) { puts 'After :all' }   config.before(:suite) { puts 'Before :suite' }   config.after(:suite) { puts 'After :suite' } end  describe 'spec1' do   example 'spec1' do     puts 'spec1'   end end  describe 'spec2' do   example 'spec2' do     puts 'spec2'   end end 

Output:

Before :suite Before :all spec1 After :all Before :all spec2 After :all After :suite 
like image 69
Leo Avatar answered Oct 04 '22 14:10

Leo