0

私はこの工場を持っています:@host = FactoryGirl.create(:host)

このテストは合格です:

it 'should update and redirect to the show page' do                                  
  new_attr = @host.attributes.merge("hostname" => "New_name")             
  put 'update', id: @host, host: new_attr              
  response.should redirect_to(host_path(@host))                           
end¬   

しかし、これは失敗します:

  it 'should fail on validation for empty hostname and redirect to edit' do                                           
    bad_attr = @host.attributes.merge("hostname" => "")                     
    put 'update', id: @host, host: bad_attr                                 
    response.should redirect_to(edit_host_path(@host))                
  end 

このエラーの場合:

   1) HostsController POST update failure should redirect to edit
         Failure/Error: response.should redirect_to(edit_host_path(@host))
           Expected response to be a redirect to <http://test.host/hosts/1/edit> but was a redirect to <http://test.host/hosts/1>
         # ./spec/controllers/hosts_controller_spec.rb:127:in `block (4 levels) in <top (required)>'

私のHostsControllerの#updateでは、空のホスト名で検証に失敗したホストオブジェクトはになっていredirect_to edit_host_path(@host)ます。これが私のHostsController#updateです

def update                                                                    
  @host = Host.find(params[:id])                                              
  if @host.save                 
    flash[:notice] = 'Host was successfully updated.'                         
    redirect_to host_path(@host)                                              
  else¬                                                                        
    redirect_to edit_host_path(@host)    
  end
end            

コンソールで属性を保存および更新してみましたが、検証は機能します。では、なぜこのエラーなのですか?何か案は?Rspecは、属性の悪いファクトリオブジェクトが検証に合格していると言っているようですが、モデルは正常に検証されます。ありがとうございました。

4

1 に答える 1

1

/meはフェイスパームを行います。

HostsController#updateでは、これは次のとおりです。

@host = Host.find(params[:id])
@host.save

これである必要があります:

@host = Host.find(params[:id])
@host.update_attributes(params[:host])
于 2012-12-08T06:58:36.213 に答える