我达到了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 endend
功能/ 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 endend解决方法 本教程所依赖的xhr的 The version,其中一个方法作为第二个参数,来自ActionController :: TestCase :: Behavior.该模块仅包含在rspec-rails gem的控制器或视图测试中.你正在从Rails中获取 another version of
xhr
,期望一个路径作为第二个参数,因此你得到的错误. 您需要通过将其包含在controllers目录中或明确设置测试类型来确保您的测试属于控制器类型.因为您在features目录中进行了测试而未进行其他类型的测试,所以它不被视为控制器测试. (注意:教程中的图11.37确实有测试驻留在spec / controllers目录中.)
总结以上是内存溢出为你收集整理的ruby-on-rails – 如何解决“错误的参数(预期的URI对象或URI字符串)”? Ruby on Rails,Michael Hartl教程全部内容,希望文章能够帮你解决ruby-on-rails – 如何解决“错误的参数(预期的URI对象或URI字符串)”? Ruby on Rails,Michael Hartl教程所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)