아주 간단하지만 테스트 통과 방법을 알 수 없습니다.삭제할 때 ID를 전달하는 방법 : Rails에서 삭제 하시겠습니까?
저는 (레일 패키지를과 비슷한 방식으로 구축하고 있습니다.) 테스트하고자하는 컨트롤러가 friendships
입니다. 그것은 localhost에서 작동합니다. 이것은 내가 만든 테스트입니다. POST #create
이 (가) 전달되었습니다.
require 'rails_helper'
RSpec.describe FriendshipsController, type: :controller do
login_user
before :each do
@friend1 = FactoryGirl.create(:user)
end
describe "POST #Create" do
it "adds new friend" do
expect {
post :create, params: { friend_id: @friend1.id}
}.to change(Friendship, :count).by(1)
end
end
describe "DELETE #destroy" do
it "removes a friend =(" do
expect {
delete :destroy, id: @friend1.id
}.to change(Friendship, :count).by(1)
end
end
end
이 실제 컨트롤러 : resources :friendships
이
오전 데 문제가 ID.I 수 없어 그림을 전달하는 것입니다 : 나는 또한 routes.rb
이 우정이 있는지 확인했다
class FriendshipsController < ApplicationController
def create
@friendship = current_user.friendships.build(friend_id: params[:friend_id])
if @friendship.save
flash[:notice] = "New friend added!"
redirect_to root_url
else
flash[:error] = "Error adding friend"
redirect_to root_url
end
end
def destroy
@friendship = current_user.friendships.find(params[:id])
@friendship.destroy
flash[:notice] = "Remove friendship"
redirect_to current_user
end
end
id
매개 변수를 전달하는 방법을 알려주세요. 내 공장과 관련이 있다고 생각해?
1) FriendshipsController DELETE #destroy removes a friend =(
Failure/Error: @friendship = current_user.friendships.find(params[:id])
ActiveRecord::RecordNotFound:
Couldn't find Friendship with 'id'=159 [WHERE "friendships"."user_id" = $1]
나는 this one, this 같은 다른 SO destroy
관련 게시물을 검색하지만 내 다른 경우입니다.
내 파괴 행동에 대한 ID 매개 변수를 어떻게 내릴 수 있습니까?
편집 : (Source)
module ControllerMacros
def login_user
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
user = FactoryGirl.create(:user)
#user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the "confirmable" module
sign_in user
end
end
end
아래의 대답에 의해 제안, 내가 그랬어 :
describe "DELETE #destroy" do
it "removes a friend =(" do
friendship = user.friendships.create!(friend_id: @friend1.id)
expect {
delete :destroy, id: friendship.id
}.to change(Friendship, :count).by(1)
end
end
그러나 지금이 오류 반환 : 당신으로
FriendshipsController DELETE #destroy removes a friend =(
Failure/Error: friendship = user.friendships.create!(friend_id: @friend1.id)
NameError:
undefined local variable or method `user' for #<RSpec::ExampleGroups::FriendshipsController::DELETEDestroy:0x007fee1ce68c70>
감사합니다. 바바라! 나는 진전을 이루었지만 아직도 문제가있다. '정의되지 않은 지역 변수 또는 메소드 사용자'. 'login_user'와 내 새로운 DELETE 테스트를 포함하도록 대답을 편집했습니다. 내가 뭘 잘못했는지 알아? – Iggy
답변을 업데이트했습니다. 지금은 좋았어. – Babar