1

私のコントローラーでは、ユーザーが新しい投稿を作成すると、新しく作成された投稿を含むページにリダイレクトされます。このリダイレクトをカバーするためにrspecでテストを作成したいのですが、問題が発生しています。具体的には、refirst_to引数に何を書くべきか知りたいです。以下はコントローラーコードです。

def create
@micropost = Micropost.new(params[:micropost])
 respond_to do |format|
  if @micropost.save
    format.html {redirect_to @micropost}
  else
    format.html {render action: 'edit'} 
  end
end
end

これがrspecテストです...

before do
  @params = FactoryGirl.build(:micropost)
end

it "redirects to index" do
  #clearly @params.id doesn't work. its telling me instead of a redirect im getting a 
  #200
  #response.should redirect_to(@params.id)
end
4

1 に答える 1

1

@paramsが有効なマイクロポストを作成すると仮定します(そうでない場合、.saveは失敗し、:editをレンダリングします)...

it "redirects to index on successful save" do
  post :create, :micropost => @params.attributes
  response.should be_redirect
  response.should redirect_to(assigns[:micropost])
end

it "renders :edit on failed save" do
  post :create, :micropost => {}
  response.should render ... # i don't recall the exact syntax...
end
于 2012-11-02T19:29:49.590 に答える