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 将创建一个有效的 Micropost(否则 .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 回答