mercredi 4 mai 2016

Rspec delete object failure with method has_many throw, No route matches {:action=>"destroy"

Need some help to make rspec test to pass, my goal is to delete a relationship that has been created with the method has_many throw. I followed this MHartl's tutorial.

relationships_controller.rb :

class RelationshipsController < InheritedResources::Base

      def create
        user = User.find(params[:followed_id])
        current_user.follow(user)
        redirect_to user
      end

      def destroy
        user = Relationship.find(params[:id]).followed
        current_user.unfollow(user)
        redirect_to user
      end
    end

relationships_controller_spec.rb :

require 'rails_helper'

describe RelationshipsController do
  let(:relationship) { create(:relationship) }
  let(:user) { create(:user) }

  before do
    sign_in :user, create(:user)
  end

  describe '#create' do
    let!(:followed) { create(:user) }
    it "should require logged-in user to create relationship" do
      expect{
        post :create, followed_id: followed.id
      }.to change(Relationship, :count).by(1)
      redirect_to root_path
    end
  end

  describe '#destroy' do
    let!(:followed) { create(:user) }

    it "should require logged-in user to destroy relationship" do
      expect {
        delete :destroy, followed_id: followed.id
      }.to change(Relationship, :count).by(-1)
      redirect_to root_path
    end
  end
end

routes.rb :

Rails.application.routes.draw do
  devise_for :users, controllers: { sessions: "users/sessions" }
  devise_for :admin_users, ActiveAdmin::Devise.config

  ActiveAdmin.routes(self)

  mount Peek::Railtie => '/peek'

  resources :users do
    member do
      get :following, :followers
    end
  end
  resources :relationships, only: [:create, :destroy]

  root to: "records#index"
end

Failures:

  1) RelationshipsController#destroy should require logged-in user to destroy relationship
     Failure/Error: delete :destroy, followed_id: followed.id

     ActionController::UrlGenerationError:
       No route matches {:action=>"destroy", :controller=>"relationships", :followed_id=>"4"}

  2) Relationships GET /relationships works! (now write some real specs)
     Failure/Error: get relationships_path

     ActionController::RoutingError:
       No route matches [GET] "/relationships"

Aucun commentaire:

Enregistrer un commentaire