Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

before hook in rspec same as before :all?

I am wondering whether the before (as seen below) is the same as before :all in RSpec. Sometimes neither :each nor :all is specified and it confuses me as to what before actually does.

require 'spec_helper'  describe "this is a description" do    before do # vs. before :all or before :each    # do something...   end end 

Would appreciate if anyone can explain the differences, if any. Thanks!

like image 870
Jonathan Lin Avatar asked Aug 28 '12 03:08

Jonathan Lin


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 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.

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 .


1 Answers

So the answer is No. before is equivalent to before :each, not before :all

Test example.

Update: Wow, this question is popular. To save your head from cognitive overload, I suggest you explicitly state :each or :all.

like image 150
Jonathan Lin Avatar answered Oct 04 '22 14:10

Jonathan Lin