3

I'm following the Ruby on Rails Tutorial by Michael Hartl.

I reached Chapter 11.37 but my test is failing. I get the following error:

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

I'm new to Ruby on Rails so I don't really know what's going wrong. Can someone help resolve this error?

controllers/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
1
  • Can you show your stacktrace down to where it hits your code? Aug 31, 2013 at 14:04

2 Answers 2

9

The version of xhr that this tutorial depends on, which takes a method as the second argument, is from ActionController::TestCase::Behavior. That module is only included for controller or view tests by the rspec-rails gem. You are picking up another version of xhr from Rails, with expects a path as the second argument, hence the error you're getting.

You need to make sure that your test is of type controller by including it in the controllers directory or setting the test type explicitly. Because you have the test in the features directory and not otherwise typed, it's not considered a controller test. (Note: Figure 11.37 in the tutorial does have the test residing in the spec/controllers directory.)

2
  • Your eyes are so sharp to find features directory in question.
    – Billy Chan
    Aug 31, 2013 at 18:02
  • @BillyChan I wish they had been. ;-) I only thought to look for it after searching the rspec-rails code for references to the module in question. Aug 31, 2013 at 18:05
3

xhr method seems to receive a path instead of an action name. So it should work if you replace by

xhr :post, relationships_path, relationship: { followed_id: other_user.id }
7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.