Reputation: 1834
I have this factory: @host = FactoryGirl.create(:host)
This test passes:
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¬
But this one fails:
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
With this error:
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)>'
In the #update of my HostsController, host objects that fail validation with empty hostnames are supposed to redirect_to edit_host_path(@host)
. Here is my 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
I've played with saving and updating attributes in the console and my validations work. So why this error? Any ideas? Rspec seems to be saying that my factory objects with bad attributes are passing validation, but my model validates fine. Thank you.
Upvotes: 0
Views: 283
Reputation: 1834
/me does a face-palm.
In HostsController#update, this:
@host = Host.find(params[:id])
@host.save
Should be this:
@host = Host.find(params[:id])
@host.update_attributes(params[:host])
Upvotes: 1