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. hive中rcfile格式(收藏文)

    首先声明,此文是属于纯粹收藏文,感觉讲的很不错. 本文介绍了Facebook公司数据分析系统中的RCFile存储结构,该结构集行存储和列存储的优点于一身,在MapReduce环境下的大规模数据分析中扮 ...

  2. SQL Server 常用命令使用方法

    (1) 数据记录筛选: sql="select * from 数据表 where 字段名=字段值 order by 字段名 [desc]" sql="select * f ...

  3. python的rename原来这么用

    本来想实践应用一下如何批量修改,后来一想怎么那么麻烦,连最基本都都不会,简化到这份上再慢慢复杂之 一开始用help(os.rename)查了该方法的用法, 出来的解释太简单了,以为路径用的是和wind ...

  4. sublime text 3 安装 pakcage control

        安装sublime的pakcage control 参考:  https://packagecontrol.io/installation#st3 ctrl+`   import urllib ...

  5. mongodb遇到的错误

    1. [root@localhost bin]# ./mongod -dbpath /data/db --logpath /usr/local/mongodb/log --13T18:: F CONT ...

  6. expect使用demo

    #!/usr/bin/expect set timeout set ip [lindex $argv ] spawn ssh root@$ip expect { "yes/no" ...

  7. Unity协程截图,WWWForm、WWW配合上传

    先说一下原理.. 截图有两种方法,第一种: Application.CaptureScreenshot(url); 这个API可以截全屏并保存到指定路径 这里我们不采用此方法 下面的代码采用第二种方法 ...

  8. SQL Server 导出数据到 PostgreSQL

    乘着倒数据这会儿,把方法记录一下 需求:因为数据迁移,需要将SQL Server 2012中的数据库导入到PostgreSQL 数据库中 思路:创建一个空的数据库,便于导入数据.下载PostgreSQ ...

  9. table边框单线的实现方法

    1.实现方法一:    <table border="0" cellspacing="1" style="    实现原理:利用table的单元 ...

  10. MVC中实现只有当用户登录成功的时候才等浏览内容,否则跳转到登录页面

    第一步,在登录的时候记录Session //提供Session接口方便后面判断用户登录 Session["UserInfo"] = uinfo;  //uInfo是用户登录Mode ...