Testing strong parameters with shoulda_matchers - ruby-on-rails

I'm having a hard time trying to figure out how to test the strong parameters with rspec and shoulda_matchers.
No matter what I've tried, I always get the message: RSpec::Expectations::ExpectationNotMetError: Expected POST #create to restrict parameters on :language_name to :name,
but it did not restrict any parameters.
This is my controller:
class LanguageNamesController < CrudController
inherit_resources
actions :all, :except => [:show]
private
def language_name_params
params.require(:language_name).permit(:name)
end
end
This is the test:
require 'rails_helper'
RSpec.describe LanguageNamesController, type: :controller do
describe "parameters" do
it do
lm_params = {
language_name: {
name: 'John'
}
}
expect(subject).to permit(:name).for(:create, params: { params: lm_params }).on(:language_name)
end
end
end
Also, I've tried this and got the same result:
it do
subject.params = ActionController::Parameters.new(language_name: {foo: 'bar', name: 'baz'})
expect(subject).to permit(:name).for(:create).on(:language_name)
end
if I try like this, I got ArgumentError: unknown keyword: language_name:
it do
lm_params = {
language_name: {
name: 'John'
}
}
expect(subject).to permit(:name).for(:create, params: lm_params).on(:language_name)
end
I'm using:
ruby 2.5.1
rails 5.1.6
shoulda-matchers 3.1.2
Anyone know how to solve this?
Thank you!
This is my workaround to test it:
it do
faker_name = Faker::Name.name
params = {
language_name: {name: faker_name, other_attribute: "other value"},
extra: {extra: 1}
}
get :index, params: params
expect(subject.send(:language_name_params)).to eq({"name"=> faker_name})
end

You do not need to create lm_params. Shoulda matchers are going to mock up the params object for you.
If you only need to test the permissions you can do should permit(:name).for(:create)
If you need to check for a value on a permitted parameter: should permit(:name).for(:create, params: {name: 'John'})
There are good examples in the documentation for strong_parameters_matcher.rb
EDIT: Another good source of documentation is the rubydoc for shoulda matchers.

Related

In RSpec, running a patch or put test will result in an ActionController::UrlGenerationError: No route matches error

What I want to solve
I want the Rspec patch or put test to succeed.
I also tested PostsContoroller before this, and I am puzzled because I did not get the same error when testing PostsContoroller.
Error
Failures:
1) Api::V1::PostItemsController update Update Content
Failure/Error: patch :update, params: { post: post_params }
ActionController::UrlGenerationError:
No route matches {:action=>"update", :controller=>"api/v1/post_items", :post=>{:id=>1, :content=>"Update-Content", :status=>false, :post_id=>1}}
# ./spec/controllers/post_items_spec.rb:11:in `block (3 levels) in <main>'
Finished in 0.35529 seconds (files took 5.58 seconds to load)
5 examples, 1 failure
Code
FactoryBot
book.rb
FactoryBot.define do
factory :book, class: Post do
sequence(:id) { |n| n}
sequence(:title) { |n| "title#{n}" }
sequence(:author) { |n| "author#{n}" }
sequence(:image) { |n| "image#{n}"}
end
end
content.rb
FactoryBot.define do
factory :content, class: PostItem do
sequence(:id) { |n| n }
sequence(:content) { |n| "list#{n}"}
sequence(:status) { false }
end
end
Spec
post_items_spec.rb
require 'rails_helper'
RSpec.describe Api::V1::PostItemsController, type: :controller do
describe 'update' do
it 'Update Content' do
book = create(:book)
content = create(:content, post_id: book.id)
post_params = { id: content.id, content: 'Update-Content', status: false, post_id: book.id }
patch :update, params: { post: post_params }
json = JSON.parse(response.body)
expect(response.status).to eq(200)
expect(json['Update-Content']).to eq('Update-content')
end
end
end
Routes
**Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :posts
resources :post_items
end
end
end
The use of controller specs is discouraged by both the Rails and RSpec teams and has been for a very long time now. You should be writing a request spec instead which sends real HTTP requests.
RSpec.describe 'Api V1 Post items', type: :request do
let(:book) { create(:book) }
describe "PATCH /api/v1/books" do
context "with valid parameters" do
subject do
patch api_v1_post_item_path(book),
params: { content: 'Update-Content' }
end
it { should be_successful }
it "updates the content" do
# refresh the record from the db
expect { book.reload }.to change(book, :title).to('Update-Content')
end
it "includes the updated entity in the response body" do
expect(response.parsed_body['content']).to eq 'Update-Content'
end
end
# #todo write specs with invalid parameters
# #todo write specs for authentication and authorization
end
end
Another problem is that you're generating IDs in your factory. Do not do this ever. When you're actually persisting records the database will automatically assign ids. When you use build_stubbed FactoryBot will create a mock id. Using a sequence to generate IDs invites bad practices such as hardcoding ids into a spec and will only cause you headaches.
If you really want to salvage that controller spec the routing error is caused by the fact that you're missing an the ID parameter - since you're calling it as patch :update, params: { post: post_params } the id parameter is buried in params[:post][:id]. So you want patch :update, params: { id: post.id, post: post_params } I don't recommend this though - get with the program and write future proof tests instead that won't let all the bugs slip though.

