How to Test Controller Concerns in Rails 4

Concerns are a new feature that was added in Rails 4. They allow to clean up code in your models and controllers. They also allow you to share functionality between models or controllers. However, they can be a bit tricky to test in isolation. In this article I want to show how you can test your controller concerns in isolation.
The Over Simplified Scenario
We have a project with several different types of objects that can be sold. Each item is unique and is marked as ‘out of stock’ once it is purchased. However, we have several different controllers and different types of purchases that need this functionality. In order to reduce code duplication, we are going to put these in a concern.
/app/controllers/concerns/transaction_processing.rb
|
If we want to test this concern, we need a controller to include it in. However, it would not be accurately unit testing to do this as there could be code in that controller that could affect the output of our test. Inside of our test, we can create a fake controller with no methods or logic of it’s own, and then write tests for that. If you are using RSpec , you can call methods directly using the subject object. Here is my example test using RSpec and FactoryGirl
/spec/controllers/concerns/transaction_processing_spec.rb
|
And there you go! Easy, isolated tests for your controller concerns.
How to Test Controller Concerns in Rails 4的更多相关文章
- javascript实现select菜单/级联菜单(用Rails.ajax实现发送请求,接收响应)
在购物网站,填写收货地址的时候,会出现XX省XX市XX区的下拉菜单,如何实现此功能?思路是什么? 功能设置: 当选择省select菜单后,市的select菜单为这个省的城市列. 当选择市菜单后,区菜单 ...
- Ruby on Rails 初次冲浪体验
为了更好的阅读体验,欢迎訪问 作者博客原文 Rails is a web application development framework written in the Ruby language. ...
- Rails generate的时候不生成assets和test
我们在执行rails g controller controller_name或者rails g model model_name的时候往往会生成相应的assets文件和test,怎么不让rails帮 ...
- ruby on rails笔记
一.新建rails项目步骤: 1.生成新项目 rails new demo cd demo vi Gemfile 末尾end前增加 gem 'execjs' gem 'therubyracer ...
- ASP.NET MVC与RAILS3的比较
进入后Web年代之后,MVC框架进入了快速演化的时代,Struts等垂垂老矣的老一代MVC框架因为开发效率低下而逐渐被抛弃,新一代的MVC则高举敏捷的大旗,逐渐占领市场,其中的代表有Rails (ru ...
- Ruby系列教程(附ruby电子书下载)【转】
摘要:http://www.cnblogs.com/dahuzizyd/category/97947.html 关键字:Ruby On Rails ,InstantRails,Windows,入门,教 ...
- rails 修改数据库之后注意修改controller
rails 修改数据库之后注意修改controller 在view中进行修改之后,注意修改controller中的内容: 这样才可以进行参数的传递:
- rails 创建项目、创建controller、model等
rails2之前创建新项目: rails3以及更高版本创建新项目:rails new webname 创建数据表model:rails g model user name:string sex:str ...
- rails ajax上传文件以及controller处理
ajax提交文件 var formData = new FormData(); formData.append('file', $('input[name="file"]')[0] ...
随机推荐
- linux学习4 网络命令和关机重启
网络命令 write write 用户名 回车后输入信息 以 ctrl+d 保存结束 wall wall [message] //that is all,发送给所有人 ping ping 选项 IP地 ...
- 使用web图标
http://www.lovelucy.info/demo/twitter-bootstrap-custom-icons/
- node中global
global:表示Node所在的全局环境,类似于浏览器的window对象.需要注意的是,如果在浏览器中声明一个全局变量,实际上是声明了一个全局对象的属性,比如var x = 1等同于设置window. ...
- nullcon HackIM 2016 -- Crypto Question 1
You are in this GAME. A critical mission, and you are surrounded by the beauties, ready to shed thei ...
- mysql查询表的数据大小
在需要备份数据库里面的数据时,我们需要知道数据库占用了多少磁盘大小,可以通过一些sql语句查询到整个数据库的容量,也可以单独查看表所占容量. 1.要查询表所占的容量,就是把表的数据和索引加起来就可以了 ...
- 调用JavaScript
当webdriver 遇到没法完成的操作时,笔者可以考虑借用JavaScript 来完成,比下下面的例子,通过JavaScript 来隐藏页面上的元素.除了完成webdriver 无法完成的操作,如果 ...
- Git学习(一)——Git介绍与安装
一.Git诞生 Linus在1991年创建了Linux,从此,Linux系统不断发展,成为最大的服务器系统软件. 2005年,Linus用C编写了一个分布式版本控制工具--Git. 二.集中式vs分布 ...
- Oracle重启 error: ora-01034:oracle not available ora-27101:shared memory realm does not exist
error: ora-01034:oracle not available ora-27101:shared memory realm does not exist 苦咖啡 他的博客中一篇文章完美的解 ...
- VS2010+C#+在新建类或接口时在文件开头自动生成作者和日期等备注
今天在公司项目准备开始,为达到项目的规范管理,要求每个文件的标准日期,作者等信息,搜集了百度的资料,新建文件时效果如下: 其实原理很简单,只要修改VS,IDE文件下的类(或接口)模版代码 文件路径:C ...
- 百度自动发贴,登录很顺利的模拟实现,但发贴攻关失败,能力有限,追JS过程中颇为痛苦
攻关失败,且短期内看不到希望,看不到方向,且越来越焦急,目前已知的是,用根据用户的鼠标事件以一定的规则结合其他数据,服务器以这些数据验证是否为真正的手动发贴. 不过闲暇时实现了百度贴吧的自动签到. 较 ...