Daniel
Daniel

Reputation: 367

what does the "it" keyword do in RSpec?

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

Upvotes: 23

Views: 21115

Answers (5)

Taimoor Changaiz
Taimoor Changaiz

Reputation: 10684

In a general unit testing sense, we use describe to describe the behavior of a class:

describe Hash do

end

Tests are written using the it block. Here’s an example of how you might write a spec for the Hash class:

describe Hash do
  it "should return a blank instance" do
    Hash.new.should == {}
  end
end

For more help use it

http://blog.teamtreehouse.com/an-introduction-to-rspec

Upvotes: 3

philant
philant

Reputation: 35836

It's not a Ruby keyword, it's part of the Rspec framework.

it contains the code examples that illustrate the facet of behavior being defined. It is comprised of two main parts: the description string and the code example, in the do/end block.

Upvotes: 17

Andrew Grimm
Andrew Grimm

Reputation: 81540

As others have said, it is not a keyword.

That being said, a lot of words that appear to be keywords aren't keywords. For example, puts looks like a keyword, but it's merely a method in the Kernel module.

Upvotes: 0

Russell
Russell

Reputation: 12439

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.

Upvotes: 22

lucapette
lucapette

Reputation: 20724

It's not a keyword. It's just a method provided by RSpec used to describe spec samples. See docs for further explanation.

Upvotes: 6

Related Questions