Skip to content

Instantly share code, notes, and snippets.

@tpendragon
Last active January 1, 2016 13:39
Show Gist options
  • Save tpendragon/8152480 to your computer and use it in GitHub Desktop.
Save tpendragon/8152480 to your computer and use it in GitHub Desktop.
Run options: include {:locations=>{"./spec/controllers/shelters_controller_spec.rb"=>[61]}}
SheltersController
Fails to PATCH update when submitting invalid data
"#<ActiveModel::Errors:0x00000005446ad8 @base=#<Shelter id: 1, name: nil, phone_number: \"606-555-1212\",
contact_person: \"Shelter Contact\", rescue_group_id: nil, created_at: \"2013-12-27 20:33:10\",
updated_at: \"2013-12-27 20:33:10\">, @messages={:name=>[\"can't be blank\"]}>"
"#<ActionDispatch::Flash::FlashHash:0x000000068fdd28 @discard=#<Set: {}>,
@flashes={:notice=>\"Successfully updated Shelter!\"}, @now=nil>"
fails to update a shelter with bad info
Finished in 0.78582 seconds
1 example, 0 failures
Randomized with seed 10114
class SheltersController < ApplicationController
def index
@shelters = Shelter.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @shelters }
end
end
def show
@shelter = Shelter.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @shelter }
end
end
def new
@shelter = Shelter.new
@shelter.build_address
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @shelter }
end
end
def create
@shelter = Shelter.new(params[:shelter])
respond_to do |format|
if @shelter.valid? == true && @shelter.save
flash[:notice] = 'Shelter was successfully created.'
format.html { redirect_to(@shelter) }
format.xml { render :xml => @shelter, :status => :created, :location => @shelter }
else
flash[:error] = 'OOPS! Shelter failed to create.'
format.html { render :action => "new" }
format.xml { render :xml => @shelter.errors, :status => :unprocessable_entity }
end
end
end
def update
@shelter = Shelter.find(params[:id])
respond_to do |format|
if @shelter.valid? == true && @shelter.update_attributes(params[:shelter])
flash[:notice] = 'Successfully updated Shelter!'
format.html { redirect_to(@shelter) }
format.xml { head :ok }
else
flash.now[:error] = "OOPS! Shelter could not be updated."
flash[:error] = "OOPS! Shelter could not be updated."
format.html { render :action => "edit" }
format.xml { render :xml => @shelter.errors, :status => :unprocessable_entity }
end
end
end
def edit
@shelter = Shelter.find(params[:id])
end
def destroy
@shelter = Shelter.find(params[:id])
@shelter.destroy
respond_to do |format|
flash[:notice] = 'Shelter successfully deleted!'
format.html { redirect_to(shelters_url) }
format.xml { head :ok }
end
end
private
def shelter_params
params.require(:shelter).permit(:name, :phone_number, :contact_person, :address_attributes[:id, :address1, :address2, :address3, :city, :state, :zip])
end
end
require 'spec_helper'
describe SheltersController do
subject(:shelter) { FactoryGirl.build(:shelter) }
describe "GET 'new'" do
it "returns http success" do
get 'new'
response.should be_success
end
end
describe "POST 'create'" do
it "creates a new shelter" do
expect {
permit_all_parameters do
post :create, {:shelter => FactoryGirl.attributes_for(:shelter), signed: true}, session
end
}.to change{Shelter.count}.by(1)
end
end
describe "GET 'index'" do
it "returns http success" do
get 'index'
response.should be_success
end
end
describe "GET 'show'" do
it "shows a shelter" do
shelter = FactoryGirl.build(:shelter)
shelter.address FactoryGirl.build(:address)
shelter.save
get :show, id: shelter.to_param
expect(response).to be_success
end
end
describe "GET 'edit'" do
it "edits a shelter" do
shelter.save
get :edit, id: shelter.to_param
response.should be_success
end
end
describe "PATCH 'update'" do
it "updates a Shelter" do
shelter.save!
shelter.update_attributes(name: "Some Updated Shelter")
patch :update, id: shelter.to_param
expect(flash[:notice]).to eq("Successfully updated Shelter!")
end
end
describe "Fails to PATCH update when submitting invalid data" do
it "fails to update a shelter with bad info" do
shelter.save!
patch :update, id: shelter.to_param, shelter: {name: nil}
expect(response).not_to be_success
expect(Shelter.find(shelter.id).updated_at).to eq shelter.updated_at
end
end
describe "DELETE 'destroy'" do
it "deletes a shelter" do
shelter.save!
delete :destroy, id: shelter.to_param
expect { change(Shelter, :count).by(-1) }
expect(response).to redirect_to(shelters_path)
expect(flash[:notice]).to eq("Shelter successfully deleted!")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment