【问题标题】:Rails4 Rspec Controller Tests No route matchesRails4 Rspec 控制器测试没有路由匹配
【发布时间】: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


    【解决方案1】:

    我很抱歉。我正在查看 Rails5 而不是 Rails4 的文档。

    它说我应该像下面这样写通过chat_group_id

    Rails4

    before do
      get :index, { chat_group_id: chat_group }
    end
    

    不是这样的

    before do
      get :index, params: { chat_group_id: chat_group }
    end 
    

    这个错误让我意识到阅读文档是多么重要...... 对于那些放置 cmets 并尝试这样做的人,非常感谢!

    【讨论】:

      【解决方案2】:

      尝试将type: controller 更改为type: request

      您也可以使用request type 并指定路径:

      get chat_group_messages_path(chat_group.id), index_params
      

      干杯

      【讨论】:

      • 你能告诉我你用这种方法遇到了什么错误吗?
      • 我解决了这个错误!非常感谢您的帮助,zauzaj!
      【解决方案3】:

      我认为你做的一切都是对的。

      但是这一行:expect(assigns(:group)).to eq group

      您期望 assigns(:group) 但我在您正在测试的索引操作中看不到任何名为 group 的变量。

      您也期望它等于组“eq group”,但在上面示例中的let 块中,不存在名为group 的变量。

      所以尝试做expect(assigns(:chat_groups)).to eq chat_group

      assigns(:chat_groups) 在您的操作中使用与实例变量相同的名称,eq chat_group 使用 let 块中的变量名称

      【讨论】:

      • 您可以随时参考relish documentation of rspec 了解更多信息,并查看示例。
      • 抱歉,我更新了我的问题。应该说的是chat_group而不是group,但是我错误地复制了错误的术语。
      • 正如你所说的 Abdullah,我阅读了文档。但是,即使我传递了路由所需的 chat_group_id,我也没有得到出现“无路由匹配”错误的原因。这让我很困扰......
      • 尝试手动通过浏览器请求索引动作,看看是否再次产生这个路由错误。
      • 在堆栈溢出时看到这个link,他的错误信息和你一样,检查一下。
      猜你喜欢
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      相关资源
      最近更新 更多