0

「VIN」モデルでレコードを作成するためのフォームに入力した後、ユーザーが作成されたばかりのレコードの edit_path にリダイレクトされるという事実をテストするために、rspec 機能/リクエストを作成しようとしています。これが私の現在のテストです:

    describe "Add VIN" do
    it "can be added on the VIN listing page" do

        sign_in_as_a_valid_user
        vin = FactoryGirl.create(:vin)

        current_path.should == vins_path
        page.should have_field("vin_text_box")
        fill_in "vin_text_box", :with => "hello"
        click_button "generate_vin"
        current_path.should == edit_vin_path(v.id))   <---- #problem
    end
end

明らかな問題は、Factory で作成した vin が、リダイレクト先の vin とは関係がないことです。

問題は、この機能をリクエスト仕様でテストするにはどうすればよいかということです。

4

2 に答える 2

0

以下は RelishApp の例です: https://www.relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec

require "spec_helper"

describe "Widget management" do

it "creates a Widget and redirects to the Widget's page" do
    get "/widgets/new"
    expect(response).to render_template(:new)

    post "/widgets", :widget => {:name => "My Widget"}

    expect(response).to redirect_to(assigns(:widget))
    follow_redirect!

    expect(response).to render_template(:show)
    expect(response.body).to include("Widget was successfully created.")
  end

end

彼らのやり方を見てください。:show を :edit に変更し、コントローラーが正しい @vin を選択したかどうかではなく、ページの整合性をテストします。これは、FactoryGirl にその属性の 1 つに特定の値を渡すことで取得できます。

于 2013-02-03T19:40:18.023 に答える
0

データベースから最近作成されたものを取得できVinます。

# ...
click_button "generate_vin"

v = Vin.order("id desc").first
current_path.should eq(edit_vin_path(v.id))
于 2013-02-03T17:52:16.677 に答える