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

 
module TransactionProcessing
extend ActiveSupport::Concern included do
helper_method :process_sale
end def process_sale(item)
item.is_in_stock = false
item.save!
end
end

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

 
require 'spec_helper'

class FakesController < ApplicationController
include TransactionProcessing
end describe FakesController do it "should mark an item out of stock" do
item = create(:item, is_in_stock: true)
subject.process_sale(item)
expect(item.is_in_stock).to be false
end
end

And there you go! Easy, isolated tests for your controller concerns.

How to Test Controller Concerns in Rails 4的更多相关文章

  1. javascript实现select菜单/级联菜单(用Rails.ajax实现发送请求,接收响应)

    在购物网站,填写收货地址的时候,会出现XX省XX市XX区的下拉菜单,如何实现此功能?思路是什么? 功能设置: 当选择省select菜单后,市的select菜单为这个省的城市列. 当选择市菜单后,区菜单 ...

  2. Ruby on Rails 初次冲浪体验

    为了更好的阅读体验,欢迎訪问 作者博客原文 Rails is a web application development framework written in the Ruby language. ...

  3. Rails generate的时候不生成assets和test

    我们在执行rails g controller controller_name或者rails g model model_name的时候往往会生成相应的assets文件和test,怎么不让rails帮 ...

  4. ruby on rails笔记

    一.新建rails项目步骤: 1.生成新项目 rails new demo cd demo vi Gemfile 末尾end前增加   gem 'execjs'   gem 'therubyracer ...

  5. ASP.NET MVC与RAILS3的比较

    进入后Web年代之后,MVC框架进入了快速演化的时代,Struts等垂垂老矣的老一代MVC框架因为开发效率低下而逐渐被抛弃,新一代的MVC则高举敏捷的大旗,逐渐占领市场,其中的代表有Rails (ru ...

  6. Ruby系列教程(附ruby电子书下载)【转】

    摘要:http://www.cnblogs.com/dahuzizyd/category/97947.html 关键字:Ruby On Rails ,InstantRails,Windows,入门,教 ...

  7. rails 修改数据库之后注意修改controller

    rails 修改数据库之后注意修改controller 在view中进行修改之后,注意修改controller中的内容: 这样才可以进行参数的传递:

  8. rails 创建项目、创建controller、model等

    rails2之前创建新项目: rails3以及更高版本创建新项目:rails new webname 创建数据表model:rails g model user name:string sex:str ...

  9. rails ajax上传文件以及controller处理

    ajax提交文件 var formData = new FormData(); formData.append('file', $('input[name="file"]')[0] ...

随机推荐

  1. ORACLE 自治事物

    ORACLE 自治事物 一.问题 (1)现象   一个表A,存在一个触发器,该触发器用来统计表A的数量,并将结果更新到B表.此时,java代码里面调用insert into select 语句,会发生 ...

  2. javaScript 正则表达式匹配日期

    // yyyyMMddhhmmss var pattern = /^(?:(?!0000)[0-9]{4}(?:(?:0[1-9]|1[0-2])(?:0[1-9]|1[0-9]|2[0-8])|(? ...

  3. 《IT蓝豹》完整阅读软件客户端app

    完整阅读软件客户端app,本项目里面还有扫描功能,搜索本地书籍,不过扫码功能通过c++层实现的. 本项目来自:https://github.com/JayFang1993/ScanBook Captu ...

  4. 抢凳子日数据sql

    --先找到档期select * from hv_item_info a where a.report_begdate > '2016-07-28' and a.report_begdate &l ...

  5. XdbxAnalysis

    Tree: TXdbxAnalysis; FDataStream: TMemoryStream; {FDataStream:= TMemoryStream.Create;    FDataStream ...

  6. modelsim无法识别include文件的解决方法

    modelsim发现include关联的文件编译报语法错误,文件名需要写绝对路径,即使这个文件和工程其它文件在一个目录上.例如只写成 `include "c0_ddr3_model_para ...

  7. shell编程之运算符

    declare声明变量类型 declare    [+ / -] [选项]  变量名 - :给变量设定类型属性 + :取消变量的类型属性 -a :将变量声明为数组型 -i :将变量声明为整数型 -x ...

  8. [题解]洛谷月赛 Hello World(升级版)

    题目背景 T1答案要mod1000000007(10^9+7),请重新提交,非常抱歉! 一天,智障的pipapi正在看某辣鸡讲义学程序设计. 题目描述 在讲义的某一面,他看见了一篇文章.这篇文章由英文 ...

  9. js 遇到问题

    1)obj.style.attr 和obj.style[attr]区别: 2)window.onload一个页面只能出现一次: 3)border-radious实现 实心和空心圆 要点:宽度高度一样大 ...

  10. NHibernate 映射失败 is not mapped

    1 区分大小写(实体类名) 2 MAP的XML设置为嵌入的资源 3 hibernate.cfg.xml配置添加map的程序集<mapping assembly="Model" ...