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. Abstract Factory Step by Step --- 抽象工厂

    抽象工厂是创建型模式的代表,其他的还有单件(Singleton).生成器(Builder).工厂方法(Factory Method)以及原型(Prototype),模式本身没有好坏之分,只有适用不适用 ...

  2. Java入门记(二):向上转型与向下转型

    在对Java学习的过程中,对于转型这种操作比较迷茫,特总结出了此文.例子参考了<Java编程思想>. 目录 几个同义词 向上转型与向下转型 例一:向上转型,调用指定的父类方法 例二:向上转 ...

  3. libsvm 训练后的模型参数讲解(转)

    主要就是讲解利用libsvm-mat工具箱建立分类(回归模型)后,得到的模型model里面参数的意义都是神马?以及如果通过model得到相应模型的表达式,这里主要以分类问题为例子.测试数据使用的是li ...

  4. What is “:-!!” in C code?

    stackoverflow上看到的这个问题,觉得挺有趣,顺手记下来. 楼主提问: I bumped into this strange macro code in /usr/include/linux ...

  5. [Docker] docker 基础学习笔记6(共6篇)

    这个迁移指的是docker整个系统的迁移,而不是说images存储位置迁移 docker info 查看docker的各项配置   首先做文件的迁移 使用rsync软件将docker的文件夹整体迁移到 ...

  6. Node.js intro

    1. require() load module http://stackoverflow.com/questions/9901082/what-is-this-javascript-require ...

  7. 在iis中设置文件下载而不是在浏览器上打开

    点击网页链接的*.txt,*.jpg,*.xml等文件时会在浏览器上直接显示,并没有像*.doc那样弹出下载提示框. 解决方法: 在部署的网站上,选择存放文件的目录,选择 HTTP响应标头 ,添加一个 ...

  8. AWS Redshift summary

    https://blogs.aws.amazon.com/bigdata/post/Tx31034QG0G3ED1/Top-10-Performance-Tuning-Techniques-for-A ...

  9. 1 Two Sum

    // Java public int[] twoSum(int[] nums, int target) { int[] answer = new int[2]; for (int i = 0; i & ...

  10. Fiddler抓包之IOS

    Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有的"进出"Fiddler的数据(指cookie,html,j ...