[翻译] Railscasts[155] Beginning With Cucumber // 双语版

Cucumber  is a high-level testing framework which is designed to let you use Behaviour Driven Development (BDD) to create Ruby on Rails applications. Cucumber’s unique feature is that it uses English (or a number of other supported languages) to define an application’s behaviour. In this episode we’re going to create a Rails application from scratch, and use Cucumber to define its behaviour.

 

Cucumber 是一个用来对Ruby On Rails应用进行行为驱动开发(BDD)的高级测试框架。Cucumber的独有特点是他使用英语(或者其他支持的语言,包括汉语)来定义程序的行为,在这一集我们将从头开始创建Rails 应用程序,并且使用Cucumber来定义其行为。

 

Creating The Application

创建应用程序

We’ll start by creating a new Rails application and for the purposes of this example we’re going to use the classic example and create a blogging app. To start we’ll create the application in the usual way.

我们将首先创建一个新的Rails应用程序,并为这个例子使用最经典的例子来创建一个博客程序。首先我们使用最通常的方式来创建一个Rails项目。

 

 

rails blog

 

Next we’ll have to set up the testing environment to use the gems we’ll need. At the bottom of /config/environments/test.rb  we’ll add the following code.

接下来,我们需要使用我们将要用到的这些gems来创建测试环境。

在 /config/environments/test.rb 的底部加入这些代码:

 

 

config.gem "rspec", :lib => false, :version => ">=1.2.2"  
config.gem "rspec-rails", :lib => false, :version => ">=1.2.2"  
config.gem "webrat", :lib => false, :version => ">=0.4.3"  
config.gem "cucumber", :lib => false, :version => ">=0.2.2"  

 As well as Cucumber we’re going to use RSpec  and Webrat . Neither are required to use Cucumber, but they work well together. When you become more proficient with Cucumber you could use Test::Unit  instead of RSpec or replace Webrat with a different testing framework.

 

除了Cucumber,我们还需要 Rspec 和 Webrat.他们都不是运行Cucumber必须的,但是他们可以很好的协同工作。当你熟悉使用Cucumber后你可以使用单元测试来代替Rspec或用一个不同的测试框架来代替Webrat.

 

Now that we’ve specified the gems we need we’ll make sure they’re installed by running the appropriate rake command.

 

现在我们在程序中已经指定了需要的gems ,我们通过运行rake命令来安装他们:

 

sudo rake gems:install RAILS_ENV=test

 

A few dependent gems will be installed with the gems listed above and this may take a few minutes if you don’t already have them. Once everything is installed we can set up Cucumber for our application with

如果你还没安装过这些gems,他们将会被自动安装。安装完毕后我们就可以为应用程序建立Cucumber了。

 

script/generate cucumber

This will generate a features  directory under our application’s root directory and it’s here where we’ll define our application’s behaviour.

这个命令会在应用程序根目录下创建一个features文件夹。这就是我们定义应用程序行为的地方。

Creating Our First Feature

创建我们的第一个故事片 (感觉自己像导演吧!):

译注:关于feature究竟应该翻译成"功能"还是"故事片"或者别的什么,我觉得翻译成故事片更合适。因为接下来就是场景什么的,我们把自己当成是一个导演,演员就是我们应用程序的用户,告诉演员要干什么干什么。从这点上来讲,cucumber确实够酷。

 

We’ll want to manage articles with our blog application, so we’ll create a file called manage_articles.feature  in the features  directory. (If you’re using TextMate then there’s a plug-in available  that will give you syntax colouring for Cucumber’s .feature files.)

 

