加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何解决“错误的参数(预期的URI对象或URI字符

发布时间:2020-12-17 01:47:28 所属栏目:百科 来源:网络整理
导读:迈克尔哈特尔跟随 Ruby on Rails Tutorial. 我达到了Chapter 11.37,但我的测试失败了.我收到以下错误: Failure/Error: xhr :post,:create,relationship: { followed_id: other_user.id }ArgumentError:bad argument (expected URI object or URI string) 我
迈克尔哈特尔跟随 Ruby on Rails Tutorial.

我达到了Chapter 11.37,但我的测试失败了.我收到以下错误:

Failure/Error: xhr :post,:create,relationship: { followed_id: other_user.id }
ArgumentError:
bad argument (expected URI object or URI string)

我是Ruby on Rails的新手,所以我真的不知道出了什么问题.有人可以帮助解决此错误吗?

控制器/ relationships_controller.rb:

class RelationshipsController < ApplicationController
  before_action :signed_in_user

  def create
    @user = User.find(params[:relationship][:followed_id])
    current_user.follow!(@user)
    respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end
  end

  def destroy
    @user = Relationship.find(params[:id]).followed
    current_user.unfollow!(@user)
    respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end
  end
end

功能/ relationships_controller_spec.rb:

require 'spec_helper'

describe RelationshipsController,type: :request do

  let(:user) { FactoryGirl.create(:user) }
  let(:other_user) { FactoryGirl.create(:user) }

  before { sign_in user,no_capybara: true }

  describe "creating a relationship with Ajax" do

    it "should increment the Relationship count" do
      expect do
        xhr :post,relationship: { followed_id: other_user.id }
      end.to change(Relationship,:count).by(1)
    end

    it "should respond with success" do
      xhr :post,relationship: { followed_id: other_user.id }
      expect(response).to be_success
    end
  end

  describe "destroying a relationship with Ajax" do

    before { user.follow!(other_user) }
    let(:relationship) { user.relationships.find_by(followed_id: other_user) }

    it "should decrement the Relationship count" do
      expect do
        xhr :delete,:destroy,id: relationship.id
      end.to change(Relationship,:count).by(-1)
    end

    it "should respond with success" do
      xhr :delete,id: relationship.id
      expect(response).to be_success
    end
  end
end

解决方法

本教程所依赖的xhr的 The version,其中一个方法作为第二个参数,来自ActionController :: TestCase :: Behavior.该模块仅包含在rspec-rails gem的控制器或视图测试中.你正在从Rails中获取 another version of xhr,期望一个路径作为第二个参数,因此你得到的错误.

您需要通过将其包含在controllers目录中或明确设置测试类型来确保您的测试属于控制器类型.因为您在features目录中进行了测试而未进行其他类型的测试,所以它不被视为控制器测试. (注意:教程中的图11.37确实有测试驻留在spec / controllers目录中.)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读