It’s pretty common to have multiple tests that are nearly the same.

In one app, we support bidding on both online and hammer auctions (auctions with people physically present). They’re separate controllers but with a lot of shared code and behavior.

We want to test both, but we’d rather not write two almost identical tests if we can help it.

So we’ve been using RSpec shared examples, with the template method patternto account for the differences, and we like it.

Here’s a simplified example:

spec/request/online_bidding_spec.rb

1
2
3
4
5
6
7
8
9
10
11
12
require "spec_helper"
require "support/shared_examples/bidding" describe "Bidding online" do
include_examples :bidding let(:auction) { FactoryGirl.create(:online_auction) } def auction_path
online_auction_path(auction)
end
end

spec/request/hammer_bidding_spec.rb

1
2
3
4
5
6
7
8
9
10
11
12
require "spec_helper"
require "support/shared_examples/bidding" describe "Bidding at hammer auction" do
include_examples :bidding let(:auction) { FactoryGirl.create(:hammer_auction) } def auction_path
hammer_auction_path(auction)
end
end

spec/support/shared_examples/bidding.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
shared_examples :bidding do
it "lets you bid when logged in" do
log_in # Implemented somewhere else.
visit auction_path
place_bid
bid_should_be_accepted
end it "doesn't let you bid when not logged in" do
visit item_path
place_bid
bid_should_be_rejected
end def auction_path
raise "Implement me."
end def place_bid
fill_in "Bid", with: 123
click_button "Place bid"
end def bid_should_be_accepted
page.should have_content("OK! :)")
end def bid_should_be_rejected
page.should have_content("NO! :(")
end
end

The only template method here is auction_path . The shared example makes sure, by raising, that it’s overridden in your concrete specs.

RSpec shared examples with template methods的更多相关文章

  1. Vue学习笔记 template methods,filters,ChromeDriver,安装sass

    ChromeDriver installation failed Error with http(s) request: Error: connect ETIMEDOUT 172.217.160.80 ...

  2. HeadFirst设计模式之模板方法模式

    一. 1.The Template Method defines the steps of an algorithm and allows subclasses to provide the impl ...

  3. Quality in the Test Automation Review Process and Design Review Template

    About this document Prerequisite knowledge/experience: Software Testing, Test Automation Applicable ...

  4. ruby Methods, Procs, Lambdas, and Closures

    define simple method定义简单方法 关键字def用于方法定义,在其后是方法名和可选的参数名列表,参数名列表会用一对圆括号括住.构成方法主体的代码放在参数列表之后,end用于结束方法定 ...

  5. 【转】ruby rake执行rspec

    RSpec 是Ruby的一个行为驱动开发(BDD)工具,当前的版本是 2.10.根据其入门文档,安装好之后,可以使用 rspec 命令来运行“测试”.但在某些情况下,如果参数较多,使用该命令并不方便: ...

  6. ES8新特性——ES8 was Released and here are its Main New Features

    原文: https://hackernoon.com/es8-was-released-and-here-are-its-main-new-features-ee9c394adf66 -------- ...

  7. SWIG 3 中文手册——6. SWIG 和 C++

    目录 6 SWIG 和 C++ 6.1 关于包装 C++ 6.2 方法 6.3 支持的 C++ 功能 6.4 命令行选项与编译 6.5.1 代理类的构造 6.5.2 代理类中的资源管理 6.5.3 语 ...

  8. Java Synchronized Blocks

    From http://tutorials.jenkov.com/java-concurrency/synchronized.html By Jakob Jenkov   A Java synchro ...

  9. Sharepoint CAML 增删改查 List

    Lists.UpdateListItems 方法 (websvcLists) Windows SharePoint Services 3   Adds, deletes, or updates the ...

随机推荐

  1. 第一二九天上课 PHP 自制简单开发模板

    构建基础架构 在项目文件夹(自定义)下创建 (1)核心目录:WQ (2)模板目录:MoBan (3)编译目录:BianYi (4)创建配置文件: config.ini.php <?php //获 ...

  2. Nginx限制访问速率和最大并发连接数模块--limit (防止DDOS攻击)

    Tengine版本采用http_limit_req_module进行限制 具体连接请参考 http://tengine.taobao.org/document_cn/http_limit_req_cn ...

  3. Python 2.7_发送简书关注的专题作者最新一篇文章及连接到邮件_20161218

    最近看简书文章关注了几个专题作者,写的文章都不错,对爬虫和数据分析都写的挺好,因此想到能不能获取最新的文章推送到Ipad网易邮箱大师.邮件发送代码封装成一个函数,从廖雪峰大神那里学的  http:// ...

  4. 《全唐诗》的sqlite3数据库

    下载地址: http://pan.baidu.com/s/1b2mE54quantangshi.db是sqlite3数据库,包括2张表.index表:volume 列(整数,主键) 表示卷号,从1到9 ...

  5. HTTPS 和 HTTP

    https://www.zhihu.com/question/52790301

  6. Java入门记(四):容器关系的梳理(上)——Collection

    目录 一.Collection及子类/接口容器继承关系 二.List 2.1 ArrayList 2.1.1 序列化的探讨 2.1.2 删除元素 2.1.3 调整大小 2.2 Vector和Stack ...

  7. Qt链接网站SLOT

    void BottomToolWidget::openLink() { QString link = "http://www.baidu.com";//在.h中添加slot声明,在 ...

  8. DataRow[]与DataTable的转换代码【精炼】

    Day_20170106 代码记录 //DataTable查询出DataRow[] DataRow[] drs = AllSysModuleDs.Tables[].Select(string.Form ...

  9. jquery EasyUI datagrid 的扩展

    接触 easyui 半年,js学的不深.大神请路过. 直接扩展 添加方法: $.extend($.fn.datagrid.methods, { getSelectedIndex: function ( ...

  10. mvc 项目下 webservice 程序无法运行

    错误描述: 可以出现调用HelloWorld的界面 点击调用按钮报无法找到该资源 错误分析: 把webservice当成controller了. 解决: global中 添加  routes.Igno ...