"it"关键字在RSpec中的作用是什么?

Dan*_*iel 16 ruby rspec

我正在关注rails3tutorial,在进行一些测试时我不明白"it"关键字的含义如下:

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
Run Code Online (Sandbox Code Playgroud)

代码片段来自:http://ruby.railstutorial.org/chapters/filling-in-the-layout#top上市5.26

Rus*_*ell 18

我认为其他答案可以更明确,最初可能会让你感到困惑的是,it打破方法命名的大多数常用约定(例如,方法没有描述它的作用)以便制作代码作为一个整体读作一种句子.

因此,库不仅仅是创建一组测试,而是试图鼓励您通过类似于人类可读规范的方式来描述您的应用程序.


phi*_*ant 14

它不是Ruby关键字,它是Rspec框架的一部分.

it包含说明正在定义的行为方面的代码示例.它由两个主要部分组成:描述字符串和代码示例,在do/end块中.


luc*_*tte 5

它不是关键字.它只是RSpec提供的用于描述规范样本的方法.有关进一步说明,请参阅文档.