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. web初学之重定向与请求转发

    重定向与请求转发的问题 (1)RequestDispatcher是通过调用HttpServletRequest对象的getRequestDispatcher()方法得到的,是属于请求对象的方法. (2 ...

  2. Bash Shell的操作环境

    1.路径与命令查找顺序 基本上,命令运行的顺序可以这样看: 1)以相对/绝对路径执行命令,例如“/bin/ls”或“./ls”; 2)由alias找到该命令来执行; 3)由bash内置的(builti ...

  3. Time.MONTH及Calendar.MONTH 默认的月份为 0-11

    Time.MONTH及Calendar.MONTH 默认的月份为  0-11 所以使用的时候要自己加1.

  4. Flex 对象克隆

    package widget.EnvPlot{ public class copyObject extends Object    {        public function copyObjec ...

  5. 遭遇flash播放mp3诡异问题

    在部分ie10+flash player 播放mp3,播放第二句话时,中断无法正常播放,(客户的机器上),自己公司的机器测试了几个,都没发现这个问题.其它浏览器(chrome,firefox)也没发现 ...

  6. 查看linux版本

    http://nameyjj.blog.51cto.com/788669/557424 1. 查看内核版本命令: 1) [root@q1test01 ~]# cat /proc/version     ...

  7. 抓取百度音乐频道歌曲url

    参考了 http://blog.csdn.net/banguijun/article/details/11815263 后写了一个抓取fm音乐url的模块,得到所有频道歌曲的url.缺点是百度会变更u ...

  8. 写了好多次SSH现在对于框架还是有一定的基础了,但是对于框架下我们该如何进行操作呢???

    首先,对于一个老手来说,我们最快捷的就是ctrl+c和ctrl+v,但是我们自己应该复制哪一些代码呢? 1.在我们导完包之后,我们需要写的就是web.xml,在其中,我们要有过滤器及映射和监听器. w ...

  9. android 设置状态栏与标题背景颜色一致

    必须在Android4.4以上版本才能设置状态栏颜色: 一.在单个Activity里面,设置状态栏的背景: 效果: 1.在Activity的布局根文件中添加属性: android:fitsSystem ...

  10. 关于ajax载入窗口使用RedirectToAction在窗口显示的问题

    在过滤器中过滤用户是否登录,没有登录就RedirectToAction("Login", "Auth", new { Area = "Account& ...