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. Python: 字典的基本操作

    字典是Python里唯一的映射类型.字典是可变的.无序的.大小可变的键值映射,有时候也称为散列表或关联数组. 例子在下面: dic = {"apple":2, "oran ...

  2. 微软ASP.NET技术“乱谈”

    微软ASP.NET技术“乱谈” 2014新年了,顺手写的一点文字,主要谈谈我对当前微软ASP.NET技术的看法,比较随意,大伙儿随便看看吧. 1 当前微软Web平台技术全貌 从2002年发布.NET ...

  3. mysql计算时间差函数

    MySql计算两个日期的时间差函数TIMESTAMPDIFF用法,只要用一句SQL语句就可以办到了. MySql计算两个日期的时间差函数TIMESTAMPDIFF用法: 语法: TIMESTAMPDI ...

  4. eclipse:不能在tomcat里添加一个项目的解决方法

    Cannot add a project to a tomcat server in eclipse You didn't create your project as "Dynamic W ...

  5. cf 730i

    题意:有n个人,每个人有两个能力值,选a个人用它的第一个能力值,b个人用它的第二个能力值,每个人只能选一次,求一个方案使得能力值之和最大,并输出选择方案. 题解:最小费用最大流,原点1向n个人每个人i ...

  6. app跳转openURL,兼容方法

    - (void)openScheme:(NSString *)scheme {   UIApplication *application = [UIApplication sharedApplicat ...

  7. nodejs学习笔记(一)

    终于开始学NodeJs了 说说遇到的问题吧 1.安装express npm install -g express(按道理说,这个全局安装是没问题的,但他就是出问题了) 解决方案: 来源:http:// ...

  8. centos 6.5 查看、开启,关闭 端口

    查看所有端口   netstat -ntlp   1.开启端口(以80端口为例)         方法一:            /sbin/iptables -I INPUT -p tcp --dp ...

  9. robotframework ride安装

    之前在python3.3.5的环境下一直无法找到匹配的wxPython版本,只能再装了一个python2.7,后面在2.7的环境下重新安装了robotframework和ride,结果还是无法启动ri ...

  10. oracle 数据库学习3 --oracle 数据库中常使用的函数

    1.字符函数: ASCII(X) -->返回X字符的ASCII码 concat(x,y)-->将y连接到X上输出 select concat(first_name,last_name) f ...