在我们创建的这个博客程序中,我们想要管理文章,所以我们在features文件夹下新建一个叫 manage_atricles.feature的文件。(如果你使用TextMate作为开发工具,那么你可以去下载cucumber的tm bundle,地址http://github.com/bmabey/cucumber-tmbundle )

 

Cucumber definitions have two sections. The first is optional and is used to define the feature itself. There are three parts to this section: “In order”, “As a” and “I Want”. For our blog application we’ll define this section like this

 

Cucumber的定义由两个部份组成。第一项是可选的,用来定义故事片情节本身,他们由3个部份组成:“In order”, “As a” 和 “I Want”. 我们像这样来定义我们的博客程序:

 

Feature: Manage Articles
  In order to make a blog
  As an author
  I want to create and manage articles

 

 

The “In order” line defines our overall goal, which is to make a blog. The “As a” line defines a condition, and the last line states the feature we want to implement.

 

"In ordder"行定义了我们的总体目标,就是创建一个博客。"As a"行定义了一个条件,可以理解为故事片中的演员。最后一行是我们想要实现的具体功能,也就是这个故事片的主要情节。

 

 

Now we’ll define some of the feature’s behaviour. This is done by writing one or more scenarios. A scenario is specified in a similar way to a feature, only now we have “Given”, “When” and “Then” parts. We’ll write a scenario that defines the behaviour for the page that lists the articles in our blog.


现在我们来定义一些这个故事片种的一些行为(情节),这是通过一个或者多个场景来实现的。场景通常按照一种相似的方式来设计,现在我们用“Given”,"When" 和 "Then"三个部份来组成场景。我们要写一个列出博客文章的场景。

Scenario: Articles List
    Given I have articles titled Pizza, Breadsticks
    When I go to the list of articles
    Then I should see "Pizza"
    And I should see "Breadsticks"
  With that done we can now run Cucumber for the first time and see the output it returns. We’re using the -n  switch to make Cucmumber return slightly more concise output.
完成以后,我们来运行Cucumber,看它的返回的输出。我们用-n 选项来让Cucumber返回一个更简介的输出:
$ cucumber features -n
Feature: Manage Articles
	In order to make a blog
	As an author
	I want to create and manage articles

  Scenario: Articles List
    Given I have articles titled Pizza, Breadsticks
    When I go to the list of articles
    Then I should see "Pizza"
    And I should see "Breadsticks"

1 scenario
3 skipped steps
1 undefined step

You can implement step definitions for missing steps with these snippets:

Given /^I have articles titled Pizza, Breadsticks$/ do
  pending
end
 
Cucumber shows us our feature and the results of running the scenario. Three of the steps were skipped and one is marked as undefined. Cucumber doesn’t know how to run the undefined step as we’ve not yet written the Ruby code to implement it. To start us off Cucumber helpfully provides a code template that we can paste in.

Cucumber显示了我们的剧本纲要和场景演出的结果。其中3个步骤被跳过,1个步骤未定义。Cucumber不知道怎么运行没定义的步骤,因为我们还没有写Ruby代码来执行。Cucumber提供了一段我们可以直接粘贴到我们的程序的代码模板。

Defining The “Given” Step

定义"Given"步骤

Steps are defined in step_definitions  under the features  directory. We’ll create a file called article_steps.rb  in this directory so that we can start to define our scenario.

 

场景步骤被定义在 features 文件中的step_definitions文件夹中。我们在这个文件中新建一个叫atricle_steps.rb的文件来定义我们的场景。

 

 

Given /^I have articles titled Pizza, Breadsticks$/ do
  pending
end

 

Our step is defined as a regular expression. This will interpret the plain English in the scenario and turn it into something that we can use in Ruby code. It’s a good idea to make a step work with any data passed to it so we’ll replace “Pizza, Breadsticks” in the regular expression with a sub-expression and then matching each title that the sub-expression returns.

 

我们的步骤被定义成一个正则表达式。它会定义这个场景种的文本语言并且返回成我们可以用到Ruby代码里去的一些东西。让这个步骤具有可以处理任何传入的数据的能力是个不错的主意,所以我们要把正则表达式中的"Pizza,Breadsticks"替换成一个子表达式,然后匹配子表达式返回的每个标题。

 

 

Given /^I have articles titled (.+)$/ do |titles|
  titles.split(', ').each do |title|
    Article.create!(:title => title)
  end
end

 

We’re going to pass a list of titles separated by a comma and a space and that list will be matched by the (.+)  part of the regular expression and passed to the titles  variable. We then split that string at “, “  to get each title. With each title matched we then create a newArticle  with that title. This will satisfy our Given  condition.

 

我们将传入一些由逗号和空格分隔的可以被(.+)部份匹配的标题,并且传递给titles这个变量。然后,我们通过","把字符串分隔成单独的title。每个能匹配上的标题我们都会用来创建一篇新的文章。这就可以满足Given的条件了。

 

 

What About The Other Steps?

其他的步骤呢?

When we ran Cucumber it told us that one step was undefined and skipped the other three. This means that although they weren’t executed, the step definitions do exist. If this is so then where are they defined? The answer is that Webrat defines a set of steps in a file calledwebrat_steps.rb  in the same directory as our article steps. These steps define a list of common tasks, such as going to a page.

 

当我们运行Cucumber的时候,它告诉我们有一个步骤没有被定义,其他三个步骤被跳过。这意味着虽然他们没有被执行,但是这些步骤的定义是存在的。如果是这样的话,那么他们该在哪里定义呢?答案是Webrat在和article步骤相同目录下的一个叫webrat_steps.rb的文件里定义了一系列的步骤。这些步骤定义了一些常用的任务,比如进入到一个页面。

 

 

When /^I go to (.+)$/ do |page_name|
  visit path_to(page_name)
end

 

This step definition matches our second step (When I go to the list of articles ) and there are step definitions to match the other skipped steps, too.

 

这个步骤定义符合我们第二步(当我查看文章列表)并且还有符合其他被跳过的步骤的定义。

 

 

Making It Turn Green, Step By Step

一步一步的把他变绿

Now that all of our steps are defined we can run Cucumber again and check the output to see what we need to do next.

现在我们所有的步骤都定义好了,我们可以再次运行Cucumber,并且查看Cucumber的输出来决定我们下一步该做什么。

 

 

$ cucumber features -n
Feature: Manage Articles
	In order to make a blog
	As an author
	I want to create and manage articles

  Scenario: Articles List
    Given I have articles titled Pizza, Breadsticks
      uninitialized constant Article (NameError)
      ./features/step_definitions/article_steps.rb:3:in `__instance_exec0'
      ./features/step_definitions/article_steps.rb:2:in `each'
      ./features/step_definitions/article_steps.rb:2:in `/^I have articles titled (.+)$/'
      features/manage_articles.feature:7:in `Given I have articles titled Pizza, Breadsticks'
    When I go to the list of articles
    Then I should see "Pizza"
    And I should see "Breadsticks"

1 scenario
1 failed step
3 skipped steps

 

This time instead of an undefined step we have a failing step. The step fails because we don’t have an Article  model. To fix this we’ll generate it and, as we’re using RSpec, we’ll use the RSpec generator to create it.

 

这次我们看不那个未定义步骤了,取而代之的是一个失败的步骤。这个步骤之所以失败是因为我们还没有创建Article模型。我们将通过RSpec生成器来创建它。

 

 

script/generate rspec_model Article title:string content:text

 译者注:windows用户请用 ruby script/generate xxxxxx

 

That done we’ll run the migrations and clone the schema changes to the test database.

然后我们运行数据迁移,并且把开发数据库结构复制到测试数据库。

 

 

rake db:migrate
rake db:test:clone

 Now we can run Cucumber again.

 

现在我们再次运行Cucumber。

 

 

$ cucumber features -n
Feature: Manage Articles
  In order to make a blog
  As an author
  I want to create and manage articles

  Scenario: Articles List
    Given I have articles titled Pizza, Breadsticks
    When I go to the list of articles
      Can't find mapping from "the list of articles" to a path.
      Now, go and add a mapping in features/support/paths.rb (RuntimeError)
      /Users/eifion/rails/apps_for_asciicasts/blog/features/support/paths.rb:12:in `path_to'
      ./features/step_definitions/webrat_steps.rb:11:in `/^I go to (.+)$/'
      features/manage_articles.feature:8:in `When I go to the list of articles'
    Then I should see "Pizza"
    And I should see "Breadsticks"

1 scenario
1 failed step
2 skipped steps
1 passed step

 We have our first passing step! The second step is now failing, though. This is because Cucumber doesn’t know how to translate “the list of articles”  into a path in our application.

 

我们的第一步通过了测试!但是第二步现在失败了。这是因为Cucumber不知道怎么把 "the list of articles" 对应到我们应用程序的路径。

 

One of the files installed by Cucumber is called paths.rb , which lives in the/features/support  directory. In this file we can define custom paths that map the English definitions in Cucumber files to paths in our Rails application. To add a mapping we just add a new when  condition to the case  statement in the path_to  method. (There’s a comment in the file to show us where to add it.)

 

Cucumber安装的时候在/features/support文件夹里会生成一个叫paths.rb的文件。在这个文件里我们可以自定义Cucumber文件里的英语定义和我们的Rails应用程序路径之间的映射关系。要增加一条映射,我们只需要在path_to 方法里添加一个新的when条件到case语句。(文件里有一个注释来告诉你添加到哪里)

 

 

# Add more page name => path mappings here
when /the list of articles/
  articles_path

 

Running Cucumber again will show the next error that needs fixing. This time it cannot find a route that matches articles_path . We just need to add the mapping to routes.rb .

 

再次运行Cucumber的时候,会显示下一个需要修复的错误。这次他找不到符合articles_path的路由。我们需要把这个映射加入到routes.rb

 

 

map.resources :articles  

 

That done, Cucumber will now tell us that the ArticlesController  is missing. As with the model we’ll use the RSpec generator to create it.

 

完成以后,Cucumber告诉我们找不到ArticlesController。和模型一样,我们使用RSpec生成器来创建这个控制器。

 

 

script/generate rspec_controller articles index
 

Cucumber still isn’t happy though. It expects to see “Pizza” in the output from the view, but as we’ve not done anything to the index view yet it won’t find it. We’ll modify the controller’sindex  action and the view code to show the list of articles.

 

Cucumber仍然不满意。他想在我们的视图里看到"Pizza",但是我们还没在index视图写任何东西所以Cucumber找不到他。我们要修改Articles控制器的index action 和 视图 代码来显示文章列表。

 

 

#The index action in the ArticlesController
def index  
  @articles = Article.all  
end  
 

 

#The view code for the Articles index page.
<h1>Articles</h1>  
<% @articles.each do |article| %>  
  <h2><%= h(article.title) %></h2>  
  <p><%= h(article.content) %></p>  
<% end %>  

 

This time Cucumber is happy and shows four passing steps when we run it.

 

这回Cucumbe高兴啦!当我们再次运行他的时候他显示4个步骤全部通过。

 

It looks like we’ve had to put a lot of effort in to get this one Cucumber scenario to pass, but we have been taking deliberately small steps to show how Cucumber always tells you what you need to do next. As you get more comfortable with Cucumber you can take bigger steps between each Cucumber run.

 

看起来我们不得不为了一个Cucumber场景的通过付出大量努力,事实上我们采用了故意很小的步骤来展示Cucumber通常都告诉你下一步该干什么。当你熟练使用cucumber后,你可以在更大些的步骤完成后运行cucumber。

Another Scenario

一个新的场景

Now that we’ve shown an overview of how Cucumber works we’ll work through another, more complex scenario, but we’ll take bigger steps.

 

现在我们已经展示了cucumber大概是怎样工作的了。我们将继续另外一个更复杂的例子,并且使用更大的步骤。

 

 

Scenario: Create Valid Article
  Given I have no articles
  And I am on the list of articles
  When I follow "New Article"
  And I fill in "Title" with "Spuds"
  And I fill in "Content" with "Delicious potato wedges!"
  And I press "Create"
  Then I should see "New article created."
  And I should see "Spuds"
  And I should see "Delicious potato wedges!"
  And I should have 1 article

 

 

This scenario defines the behaviour for creating a new article but unlike the last one it spans several pages of our application. The scenario assumes that we have no articles to start with, clicks a link with the text “New Article”, fills in a form and submits it then expects to see the new article and find one article in the database.


这个场景定义了新建一个文章的行为,但是和刚才那个不同,他包含了我们应用程序的很多个页面。这个场景假设我们刚开始没有文章,然后点击"New Article"这个文字链接,提填写表单然后提高,然后然后希望看到这篇新文章。并且在这个时候数据库中有一篇文章。

As always our first step is to run Cucumber. When we do it will tell us that we have two undefined steps and six skipped ones. The two undefined steps are the first (“Given I have no articles” ) and the last (“And I should have 1 article” ). The other six are covered by Webrat steps.

和平常一样,我们的第一部是运行Cucumber. 运行后他告诉我们有两个没有定义的步骤和6个跳过的步骤。两个未定义的步骤是第一个("Given I have no articles")和第二个("And I should have 1 article")。 其他6个被包含在Webrat步骤中。

The first thing to do is to create the two undefined steps.

首先我们要创建那两个没定义的步骤.

Given /^I have no articles$/ do
  Article.delete_all
end

Then /^I should have ([0-9]+) article$/ do |count|
  Article.count.should == count.to_i
end
 

The first step is straightforward. To satisfy the condition we just delete all the articles. For the second step we have replaced the number 1  with a regular expression that matches any positive number and passed that number as a variable to the block. The block then checks that the number of articles equals the variable that was passed. Note that Cucumber will treat all variables matched in a regular expression as strings so we have to convert the count to an integer.

 

第一步直截了当写出来。为了满足那个条件,我们只需要删除所有的文章。第二步中,我们把数字1改成了一个可以匹配正数的正则表达式,并且把那个数字当成一个变量传给代码块。然后代码块检查文章的数量是否等于那个传进来的变量。注意Cucumber会把接收到的任何正则表达式当成字符串处理,所以我们需要显式的把count这个变量转换成integer类型。

 

With those done another Cucumber run shows us that the first two steps now pass, but the third one fails.

再次运行Cucumber,显示前面两个步骤现在通过测试,但是第三步失败。

Given I have no articles
And I am on the list of articles
When I follow "New Article"
  Could not find link with text or title or id "New Article" (Webrat::NotFoundError)
  (eval):2:in `click_link'
  ./features/step_definitions/webrat_steps.rb:19:in `/^I follow "([^\"]*)"$/'
  features/manage_articles.feature:15:in `When I follow "New Article"'
 
The step fails because Cucumber could not find a link with the text “New Article”. To make the step pass we’ll need to create link from the index page to the new article page and create the page to create a new article.

这步失败是因为cucumber找不到一个叫"New Artical"的文字链接。我们需要在index页和新文章页之间创建一个链接并且创建这个页面。

On the index page we’ll just add a link to the new article page.

我们在index页上创建一个指向新文章页的链接。

<p><%= link_to "New Article", new_article_path %></p>  
 
And then create the page for the new article with a form on it.

然后在/views/articles/下面的new.html.erb中为新文章创建一个表单。

<% form_for @article do |f| %>  
  <ol class="formList">  
    <li>  
      <%= f.label :title %>  
      <%= f.text_field :title %>  
    </li>  
    <li>  
      <%= f.label :content %>  
      <%= f.text_field :content %>  
    </li>  
    <li>  
      <%= f.submit "Create" %>  
    </li>  
  </ol>  
<% end %>  
 
Finally we’ll set up the controllers.

最后我们创建控制器

def new  
  @article = Article.new  
end  
    
def create  
  @article = Article.create!(params[:article])  
  flash[:notice] = "New article created."  
  redirect_to articles_path  
end  
 
We’ve taken quite a big step here and implemented the new  and create  actions for our controller. We’ll give Cucumber another run through the scenario and see what needs to be done next. This time the first six steps pass, but then we get this failing step.

我们在为控制器编写new和create action的时候迈了一大步。我们来再次用cucumber运行这个场景,然后看还有啥要做的。这回前面6个步骤都通过了,然后就遇到这个失败的步骤。

Then I should see "New article created."
  expected the following element's content to include "New article created.":
  ArticlesSpudsDelicious potato wedges!New Article (Spec::Expectations::ExpectationNotMetError)
  ./features/step_definitions/webrat_steps.rb:94:in `/^I should see "([^\"]*)"$/'
  features/manage_articles.feature:19:in `Then I should see "New article created."'
 
Although we’ve created the flash message in the create  action we’re not displaying it on the index page. Adding it in should make this step pass. It should go in a layout file, but we’ll just add it to the top of the index page to make the step pass.

虽然我们在create action种创建了flash message,但是我们并没有在index页面里显示出来。加上它应该就可以让这步测试通过。它应当放在模板文件里,但是由于我们的目标只是让这步测试通过,所以我们放在index页面里。

<%= flash[:notice] %>  
 

Everything’s Gone Green

全部变绿啦

With the flash message added to the index page when we run Cucumber again all of the steps pass.

 

我们加上flash message后,再次运行cucumber,所有的步骤都绿了。

 

 

$ cucumber features -n
Feature: Manage Articles
	In order to make a blog
	As an author
	I want to create and manage articles

  Scenario: Articles List
    Given I have articles titled Pizza, Breadsticks
    When I go to the list of articles
    Then I should see "Pizza"
    And I should see "Breadsticks"

  Scenario: Create Valid Article
    Given I have no articles
    And I am on the list of articles
    When I follow "New Article"
    And I fill in "Title" with "Spuds"
    And I fill in "Content" with "Delicious potato wedges!"
    And I press "Create"
    Then I should see "New article created."
    And I should see "Spuds"
    And I should see "Delicious potato wedges!"
    And I should have 1 article

2 scenarios
14 passed steps

 

Our application now behaves as we want it to. With the scenario passing the next steps would be to tidy up the code, check that everything still passes and then write the next scenario, say one for an invalid article.

 

现在我们的应用程序的行为和我们设计的一样了。随着这个场景接下来的步骤的通过,我们开始一个新的场景,觉一篇没有通过的文章。

 

We’ve only really covered the basics of Cucumber here. It might seem like quite a lot of work to define the tests in English and implement each step one at a time, but Cucumber testing has several advantages. With it you’re testing your whole stack, so you’re writing tests that cover everything from the user interface down to the database. While they’re not a replacement for unit tests, they provide an excellent way of writing high-level tests to test the overall behaviour of your application.

 

在这里我们已经介绍了cucumber的基础部份。它看起来需要很多的工作来用英语定义测试,并且一次一次的完成每个步骤。但是cucumber测试有很多有点。你用它测试你的真个堆栈,所以你写的测试包括了从用户界面到数据库的一切。他不是单元测试的替代物,他提供了一个写测试你的应用程序的整体行为好方法。

 

 

你可能感兴趣的:(正则表达式,Flash,Ruby,Rails,rspec)