1

私はFriendlyId宝石を使用していますが、レコードが保存されるとスラグが生成されます。それはうまくいきます。

次のようにスラッグにルーティングできます: places/house-of-dead、完璧です。しかし、テストに関しては問題があります。

作成するパラメーターをスタブ化するために使用FactoryGirlしていますが、生成されたスラッグを取得して、ルートがリダイレクトされるかどうかをアサートする方法がわかりません。テストは以下と ( ???? should be the generated slug).:

    context '#create' do
      it 'should redirect to place after created' do
        place = FactoryGirl.attributes_for(:place)

        post :create, { place: FactoryGirl.attributes_for(:place) }

        response.should redirect_to places_path(slug: ????)
      end
    end

私のコントローラーはシンプルです:

def create
    @place = Place.new(params[:place])

    # setting up who owns this place
    @place.user = current_user

    if @place.save
      flash[:success] = "Place created."

      redirect_to @place
    else
      render :new
    end
end

ご覧のとおり、使用するredirect_to @placeと「place/:slug」にリダイレクトされますが、そのリダイレクトをテストするにはどうすればよいでしょうか?

また、私のテスト方法は正しいですか?

ありがとう。

4

1 に答える 1

2

あなたはほとんど自分自身に答えました:

response.should redirect_to places_path(assigns(:place))

またはスラッグを使用する必要がある場合:

response.should redirect_to places_path(slug: assigns(:place).slug)

の詳細については、 https://github.com/rspec/rspec-rails#assignsを参照してくださいassigns()

そして、この行

place = FactoryGirl.attributes_for(:place)

placeテストの残りの部分で変数を使用しないため、冗長です。

于 2012-11-07T15:26:25.610 に答える