What is the real difference between shared_examples and shared_context ?

My observations :

  1. I can test same things using both (i.e. with shared_examples or shared_context)

  2. But some of my other tests fails if I use later one.

Observation #1 :

I compared shared_examples and shared_context per documentation onhttps://www.relishapp.com/

Syntactical differences are :

  • shared_context to define a block that will be evaluated in the context of example groups by implicitly matching metadata

Example :

shared_context "shared stuff", :a => :b do
...
end
  • The way these are included or called from a test file

shared_examples

include_examples "name"      # include the examples in the current context
it_behaves_like "name" # include the examples in a nested context
it_should_behave_like "name" # include the examples in a nested context

shared_context

include_context "shared stuff"

Observation #2

I have a test case

shared_context 'limit_articles' do |factory_name|
before do
@account = create(:account)
end it 'should restrict 3rd article' do
create_list(factory_name, 3, account: @account) article4 = build(factory_name, account: @account)
article4.should be_invalid
end it 'should allow 1st article' do
...
end it 'should allow 2nd article' do
...
end
end

And include the context in a spec file which already has one shared_context included, then the existing one fails. But I change the order then all my test passes

Fails

include_context 'existing_shared_context'

include_context 'limit_articles'

Also if I replace the shared_context with shared_examples and accordingly include it in test case.

Passes

include_context 'existing_shared_context'

it_behaves_like 'limit_articles'

原文:http://stackoverflow.com/questions/21117123/rspec-shared-examples-vs-shared-context

rspec中的shared_examples与shared_context有什么不同的更多相关文章

  1. rspec中的let和let!区别

    文档 https://relishapp.com/rspec/rspec-core/v/2-5/docs/helper-methods/let-and-let 从上面文档中得出 let 1 只会在一个 ...

  2. Rspec中describe和context不同

    转自  http://lmws.net/describe-vs-context-in-rspec 学习rspec,不太理解describe和context.google了一下,找到这篇文章,感觉说的有 ...

  3. Rails中的测试RSpec升级遇到的问题

    bundle exec rspec spec/ /home/wuxj/Prac/rrprac/sample_app/spec/spec_helper.rb::in `block in <top ...

  4. rspec的一些常见用法

    这里讲了如何安装rspec,安装使用rspec. 下面介绍一下rspec中常见的使用方法. 下面是一个最简单的测试用例,判断true是不是等于true,should_be是旧的用法,新用法推荐使用ex ...

  5. Rspec: feature spec 功能测试 测试JavaScript.

    我们要把应用各组件放在一起做集成 测试,这样才能保证模型和控制器之间能够良好契合. 在 RSpec 中,这种测试称为功能测试(feature spec),有时也称为验收测试(acceptance te ...

  6. Ruby之Rspec的报错解决

    #enconding:utf-8 require 'selenium-webdriver' require 'rspec' describe "baidu main page" d ...

  7. pundit

    gem "pundit" Include Pundit in your application controller: class ApplicationController &l ...

  8. 有意练习--Rails RESTful(一)

    书要反复提及<哪里有天才>在说,大多数所谓的天才是通过反复刻意练习获得. 当你的练习时间达到10000几个小时后,.你将成为该领域的专家. 近期在学习rails怎样实现RESTful We ...

  9. BDD

    Binding business requirements to .NET code http://www.specflow.org/ 行为驱动开发 BDD:Behavior Driven Devel ...

随机推荐

  1. 前端mac下的工具

    1.制作base64图片的 DataURLMaker imageAlpha 减少png图片尺寸 2.共享iphone X-Mirage 3.制作交互图 briefs 4.制作矢量图 sketch

  2. poj1050

    To the Max Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39081   Accepted: 20639 Desc ...

  3. Graded Browser Support

    ( The YUI Target Environments Matrix is here) About the Browser Test Baseline and Operating Systems ...

  4. 将复杂查询写到SQL配置文件--SOD框架的SQL-MAP技术简介

    引言 今天看到一片热门的博客, .NET高级工程师面试题之SQL篇 ,要求找出每一个系的最高分,并且按系编号,学生编号升序排列.这个查询比较复杂,也比较典型,自从用了ORM后,很久没有写过SQL语句了 ...

  5. 使用FreePic2Pdf导出书签至Word建立层级目录——快速初始化Word笔记本目录

    使用FreePic2Pdf导出书签至Word建立层级目录 --快速初始化Word笔记本目录 文:安徽师范大学2014级计算机科学与技术 王昊 (Get Contact:441301158@qq.com ...

  6. #define 小知识

    #define N 15 #define M 2+N #define W 2*M 问3*W的结果是多少? 结果为27: 知识点:define只能进行非常简单的运算,简单来说就是仅仅是将自身的值代入,而 ...

  7. iOS 开发 常用的正则验证表达式:电话 、邮箱等等

    #pragma mark - 验证手机号 +(BOOL)checkForMobilePhoneNo:(NSString *)mobilePhone{ NSString *regEx = @" ...

  8. DEV提示控件ToolTipControl

    1.设置边框的颜色和显示箭头图标:在构造函数里面设置 public frmLogin()        {            InitializeComponent(); //设置边框颜色     ...

  9. PPTP VPN 限制一个账号只允许一个用户来登录

    创建auth-up文件 vi /etc/ppp/auth-up chmod a+x /etc/ppp/auth-up auth-up脚本内容如下 #!/bin/sh # get the usernam ...

  10. linux 搭建svn

    1          安装SVN 官网下载:http://subversion.apache.org/packages.html SVN客户端:TortoiseSVN,官网下载:http://tort ...