rspecコントローラテストを実行するときになぜ私がいつもゼロの値を得ているのか尋ねたいのですが? 私はすでにこのサイトで読むと回答の最も内側に使用して複数形の単語が を割り当てるので、私の場合で働いて、私はまだ同じ値 を得た。これは私のコントローラRspecでの使用を割り当てます
class ContactsController < ApplicationController
load_and_authorize_resource
before_action :find_contact, only: [:show,:edit,:update,:destroy]
def index
authorize! :index, Contact
@contact = Contact.accessible_by(current_ability)
# @contact = Contact.all
end
def show
end
def new
@contact = Contact.new
end
def edit
end
def create
@contact = current_user.contact.new(contact_params)
if @contact.save
redirect_to @contact
else
render 'new'
end
end
def update
# @contact = Contact.find(params[:id])
if @contact.update(contact_params)
redirect_to @contact
else
render 'edit'
end
end
def destroy
@contact.destroy
redirect_to contacts_path
end
private
def contact_params
params.require(:contact).permit(:firstname,
:lastname,
:alamat,
details_attributes: [:id, :number, :_destroy])
end
def find_contact
@contact = Contact.find(params[:id])
end
end
であり、これは私のシンプルなコントローラではないthatsのテスト
require 'rails_helper'
RSpec.describe ContactsController do
describe "Contact" do
it "succesfully create the contact" do
contact = FactoryGirl.create(:contact)
get :index
# byebug
expect(assigns(:contacts)).to eq([contact])
end
end
end
はさえ、私はまだ同じ価値を持っassigns(:contacts)
assigns(:contact)
に変更します。だから私は間違っているのですか? 親切にしても、私はまだ 同じ値を得たassigns(:contact)
にassigns(:contacts)
を変更するには、この、大きな感謝
使用しているRailsのバージョンを指定してください。 –
Rails 5.1.4 sir – Willy
あなたのテストではなく、あなたがコントローラのインデックスメソッドに '@contact'変数を持っています。 – Surya