RSpec controller test - ActionController::UnfilteredParameters: unable to convert unpermitted parameters to hash

In my Rails 6 app I'm trying to test controller methods which allow admin users to update user data without providing user passwords. All actions are run in ActiveAdmin.
admin/users.rb
controller do
def update
model = :user
%w[password password_confirmation].each { |p| params[model].delete(p) } if params[model][:password].blank?
super
end
end
Based on this page I tried to write specs:
admin/users_spec.rb
describe 'PUT update' do
let(:user) { create(:user, :random_email) }
let(:valid_attributes) do
ActionController::Parameters.new(
{
user: {
email: 'michael.kelso#example.com',
password: '',
},
},
)
end
before do
put :update, params: { id: user.id, user: valid_attributes }
end
it 'update user' do
expect(user.reload.email).to eq('michael.kelso#example.com')
end
end
end
What should I do to have this test green?
you can still add strong params for activeadmin.
Using method permit_params method like: permit_params :title, :content, :publisher_id
activeadmin strong param docs:
https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
this post is related to this one: https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters

RSpec testing: post multiple params to methods

I am using RSpec to test my Rails 4 application and I want to post a "multiple select" param. The params method is like this:
def general_mailing_params
params.require(:mailing).permit({:receivers => []}, :subject, :content)
end
As you can see the receivers param is a multiple select, how can I post this sort of params in RSpec test?
In RSpec controller and request specs you can simply pass arrays and hashes to create any given params hash.
Controller (functional) spec:
require 'rails_helper'
describe MailingsController do
let!(:receiver) { create(:receiver) }
describe 'POST :create' do
it "has the correct receivers" do
post :create, { mailing: { receivers: [receiver.id] } }
expect(Post.last.receivers).to eq [receiver]
end
end
end
Request (integration) spec:
require 'rails_helper'
describe 'Mailings' do
let!(:receiver) { create(:receiver) }
describe 'POST /mailings' do
it "has the correct receivers" do
post '/mailings', { mailing: { receivers: [receiver.id] } }
expect(Post.last.receivers).to eq [receiver]
end
end
end
Note however if you are using the rails collection helpers such as collection_checkboxes properly the param key should be receiver_ids.

rails 4 ActionController::UrlGenerationError no match route with rspec

