
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
What's the responsibility of a controller?
Render. Redirect. Flash.
I'd argue that those are, in fact, the only responsibilities controllers have. Now, I realize controllers' actions are often burdened with resource creation, deletion and updating, but let's keep it real here - all this logic belongs to another object, usually a service. And indeed, most of the non-trivial, well designed applications use service objects, instead of cramming everything inside poor controllers.
Add this line to your application's Gemfile:
gem 'ctrlspecs'
And then execute:
$ bundle
Or install it yourself as:
$ gem install ctrlspecs
In your rails_helper.rb add:
RSpec.configure do |config|
config.extend Ctrlspecs
...
end
Ctrlspecs gives you a few helpers, which are basically just shared examples testing what your typical controller does:
subject { post :create, params } # subject is required
it_renders_template :create
it_redirects_to 'resources_path(assigns(:resource))' # the path argument will be evaled
it_flashes notice: I18n.t('shared.created')
it_calls_service SuchService
Ctrlspecs also provides contexts for testing failures:
mock_save_record_failure
mock_service_returning_error, VeryService, Response::Error
So, how does your typical controller action spec look like now?
require 'rails_helper'
describe PostsController do
describe 'POST #create' do
let!(:params) do
{ post: { content: 'such content, wow' } }
end
subject { post :create, params }
context 'success' do
it_redirects_to 'post_path(assigns(:post))'
it_flashes notice: I18n.t('shared.created', resource: 'Post')
end
context 'failure' do
mock_save_record_failure
it_renders_template :new
end
end
describe 'GET #index' do
subject { get :index }
it_calls_service Posts::GetAllTheFrigginPosts
end
end
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)FAQs
Unknown package
We found that ctrlspecs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.