gpt4 book ai didi

ruby-on-rails - Rspec 使用 has_many throw 方法删除对象失败,没有路由匹配 { :action= >"destroy"

转载 作者:行者123 更新时间:2023-11-28 21:27:12 25 4
gpt4 key购买 nike

需要一些帮助来使 rspec 测试通过,我的目标是删除使用 has_many throw 方法创建的关系。我跟着这个MHartl's tutorial .

关系 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!(:relationship) { create(:relationship) }

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

路线.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

用户.rb:

class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable,
:validatable, :confirmable, :timeoutable

# Associations
has_many :active_relationships, class_name: "Relationship",
foreign_key: "follower_id",
dependent: :destroy
has_many :passive_relationships, class_name: "Relationship",
foreign_key: "followed_id",
dependent: :destroy
has_many :following, through: :active_relationships, source: :followed
has_many :followers, through: :passive_relationships, source: :follower

# Follows a user.
def follow(other_user)
active_relationships.create(followed_id: other_user.id)
end

# Unfollows a user.
def unfollow(other_user)
active_relationships.find_by(followed_id: other_user.id).destroy
end

# Returns true if the current user is following the other user.
def following?(other_user)
following.include?(other_user)
end
end

工厂:

FactoryGirl.define do
factory :relationship do
follower_id 1
followed_id 1
end
end

失败:

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

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

2) RelationshipsController#destroy should require logged-in user to destroy relationship
Failure/Error: active_relationships.find_by(followed_id: other_user.id).destroy

NoMethodError:
undefined method `id' for nil:NilClass

最佳答案

您的销毁路径是 /relationships/:id 并且 Controller 会销毁关系,但是您传递的不是关系的 :id 而不是 :followed_id,所以确实没有这样的路由。

因此您可以修改您的路由和 Controller ,或者将当前代码更改为:

delete :destroy, id: relationship.id

(然后您可能会收到有关没有用户的关系的错误,具体取决于工厂)

关于ruby-on-rails - Rspec 使用 has_many throw 方法删除对象失败,没有路由匹配 { :action= >"destroy",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37025967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com