I'm following the rails3tutorial and I don't understand the meaning of the "it" keyword when doing some testing as follows:
我正在跟踪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
code fragment comes from: http://ruby.railstutorial.org/chapters/filling-in-the-layout#top listing 5.26
代码片段来自:http://ruby.railstutorial.org/chapters/filling-in- layout#顶部清单5.26
6 个解决方案
#1
13
It's not a Ruby keyword, it's part of the Rspec framework.
它不是Ruby关键字,而是Rspec框架的一部分。
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.
它包含说明正在定义的行为方面的代码示例。它由两个主要部分组成:do/end块中的描述字符串和代码示例。
#2
17
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.
因此,库不只是创建一组测试,而是鼓励您通过类似于人类可读规范的测试来描述应用程序。
#3
#4
2
In a general unit testing sense, we use describe to describe the behavior of a class:
在一般的单元测试意义上,我们使用description来描述类的行为:
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:
测试是使用it块编写的。下面是如何为哈希类编写规范的示例:
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
http://blog.teamtreehouse.com/an-introduction-to-rspec
#5
1
This is Rspec key word . RSpec uses the words "describe" and "it" so we can express concepts like a conversation:
这是Rspec的关键词。RSpec使用了“描述”和“it”这样我们可以像对话一样表达概念:
"Describe an account when it is first opened." "It has a balance of zero." The describe method creates an example group. Within the block passed to describe you can declare nested groups using the describe or context methods, or you can declare examples using the it or specify methods.
“描述一个帐户刚开的时候。”“它的余额为零。”description方法创建一个示例组。在传递给describe的块中,可以使用describe或上下文方法声明嵌套的组,也可以使用it或指定方法声明示例。
Under the hood, an example group is a class in which the block passed to describe or context is evaluated. The blocks passed to it are evaluated in the context of an instance of that class.
在引擎盖下,一个示例组是一个用于描述或描述上下文的类。传递给它的块在该类实例的上下文中进行计算。
#6
0
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.
话虽如此,很多看似关键字的词并不是关键字。例如,put看起来像一个关键字,但它只是内核模块中的一个方法。
#1
13
It's not a Ruby keyword, it's part of the Rspec framework.
它不是Ruby关键字,而是Rspec框架的一部分。
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.
它包含说明正在定义的行为方面的代码示例。它由两个主要部分组成:do/end块中的描述字符串和代码示例。
#2
17
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.
因此,库不只是创建一组测试,而是鼓励您通过类似于人类可读规范的测试来描述应用程序。
#3
5
It's not a keyword. It's just a method provided by RSpec used to describe spec samples. See docs for further explanation.
这不是一个关键字。这只是RSpec提供的一种方法,用于描述spec示例。请参阅文档以获得进一步的解释。
#4
2
In a general unit testing sense, we use describe to describe the behavior of a class:
在一般的单元测试意义上,我们使用description来描述类的行为:
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:
测试是使用it块编写的。下面是如何为哈希类编写规范的示例:
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
http://blog.teamtreehouse.com/an-introduction-to-rspec
#5
1
This is Rspec key word . RSpec uses the words "describe" and "it" so we can express concepts like a conversation:
这是Rspec的关键词。RSpec使用了“描述”和“it”这样我们可以像对话一样表达概念:
"Describe an account when it is first opened." "It has a balance of zero." The describe method creates an example group. Within the block passed to describe you can declare nested groups using the describe or context methods, or you can declare examples using the it or specify methods.
“描述一个帐户刚开的时候。”“它的余额为零。”description方法创建一个示例组。在传递给describe的块中,可以使用describe或上下文方法声明嵌套的组,也可以使用it或指定方法声明示例。
Under the hood, an example group is a class in which the block passed to describe or context is evaluated. The blocks passed to it are evaluated in the context of an instance of that class.
在引擎盖下,一个示例组是一个用于描述或描述上下文的类。传递给它的块在该类实例的上下文中进行计算。
#6
0
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.
话虽如此,很多看似关键字的词并不是关键字。例如,put看起来像一个关键字,但它只是内核模块中的一个方法。