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. Indent Guides 代码括号对齐工具

    搜不到怎么办: 下载版本要正确.

  2. Idea安装及简单配置

    1. 安装JDK   设置环境变量   JAVA_HOME    C:\Program Files\Java\jdk1.8.0_45   CLASSPATH    .;%JAVA_HOME%\lib; ...

  3. JQuery实现的模块交换动画效果

    <!doctype html> <html> <head> <meta http-equiv="content-type" content ...

  4. WAMPP安装后mysql无法启动

    上午 10:23:42 [mysql] This may be due to a blocked port, missing dependencies, 上午 10:23:42 [mysql] imp ...

  5. vs2012中将图片放到resource中进行调用

    1.在项目中新建一个名叫resource的文件夹,然后将所需图片信息放入该文件夹,如图 2.右击该项目,选择属性->资源选项卡,步骤如图所示 点击添加现有文件,然后找到你刚刚添加的resourc ...

  6. tablediff使用方法

    tablediff -sourceserver "db0093\sql2008" -sourcedatabase "testly" -sourcetable & ...

  7. Java 字符串拼接 五种方法的性能比较分析 从执行100次到90万次

    [请尊重原创版权,如需引用,请注明来源及地址] > 字符串拼接一般使用“+”,但是“+”不能满足大批量数据的处理,Java中有以下五种方法处理字符串拼接,各有优缺点,程序开发应选择合适的方法实现 ...

  8. OpenLDAP,一登录系统就修改密码

    http://guodayong.blog.51cto.com/263451/d-2 郭大勇的博客   1:修改配置文件 在前面打开注释 moduleload ppolicy.la modulepat ...

  9. 一、javase学习:数据库操作练习

    package JDBC_TEST; import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLExcept ...

  10. 使用miniui框架制作树形节点

    <div id="leftTree" class="mini-outlooktree" url="<%=basePath%>mana ...