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

/我做了一个手掌。

在 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 回答