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
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.
The do keyword is used together with the end keyword to delimit a code block.
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.
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.
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.
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