Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does the "it" keyword do in RSpec?

Tags:

I'm following the rails3tutorial and I don't understand the meaning of the "it" keyword when doing some testing as follows:

require 'spec_helper'  describe UsersController do   render_views    describe "GET 'new'" do     it "should be successful" do       get 'new'       response.should be_success     end      it "should have the right title" do       get 'new'       response.should have_selector("title", :content => "Sign up")     end   end end 

code fragment comes from: http://ruby.railstutorial.org/chapters/filling-in-the-layout#top listing 5.26

like image 267
Daniel Avatar asked Jan 16 '12 16:01

Daniel


People also ask

What is it do in RSpec?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.

What is do keyword in Ruby?

The do keyword is used together with the end keyword to delimit a code block.

What is context in RSpec?

According to the rspec source code, “context” is just a alias method of “describe”, meaning that there is no functional difference between these two methods. However, there is a contextual difference that'll help to make your tests more understandable by using both of them.

What is let in RSpec?

Use let to define a memoized helper method. The value will be cached. across multiple calls in the same example but not across examples. Note that let is lazy-evaluated: it is not evaluated until the first time. the method it defines is invoked.


1 Answers

What I think the other answers could make more explicit, and what may be what initially confused you, is that it breaks most of the usual conventions for method naming (nothing about the method describes what it does, for example) in order to make the code as a whole read as a sort of sentence.

So rather than just creating a set of tests, the library is trying to encourage you to describe your application through tests in a way that resembles a human-readable specification.

like image 112
Russell Avatar answered Sep 24 '22 00:09

Russell