All. I am a beginner developer of Ruby on Rails. There is an error that I have been struggling and not been able to solve.
I am trying to see if an variable @chat_group declared in messages_controller.rb
matches another variable declared in message_controller_spec.rb
. I have not had any clue to solve this error because I am 100% positive that I am giving what I am supposed to give to as a parameter, which is chat_group_id as the route says.
Does anyone have any insight to solve this problem? Or has anyone encountered a similar issue before?
If you does or has, could you please give a way of solving this problem? Any advice would be appreciated! Thanks in advnace!
1) MessagesController#GET index when the user logs in Checking if the variable in index action matches
Failure/Error: get :index, params: index_params
ActionController::UrlGenerationError:
No route matches {:action=>"index", :controller=>"messages", :params=>{:chat_group_id=>"522"}}
# ./spec/controllers/messages_controller_spec.rb:13:in `block (4 levels) in <top (required)>'
messages_controller_spec.rb
RSpec.describe MessagesController, type: :controller do
let(:chat_group) { create(:chat_group) }
let(:message) { create(:message) }
let(:index_params) { { chat_group_id: chat_group } }
describe '#GET index' do
context 'when the user logs in' do
login_user
it 'Checking if the variable in index action matches' do
get :index, params: index_params
expect(assigns(:chat_group)).to eq chat_group
end
end
end
end
Route.rb
chat_group_messages GET /chat_groups/:chat_group_id/messages(.:format) messages#index
POST /chat_groups/:chat_group_id/messages(.:format) messages#create
chat_groups GET /chat_groups(.:format) chat_groups#index
POST /chat_groups(.:format) chat_groups#create
new_chat_group GET /chat_groups/new(.:format) chat_groups#new
edit_chat_group GET /chat_groups/:id/edit(.:format) chat_groups#edit
chat_group PATCH /chat_groups/:id(.:format) chat_groups#update
PUT /chat_groups/:id(.:format) chat_groups#update
MessageController.rb
class MessagesController < ApplicationController
before_action :set_chat_group
def index
@chat_groups = current_user.chat_groups
@messages = @chat_group.messages
@message = Message.new
respond_to do |format|
format.html { render :index }
format.json { render json:
@chat_group.messages.includes(:user).map{|x| x.json_api} }
end
end
private
def set_chat_group
@chat_group = ChatGroup.find(params[:chat_group_id])
end
end
Update
I solved the error! I put the way of solving it in the comments below!
I am so sorry. I was looking at a documentation for Rails5 instead of Rails4.
It says that I should pass the chat_group_id
by writing like below.
Rails4
before do
get :index, { chat_group_id: chat_group }
end
Not Like this
before do
get :index, params: { chat_group_id: chat_group }
end
This error made me realize how important it is to read the documentation... For those who put comments and tried to do so, THANK YOU VERY MUCH!
try to change type: controller
to type: request
?
Also you can use, request type
and specify path:
get chat_group_messages_path(chat_group.id), index_params
Cheers
I think You are doing everything right.
but this line : expect(assigns(:group)).to eq group
you expected assigns(:group)
but i can't see any variable named group
in your index action that you are testing.
also you are expecting that to equal group " eq group
", but in your let
blocks in the example above no variable named group
exists.
So try to do expect(assigns(:chat_groups)).to eq chat_group
the assigns(:chat_groups)
uses the same name of the instance variable inside your action, and the eq chat_group
uses the name of the variable in the let block
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.