The RSpec Book笔记《三》Automating Features with Cucumber使用cucumber自动完成features

简介:

Automating Features with cucumber

cucumber

 

 
  1. Given /^I am not yet playing$/ do 
  2.   pending # express the regexp above with the code you wish you had 
  3. end 


这就是一个cucumber的步骤定义。你可以把它想成是一个方法,然后在definition中定义一个这样的方法。

 

 
  1. mkdir features/step_definitions 
  2.  
  3. vi features/step_definitions/codebreaker_steps.rb 



把上面的内容拷贝进去,并且删除pending一行。

 

 
  1. Given /^I am not yet playing$/ do 
  2.   
  3. end 



执行下面的命令

 

 
  1. cucumber features/codebreaker_start_game.feature 
 
  1. Feature: code-breaker starts game 
  2.   
  3.   As a code-breaker 
  4.   I want to start a game 
  5.   So that I can break the code 
  6.  
  7.   Scenario: start game                            # features/codebreaker_start_game.feature:7 
  8.     Given I am not yet playing                    # features/step_definitions/codebreaker_steps.rb:1 
  9.     When I start a new game                       # features/codebreaker_start_game.feature:9 
  10.     Then I should see "Welcome to Codebreaker!"   # features/codebreaker_start_game.feature:10 
  11.     And I should see "Enter guess:"               # features/codebreaker_start_game.feature:11 
  12.     But I should not see "What is your question?" # features/codebreaker_start_game.feature:12 
  13.  
  14. 1 scenario (1 undefined) 
  15. 5 steps (4 undefined, 1 passed) 
  16. 0m0.005s 
  17.  
  18. You can implement step definitions for undefined steps with these snippets: 
  19.  
  20. When /^I start a new game$/ do 
  21.   pending # express the regexp above with the code you wish you had 
  22. end 
  23.  
  24. Then /^I should see "(.*?)"$/ do |arg1| 
  25.   pending # express the regexp above with the code you wish you had 
  26. end 
  27.  
  28. Then /^I should not see "(.*?)"$/ do |arg1| 
  29.   pending # express the regexp above with the code you wish you had 
  30. end 


结果告诉我们有一个scenario,五个步骤,四个还没有定义,一个已经通过。

cucumber命令的参数是features/codebreaker_start_game.feature,在cucumber启动的时候会加载目录下所有的.rb文件,已经子目录下面的.rb文件,包括前面的features/step_definitions/codebreaker_steps.rb。

我们通过调用cucumber提供的五个方法来定义feature中的步骤:Given(), When(), Then(), And(), But()。And()和But()会呈现跟在前面的Given, When, Then的意思。在上面的例子中,scenario中最后两行的And和But被当做是Then来处理。

每个方法都通过正则表达来匹配。

Given方法对应的正则表达式是:/^I am not yet playing$/。就会匹配feature中的Given部分。

When方法中需要我们创建一个新游戏,然后开始这个游戏。

 

 
  1. When /^I start a new game$/ do 
  2.   Codebreaker::Game.new.start 
  3. end 



这时候,我们的应用还没有任何代码,我们只是按照我们想要的方式来写。我们希望简单,尽可能的简单。
The code you wish you had.

再次运行
 

 
  1. cucumber features/codebreaker_start_game.feature  


你会发现一些红色的信息
uninitialized constant Codebreaker (NameError)

因为你还没有定义Codebreaker以及Game以及start方法。好吧,我们来定义一下。
 

 
  1. mkdir -p lib/codebreaker/ 
  2. vi lib/codebreaker/game.rb 



输入下面的内容

 
  1. module Codebreaker 
  2.   class Game 
  3.     def start 
  4.  
  5.     end 
  6.   end 
  7. end 



再次执行
 

 
  1. cucumber features/codebreaker_start_game.feature  



还是出现之前的红色信息。

这是因为按照约定,在lib目录需要有一个以顶级module名称命名的文件。

 

 
  1. vi lib/codebreaker.rb 



写入下面的内容
 

 
  1. require 'codebreaker/game' 



 

 
  1. vi features/support/env.rb  



写入下面的内容
 

 
  1. $LOAD_PATH << File.expand_path('../../../lib', __FILE__) 
  2. require 'codebreaker' 


再次执行
 

 
  1. cucumber features/codebreaker_start_game.feature  



看到的内容又变绿色了。

在完成第二个步骤之后,我们来看看Then这个步骤。

在这个步骤中,我们希望在console中看到Welcome to Codebreaker!。这意味着我们需要一个工具,来捕获Game发送到STDOUT的信息。

 

 
  1. vi features/step_definitions/codebreaker_steps.rb 



加入下面的内容
 

 
  1. Then /^I should see "(.*?)"$/ do |message| 
  2.   output.messages.should include(message) 
  3. end 



修改When的内容
 

 
  1. When /^I start a new game$/ do 
  2.   game=Codebreaker::Game.new(output) 
  3.   game.start 
  4. end 



增加下面的内容
 

 
  1. class Output 
  2.   def messages 
  3.     @messages ||= [] 
  4.   end 
  5.  
  6.   def puts(message) 
  7.     messages << message 
  8.   end 
  9. end 
  10.  
  11. def output 
  12.   @output ||= Output.new 
  13. end 



再次执行
 

 
  1. cucumber features/codebreaker_start_game.feature  


提示我们wrong number of arguments(1 for 0)(ArgumentError)

是因为我没有定义带有参数的initialize。

 

 
  1. vi lib/codebreaker/game.rb  



用下面的内容替换

 

 
  1. module Codebreaker 
  2.   class Game 
  3.     def initialize(output) 
  4.  
  5.     end 
  6.  
  7.     def start 
  8.  
  9.     end 
  10.   end 
  11. end 



再次执行
 

 
  1. cucumber features/codebreaker_start_game.feature  



这时候提示我们有逻辑错误,因为我们的game还没有完成。我们会在后面完成。

总结

我们先在.feature文件中写一个feature的scenario,已经scenario的steps,然后在step_definitions中定义Given,When,Then,然后在通过测试cucumber .feature文件来驱动我们编写实现代码。

当目前为止,我们已经学会使用cucumber从外部描述一件事。在接下来的章节中,我们将会进行从外到里的工作方式,使用RSpec来驱动单个对象的外部行为。




本文转自 virusswb 51CTO博客,原文链接:http://blog.51cto.com/virusswb/1060620,如需转载请自行联系原作者
目录
相关文章
|
4天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
15天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1309 5
|
1天前
|
监控 JavaScript Java
基于大模型技术的反欺诈知识问答系统
随着互联网与金融科技发展,网络欺诈频发,构建高效反欺诈平台成为迫切需求。本文基于Java、Vue.js、Spring Boot与MySQL技术,设计实现集欺诈识别、宣传教育、用户互动于一体的反欺诈系统,提升公众防范意识,助力企业合规与用户权益保护。
|
14天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1343 87
|
1天前
|
JavaScript Java 大数据
基于JavaWeb的销售管理系统设计系统
本系统基于Java、MySQL、Spring Boot与Vue.js技术,构建高效、可扩展的销售管理平台,实现客户、订单、数据可视化等全流程自动化管理,提升企业运营效率与决策能力。
|
3天前
|
弹性计算 安全 数据安全/隐私保护
2025年阿里云域名备案流程(新手图文详细流程)
本文图文详解阿里云账号注册、服务器租赁、域名购买及备案全流程,涵盖企业实名认证、信息模板创建、域名备案提交与管局审核等关键步骤,助您快速完成网站上线前的准备工作。
189 82
2025年阿里云域名备案流程(新手图文详细流程)