I am currently using rspec for unit test, I have traveled quite a doc and I did not find the answer.
I think I just used.
when I test my controller it tells me that is not my way and yet when I test in normal mode so everything works my way to work.
here Controller
#app/controller/app/email_contacts_controller.rb
class App::EmailContactsController < App::ApplicationController
def create
end
end
here rspec controller
require 'rails_helper'
describe App::EmailContactsController, type: :controller do
login_lawyer_partner
let(:contact) { create(:contact) }
let(:lawyer) { controller.current_user }
let(:order) { create :order, contact_id: contact.id, lawyer_id: lawyer.id}
describe 'POST email_contacts' do
before :each do
xhr :post :create, id: order.id, mail: { subject: 'Mail Subject', reply_to: 'toto titi <tooto#titi.com', message: 'This is a long message which length has to be greater than seventy characters, so it passes' }
order.reload
end
it { expect(Order.all).to eq [order] }
it { is_expected.to respond_with :success }
it { expect(order.comments.count).to eq 1 }
end
end
puts params => "controller"=>"app/email_contacts", "action"=>"create"
And
rake route
app_order_email_contacts_path POST /app/orders/:order_id/email_contacts(.:format) app/email_contacts#create
Yet the url is working well, so I think I forgot something or keyword, but I do not see what could miss
Thank you
I think I may have spotted the error. When using a xhr in controller tests there is a comma after the verb symbol. You are missing one after the :post.
Minimally rewritten:
xhr :post, :create, id: order.id, mail: { ...

Set Rspec default GET request format to JSON

I am doing functional tests for my controllers with Rspec. I have set my default response format in my router to JSON, so every request without a suffix will return JSON.
Now in rspec, i get an error (406) when i try
get :index
I need to do
get :index, :format => :json
Now because i am primarily supporting JSON with my API, it is very redundant having to specify the JSON format for every request.
Can i somehow set it to default for all my GET requests? (or all requests)
before :each do
request.env["HTTP_ACCEPT"] = 'application/json'
end
Put this in spec/support:
require 'active_support/concern'
module DefaultParams
extend ActiveSupport::Concern
def process_with_default_params(action, parameters, session, flash, method)
process_without_default_params(action, default_params.merge(parameters || {}), session, flash, method)
end
included do
let(:default_params) { {} }
alias_method_chain :process, :default_params
end
end
RSpec.configure do |config|
config.include(DefaultParams, :type => :controller)
end
And then simply override default_params:
describe FooController do
let(:default_params) { {format: :json} }
...
end
The following works for me with rspec 3:
before :each do
request.headers["accept"] = 'application/json'
end
This sets HTTP_ACCEPT.
Here is a solution that
works for request specs,
works with Rails 5, and
does not involve private API of Rails (like process).
Here's the RSpec configuration:
module DefaultFormat
extend ActiveSupport::Concern
included do
let(:default_format) { 'application/json' }
prepend RequestHelpersCustomized
end
module RequestHelpersCustomized
l = lambda do |path, **kwarg|
kwarg[:headers] = {accept: default_format}.merge(kwarg[:headers] || {})
super(path, **kwarg)
end
%w(get post patch put delete).each do |method|
define_method(method, l)
end
end
end
RSpec.configure do |config|
config.include DefaultFormat, type: :request
end
Verified with
describe 'the response format', type: :request do
it 'can be overridden in request' do
get some_path, headers: {accept: 'text/plain'}
expect(response.content_type).to eq('text/plain')
end
context 'with default format set as HTML' do
let(:default_format) { 'text/html' }
it 'is HTML in the context' do
get some_path
expect(response.content_type).to eq('text/html')
end
end
end
FWIW, The RSpec configuration can be placed:
Directly in spec/spec_helper.rb. This is not suggested; the file will be loaded even when testing library methods in lib/.
Directly in spec/rails_helper.rb.
(my favorite) In spec/support/default_format.rb, and be loaded explicitly in spec/rails_helper.rb with
require 'support/default_format'
In spec/support, and be loaded by
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
which loads all the files in spec/support.
This solution is inspired by knoopx's answer. His solution doesn't work for request specs, and alias_method_chain has been deprecated in favor of Module#prepend.
In RSpec 3, you need make JSON tests be request specs in order to have the views render. Here is what I use:
# spec/requests/companies_spec.rb
require 'rails_helper'
RSpec.describe "Companies", :type => :request do
let(:valid_session) { {} }
describe "JSON" do
it "serves multiple companies as JSON" do
FactoryGirl.create_list(:company, 3)
get 'companies', { :format => :json }, valid_session
expect(response.status).to be(200)
expect(JSON.parse(response.body).length).to eq(3)
end
it "serves JSON with correct name field" do
company = FactoryGirl.create(:company, name: "Jane Doe")
get 'companies/' + company.to_param, { :format => :json }, valid_session
expect(response.status).to be(200)
expect(JSON.parse(response.body)['name']).to eq("Jane Doe")
end
end
end
As for setting the format on all tests, I like the approach from this other answer: https://stackoverflow.com/a/14623960/1935918
Perhaps you could add the first answer into spec/spec_helper or spec/rails_helper with this:
config.before(:each) do
request.env["HTTP_ACCEPT"] = 'application/json' if defined? request
end
if in model test (or any not exist request methods context), this code just ignore.
it worked with rspec 3.1.7 and rails 4.1.0
it should be worked with all rails 4 version generally speaking.
Running Rails 5 and Rspec 3.5 I had to set the headers to accomplish this.
post '/users', {'body' => 'params'}, {'ACCEPT' => 'application/json'}
Thi matches what the example in the docs looks like:
require "rails_helper"
RSpec.describe "Widget management", :type => :request do
it "creates a Widget" do
headers = {
"ACCEPT" => "application/json", # This is what Rails 4 accepts
"HTTP_ACCEPT" => "application/json" # This is what Rails 3 accepts
}
post "/widgets", { :widget => {:name => "My Widget"} }, headers
expect(response.content_type).to eq("application/json")
expect(response).to have_http_status(:created)
end
end
Per the Rspec docs, the supported method is through the headers:
require "rails_helper"
RSpec.describe "Widget management", :type => :request do
it "creates a Widget" do
headers = {
"ACCEPT" => "application/json", # This is what Rails 4 and 5 accepts
"HTTP_ACCEPT" => "application/json", # This is what Rails 3 accepts
}
post "/widgets", :params => { :widget => {:name => "My Widget"} }, :headers => headers
expect(response.content_type).to eq("application/json")
expect(response).to have_http_status(:created)
end
end
For those folks who work with request tests the easiest way I found is to override #process method in ActionDispatch::Integration::Session and set default as parameter to :json like this:
module DefaultAsForProcess
def process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: :json)
super
end
end
ActionDispatch::Integration::Session.prepend(DefaultAsForProcess)
Not sure if this will work for this specific case. But what I needed in particular was to be able to pass a params hash to the post method. Most solutions seem to be for rspec 3 and up, and mention adding a 3rd parameter like so:
post '/post_path', params: params_hash, :format => 'json'
(or similar, the :format => 'json' bit varies)
But none of those worked. The controller would receive a hash like: {params: => { ... }}, with the unwanted params: key.
What did work (with rails 3 and rspec 2) was:
post '/post_path', params_hash.merge({:format => 'json'})
Also check this related post, where I got the solution from: Using Rspec, how do I test the JSON format of my controller in Rails 3.0.11?
Why don't RSpec's methods, "get", "post", "put", "delete" work in a controller spec in a gem (or outside Rails)?
Based off this question, you could try redefining process() in ActionController::TestCase from https://github.com/rails/rails/blob/32395899d7c97f69b508b7d7f9b7711f28586679/actionpack/lib/action_controller/test_case.rb.
Here is my workaround though.
describe FooController do
let(:defaults) { {format: :json} }
context 'GET index' do
let(:params) { defaults }
before :each do
get :index, params
end
# ...
end
context 'POST create' do
let(:params) { defaults.merge({ name: 'bar' }) }
before :each do
post :create, params
end
# ...
end
end

Resources