【发布时间】:2017-08-10 11:36:46
【问题描述】:
全部。我是 Ruby on Rails 的初学者开发人员。有一个错误,我一直在苦苦挣扎,无法解决。
我正在尝试查看messages_controller.rb 中声明的变量@chat_group 是否与message_controller_spec.rb 中声明的另一个变量匹配。我没有任何线索来解决这个错误,因为我 100% 肯定我正在提供我应该作为参数提供的内容,即路由所说的 chat_group_id。
有没有人有任何见解来解决这个问题? 或者有没有人遇到过类似的问题?
如果你有或有过,你能给出解决这个问题的方法吗? 任何意见,将不胜感激!提前感谢!
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
更新
我解决了错误!我把解决的方法放在下面的cmets中!
【问题讨论】:
标签: ruby-on-rails rspec rspec-rails rspec3