Have gone through othe similar questions but the problem is not solved.
Test:
expect{
post message_direct_create_path(friend.id), params: {message_direct:
FactoryBot.attributes_for(:message_direct, user_id: user.id,
friend_id: friend.id)}
}.to change{MessageDirect.count}.by(1)
Route:
post '/message_directs/:friend_id', to: 'message_directs#create', as: 'message_direct_create'
Error:
ActionController::UrlGenerationError:
No route matches {:action=>"/message_directs/1",
:controller=>"message_directs", :message_direct=>{:text=>"Sends
Message", :user_id=>1, :friend_id=>1}}
I have also tried
:create and "/message_directs/#{friend.id}" but the error is still same
Related
I am trying to test patch for devise user info using Rspec, the update url looks like this # PATCH/PUT /api/users/1 but I am getting this error for all the following cases
Error ArgumentError:
wrong number of arguments (given 2, expected 1)
Cases I tried
patch :update, {'id'=> #api_user['user']['id'], 'user' => attributes_for(:normal_user)}
patch :update, 'id'=> #api_user['user']['id'], 'user' => attributes_for(:normal_user)
patch :update, 'id'=> #api_user['user']['id'], :params => {'user' => attributes_for(:normal_user)}
And I tried this
patch :update, :params => {'user' => create(:normal_user)}. # this one has the id within
but gives this error
No route matches {:action=>"update", :controller=>"api/users", :user=>#<User id: 227794695, email: "test11#example.com", created_at: "2020-05-03 08:51:55", updated_at: "2020-05-03 08:51:55", is_admin: nil, first_name: "test", last_name: "test">} which make sence, the url should be update/id
you should not put "update" after patch, since
patch it self will automatically will route to update to user controller
this is the reason error message informed you given 2, expected 1 arguments
here is sample for your reference from update
require 'rails_helper'
RSpec.describe 'User request', type: :request do
it 'should update user email' do
patch "/api/users/#{#api_user['user']['id']}",
params: {
user: {
email: 'new_email_address#gmail.com'
}
},
as: :json
expect(response).to have_http_status(:success)
end
end
here is sample of /config/routes.rb for your reference to update users
Rails.application.routes.draw do
namespace :api, defaults: { format: :json } do
resources :users, only: [:create, :update, :destroy]
end
end
While testing my controller action I am getting this routing error. I am not able to figure it out where I am doing it wrong
route
get ':user_type', to: 'user_posts#index', param: :user_type, constraints: lambda { |req| ['abc', 'def'].include?(req.params["user_type"]) }, as: :custom_posts
Spec:
describe "GET #index: enabled" do
it "returns http success" do
get :index, params: {user_type: 'abc'}
expect(response).to have_http_status(:success)
end
end
Error:
ActionController::UrlGenerationError:
No route matches {:action=>"index", :controller=>"user_posts"}
Also, I tried one solution on github by changing my route but it didn't work for me
get ':user_type', to: 'user_posts#index', param: :user_type, constraints: {user_type: lambda { |req| ['abc', 'def'].include?(req.params["user_type"]) } }, as: :custom_posts
Finally, I got an answer. Here is the detail discussion
describe "GET #index: enabled" do
it "returns http success" do
get :index, params: { user_type: 'abc', param: :user_type }
expect(response).to have_http_status(:success)
end
end
I have the following code:
it 'should create' do
post :create, investor: VALID_CREATE_PARAMS
expect(response).to redirect_to(controller: 'admin/investors', action: 'show')
end
It generates following error:
1) Admin::InvestorsController create should create
Failure/Error: expect(response).to redirect_to(controller: 'admin/investors', action: 'show')
ActionController::UrlGenerationError:
No route matches {:action=>"show", :controller=>"admin/investors"}
The routes are
namespace :admin do
resources :investors
end
How can I fix this issue? I have no option to assert redirection with admin_investor_path, because I keep url params while redirecting.
Show action by default requires an id to be passed as well, without the id it can't generate the url
You need to add it like this:
expect(response).to redirect_to(controller: 'admin/investors', action: 'show', id: assigns(:investor).id )
Very strange error. I am writing RSpec tests for a subgroup controller, which is in a many-to-one relationship with group. group accepts nested attributes for subgroup. I used scaffolding, and I've used the tests provided (edited to test). These tests were all working before I started testing for Devise. Now, my tests for updating with invalid parameters are failing.
tests in subgroups_controller_spec.rb
let(:invalid_attributes) {
{name: nil, group_id: nil}
}
context "with invalid params" do
it "assigns the subgroup as #subgroup" do
subgroup = Subgroup.create! valid_attributes
puts "param: #{subgroup.to_param}, id: #{subgroup.id}
put :update, {:id => subgroup.to_param, :subgroup => invalid_attributes}, valid_session
expect(assigns(:subgroup)).to eq(subgroup)
end
it "re-renders the 'edit' template" do
subgroup = Subgroup.create! valid_attributes
put :update, {:id => subgroup.to_param, :subgroup => invalid_attributes}, valid_session
expect(response).to render_template("edit")
end
end
error message:
1) SubgroupsController PUT #update with invalid params assigns the subgroup as #subgroup
Failure/Error: put :update, {:id => subgroup.to_param, :subgroup => invalid_attributes}, valid_session
ActionView::Template::Error:
No route matches {:action=>"show", :controller=>"groups", :id=>nil, :locale=>nil}
But puts "param: #{subgroup.to_param}, id: #{subgroup.id}" prints param: 1, id: 1 so clearly these ids are not nil.
rake routes:
subgroups GET (/:locale)/subgroups(.:format) subgroups#index
POST (/:locale)/subgroups(.:format) subgroups#create
new_subgroup GET (/:locale)/subgroups/new(.:format) subgroups#new
edit_subgroup GET (/:locale)/subgroups/:id/edit(.:format) subgroups#edit
subgroup GET (/:locale)/subgroups/:id(.:format) subgroups#show
PATCH (/:locale)/subgroups/:id(.:format) subgroups#update
PUT (/:locale)/subgroups/:id(.:format) subgroups#update
DELETE (/:locale)/subgroups/:id(.:format) subgroups#destroy
p.s. there are locales listed in the routes but I removed them for easier viewing. I don't think locales is this issue as update needs an :id to update.
The problem is not with your put route, with the in the rendering that it's doing. You're getting a template error and the error specifics show that you're trying to do a show on groups. Check your layout for paths that are used.
my
describe PaymentController do
it 'should assign amount_to_pay' do
get bank_payment_path, {id: "1"}
expect(assigns[:amount_to_pay]).to eq "123"
end
end
my error:
Error: ActionController::RoutingError: No route matches {:action=>"bank", :controller=>"payment"}
and rake routes:
bank_payment GET /payment/:id/bank(.:format) payment#bank
What is wrong there?
The request should be:
get :bank, id: "1"