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?
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.
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 .
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With