3

Michael Hartl によるRuby on Rails チュートリアルに従っています。

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

features/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, :create, relationship: { followed_id: other_user.id }
      end.to change(Relationship, :count).by(1)
    end

    it "should respond with success" do
      xhr :post, :create, 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, :destroy, id: relationship.id
      expect(response).to be_success
    end
  end
end
4

2 に答える 2

9

このチュートリアルが依存しているのバージョンxhr、2 番目の引数としてメソッドを受け取り、 fromActionController::TestCase::Behaviorです。そのモジュールは、rspec-rails gem によるコントローラーまたはビューのテスト用にのみ含まれています。2 番目の引数としてパスを期待して、Rails から別のバージョンを取得しているため、エラーが発生していますxhr

controllerディレクトリに含めるcontrollersか、テストの種類を明示的に設定して、テストの種類を確認する必要があります。featuresディレクトリにテストがあり、それ以外の方法で入力されていないため、コントローラーテストとは見なされません。(注: チュートリアルの図 11.37 では、ディレクトリにテストが存在しspec/controllersます。)

于 2013-08-31T16:26:02.063 に答える