2017-09-26 1 views
0

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)を変更するには、この、大きな感謝

+0

使用しているRailsのバージョンを指定してください。 –

+0

Rails 5.1.4 sir – Willy

+0

あなたのテストではなく、あなたがコントローラのインデックスメソッドに '@contact'変数を持っています。 – Surya

答えて

1

を答えてください。だから私は間違っているのですか?

assignsassert_templateあなたは承認チェック@contactに代入する前に

authorize! :index, Contact 

を持って削除して

Source

0

レール5のgemに抽出されています。

しかし、どのような方法でも要求しているユーザーにアクセス許可を与えるためのテストはありません。

このようなエラーを検出するために表示するテストと並行して追加のテストを行うのはおそらく意味があります。例:

it "returns 200 (OK)" do 
    get :index 
    expect(response.response_code).to eq(200) 
end 
関連する問題