The RSpec Book笔记《三》Automating Features with Cucumber使用cucumber自动完成features
Automating Features with cucumber
cucumber
- Given /^I am not yet playing$/ do
- pending # express the regexp above with the code you wish you had
- end
这就是一个cucumber的步骤定义。你可以把它想成是一个方法,然后在definition中定义一个这样的方法。
- mkdir features/step_definitions
- vi features/step_definitions/codebreaker_steps.rb
把上面的内容拷贝进去,并且删除pending一行。
- Given /^I am not yet playing$/ do
- end
执行下面的命令
- cucumber features/codebreaker_start_game.feature
- Feature: code-breaker starts game
- As a code-breaker
- I want to start a game
- So that I can break the code
- Scenario: start game # features/codebreaker_start_game.feature:7
- Given I am not yet playing # features/step_definitions/codebreaker_steps.rb:1
- When I start a new game # features/codebreaker_start_game.feature:9
- Then I should see "Welcome to Codebreaker!" # features/codebreaker_start_game.feature:10
- And I should see "Enter guess:" # features/codebreaker_start_game.feature:11
- But I should not see "What is your question?" # features/codebreaker_start_game.feature:12
- 1 scenario (1 undefined)
- 5 steps (4 undefined, 1 passed)
- 0m0.005s
- You can implement step definitions for undefined steps with these snippets:
- When /^I start a new game$/ do
- pending # express the regexp above with the code you wish you had
- end
- Then /^I should see "(.*?)"$/ do |arg1|
- pending # express the regexp above with the code you wish you had
- end
- Then /^I should not see "(.*?)"$/ do |arg1|
- pending # express the regexp above with the code you wish you had
- 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方法中需要我们创建一个新游戏,然后开始这个游戏。
- When /^I start a new game$/ do
- Codebreaker::Game.new.start
- end
这时候,我们的应用还没有任何代码,我们只是按照我们想要的方式来写。我们希望简单,尽可能的简单。
The code you wish you had.
再次运行
- cucumber features/codebreaker_start_game.feature
你会发现一些红色的信息
uninitialized constant Codebreaker (NameError)
因为你还没有定义Codebreaker以及Game以及start方法。好吧,我们来定义一下。
- mkdir -p lib/codebreaker/
- vi lib/codebreaker/game.rb
输入下面的内容
- module Codebreaker
- class Game
- def start
- end
- end
- end
再次执行
- cucumber features/codebreaker_start_game.feature
还是出现之前的红色信息。
这是因为按照约定,在lib目录需要有一个以顶级module名称命名的文件。
- vi lib/codebreaker.rb
写入下面的内容
- require 'codebreaker/game'
- vi features/support/env.rb
写入下面的内容
- $LOAD_PATH << File.expand_path('../../../lib', __FILE__)
- require 'codebreaker'
再次执行
- cucumber features/codebreaker_start_game.feature
看到的内容又变绿色了。
在完成第二个步骤之后,我们来看看Then这个步骤。
在这个步骤中,我们希望在console中看到Welcome to Codebreaker!。这意味着我们需要一个工具,来捕获Game发送到STDOUT的信息。
- vi features/step_definitions/codebreaker_steps.rb
加入下面的内容
- Then /^I should see "(.*?)"$/ do |message|
- output.messages.should include(message)
- end
修改When的内容
- When /^I start a new game$/ do
- game=Codebreaker::Game.new(output)
- game.start
- end
增加下面的内容
- class Output
- def messages
- @messages ||= []
- end
- def puts(message)
- messages << message
- end
- end
- def output
- @output ||= Output.new
- end
再次执行
- cucumber features/codebreaker_start_game.feature
提示我们wrong number of arguments(1 for 0)(ArgumentError)
是因为我没有定义带有参数的initialize。
- vi lib/codebreaker/game.rb
用下面的内容替换
- module Codebreaker
- class Game
- def initialize(output)
- end
- def start
- end
- end
- end
再次执行
- cucumber features/codebreaker_start_game.feature
这时候提示我们有逻辑错误,因为我们的game还没有完成。我们会在后面完成。
总结
我们先在.feature文件中写一个feature的scenario,已经scenario的steps,然后在step_definitions中定义Given,When,Then,然后在通过测试cucumber .feature文件来驱动我们编写实现代码。
当目前为止,我们已经学会使用cucumber从外部描述一件事。在接下来的章节中,我们将会进行从外到里的工作方式,使用RSpec来驱动单个对象的外部行为。
The RSpec Book笔记《三》Automating Features with Cucumber使用cucumber自动完成features相关推荐
- The RSpec Book笔记《一》初步认识TDD,BDD,RSpec,Cucumber
TDD(Test-Driven Development)测试驱动开发. 在编写代码之前先编写一个测试,这时候测试是失败的,red,因为还没有写对应的实现代码.然后开始编写实现的代码,然后跑刚才写的测试 ...
- J2EE学习笔记三:EJB基础概念和知识 收藏
J2EE学习笔记三:EJB基础概念和知识 收藏 EJB正是J2EE的旗舰技术,因此俺直接跳到这一章来了,前面的几章都是讲Servlet和JSP以及JDBC的,俺都懂一些.那么EJB和通常我们所说的Ja ...
- 吴恩达机器学习笔记55-异常检测算法的特征选择(Choosing What Features to Use of Anomaly Detection)
吴恩达机器学习笔记55-异常检测算法的特征选择(Choosing What Features to Use of Anomaly Detection) 对于异常检测算法,使用特征是至关重要的,下面谈谈 ...
- tensorflow学习笔记(三十二):conv2d_transpose (解卷积)
tensorflow学习笔记(三十二):conv2d_transpose ("解卷积") deconv解卷积,实际是叫做conv_transpose, conv_transpose ...
- Ethernet/IP 学习笔记三
Ethernet/IP 学习笔记三 原文为硕士论文: 工业以太网Ethernet/IP扫描器的研发 知网网址: http://kns.cnki.net/KCMS/detail/detail.aspx? ...
- 深度学习入门教程UFLDL学习实验笔记三:主成分分析PCA与白化whitening
深度学习入门教程UFLDL学习实验笔记三:主成分分析PCA与白化whitening 主成分分析与白化是在做深度学习训练时最常见的两种预处理的方法,主成分分析是一种我们用的很多的降维的一种手段,通 ...
- AT91RM9200Linux移植笔记(三)-移植Linux kernel 2.6.17
AT91RM9200Linux移植笔记(三)-移植Linux kernel 2.6.17 手上板子原来自带的是2.4.19的内核, 打算移植新的2.6的内核,从网上下了2.6.17的kernel,下载 ...
- iView学习笔记(三):表格搜索,过滤及隐藏列操作
iView学习笔记(三):表格搜索,过滤及隐藏某列操作 1.后端准备工作 环境说明 python版本:3.6.6 Django版本:1.11.8 数据库:MariaDB 5.5.60 新建Django ...
- 吴恩达《机器学习》学习笔记三——多变量线性回归
吴恩达<机器学习>学习笔记三--多变量线性回归 一. 多元线性回归问题介绍 1.一些定义 2.假设函数 二. 多元梯度下降法 1. 梯度下降法实用技巧:特征缩放 2. 梯度下降法的学习率 ...
- mysql数据库权威指南_MySQL_MySQL权威指南读书笔记(三),第二章:MYSQL数据库里面的数 - phpStudy...
MySQL权威指南读书笔记(三) 第二章:MYSQL数据库里面的数据 用想用好MYSQL,就必须透彻理解MYSQL是如何看待和处理数据的.本章主要讨论了两个问题:一是SQL所能处理的数据值的类型:二是 ...
最新文章
- Android FrameWork——Touch事件派发过程详解
- vue项目中使用JSX
- c语言 单词长度统计,编写一个程序,打印输入中单词长度的直方图
- 【人工智能】全网首发!2020年AI、CV、NLP等最全国际会议、顶会时间汇总!!
- oracle备份恢复之rman恢复到异机(二)
- 释放只有声明没有定义的对象,都是耍流氓
- 基于mini2440的两种触屏中断程序(T35)
- 【bug解决】No OpKernel was registered to support Op 'CudnnRNN' with these attrs.
- pycharm 修改darcual(暗黑)主题滚动条颜色
- dnf脚本是php,dnf自动搬砖脚本教程autojs在使用
- 正点原子T100智能焊台体验,顺便咱们来说说它的软件菜单、界面切换如何来实现?
- 老龄化带来新机遇 银发经济实力不容小觑
- MFC双人五子棋(VS2019)
- 计算机与通信学院方阵解说词,方阵解说词
- RxSwift取消定时
- Memcached完整教程
- windows安装证书后无法信任问题解决
- App前端及后端接口,模拟数据及返回值
- 统计学中基础概念说明
- 在线预览office文件