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. 项目在build machine中失败,本地Build成功的程序集版本问题

    MSBuild在build machine中遇到which has a higher version than its reference assembly:(in my case let's say ...

  2. 搭载在webstorm上的go语言开发插件安装

    1. 2.搜索框内搜索go,单击“Browse repositories... ”没有匹配结果(因本人已安装好插件,所以go已经显示在上面了) 3.单击"Manage repositorie ...

  3. completed solution matches microsoft sequential workflow tutorial

    microsoft sequential workflow tutorial website:http://msdn.microsoft.com/en-us/library/ms734794(v=vs ...

  4. IT蓝豹--RecyclerView加载不同view实现效果

    本项目由开发者:黄洞洞精心为初学者编辑RecyclerView的使用方法. RecyclerView加载不同view实现效果,支持加载多个view,并且支持用volley获取数据, 项目主要介绍: 初 ...

  5. DSP学习中遇到的几个问题(初级)

    1..c和.asm 文件分别为C语言和汇编语言的源文件. 2..cmd是存储器分配说明文件,主要功能是指定工程中的各段分配到那段存储器中,比如有片内RAM(起始地址,大小)和SDRAM等.这些要根据平 ...

  6. shell 里的变量 总结

    对于linux shell的使用者来说, 巧妙的应用变量不仅能够快速的解决问题,同时能够获取非常大的乐趣,因为shell的变量内部可以附加一些运算,使得程序非常简洁明了并且功能强大,以下详细介绍一下: ...

  7. JavaScript DOM编程艺术读书笔记(四)

    第十章 实现动画效果 var repeat = "moveElement('"+elementID+"',"+final_x+","+fin ...

  8. Python之路-Day2

    二进制 1bit = 一个二进制位 8bit = 1byte 循环 for while 数据类型 数字.字符串.列表.元祖.字典.集合 字符编码 文件处理 for循环: for i in range( ...

  9. windows 下 gvim打开默认全屏显示

    在_vimrc文件中加入如下配置即可: autocmd GUIEnter * simalt ~x

  10. 初学c# -- 学习笔记(八)

    RichTextBox我服了,背景透明.宽度高度自适应.图片背景透明.gif动画等等都tm实现困难无比,搞来搞去,就最后一题做不出来了: RichTextBox不管什么背景,透明背景也好,图片背景也罢 ...