2012-12-08 58 views
0

我有這樣的工廠:@host = FactoryGirl.create(:host)Rspec的驗證測試失敗的 '把' 更新

該測試通過:

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)。這裏是我的主機控制器#更新

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似乎在說我的具有不良屬性的工廠對象正在通過驗證,但我的模型驗證正確。謝謝。

+1

您可以發佈您相關的控制器代碼? –

回答

1

/我做了一個臉掌。

在HostsController#更新,這一點:

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

應該是這樣的:

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