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.
相关文章
- ORA-14450: attempt to access a transactional temp table already in use
- [Javascript] Broadcaster + Operator + Listener pattern -- 10. Define a Function to Set Common Behaviors in Operators
- [Svelte 3] Use await block to wait for a promise and handle loading state in Svelte 3
- [PWA] Add Push Notifications to a PWA with React in Chrome and on Android
- [Tools] Convert SVG to a PDF in Node with PDFKit and SVG.js
- [RxJS] `add` Inner Subscriptions to Outer Subscribers to `unsubscribe` in RxJS
- [Tailwind] Abstract Utility Classes to BEM Components in Tailwind
- [AngularFire] Angular File Uploads to Firebase Storage with Angular control value accessor
- 利用crawlergo-to-xray实现自动化漏洞被动扫描平台搭建
- [XState + React] using @xstate/inspect to display state machine char in webapp
- [Unit testing + Jest] Use jest-in-case to Reduce Duplication and Improve Test Titles
- [Tools] Convert SVG to a PDF in Node with PDFKit and SVG.js
- [Nuxt] Use Vuex Actions to Delete Data from APIs in Nuxt and Vue.js
- [Protractor] Use protractor to catch errors in the console
- How to open a given CDS view in ABAP development studio
- How to achieve conditional break point in your ABAP program
- Step by step to create web service in Cloud Application studio and bind it to a custom action in sta
- How to hide the technical attributes in configuration
- How to resolve error message Pricing - Document currency missing in sales
- How to create and consume web service in CRM
- How to put S4 extension field to CRM WebUI search view in the design time
- 成功解决ValueError: Unable to add relationship because child variable ‘ID‘ in ‘cats_df‘ is also its inde
- 成功解决ERROR: Unable to find the development tool `cc` in your path; please make sure that you have the
- Unable to update the EntitySet 'T_JsAPI' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.
- SyntaxError: Missing parentheses in call to 'print'
- How to write a testbench in Verilog?