Skip to content

Instantly share code, notes, and snippets.

@supernullset
Created December 12, 2012 21:40
Show Gist options
  • Save supernullset/4271872 to your computer and use it in GitHub Desktop.
Save supernullset/4271872 to your computer and use it in GitHub Desktop.
redirect_test
def create
# generic file specific code
if files = params.fetch(:flu_vaccine){{}}.fetch(:generic_files){nil}
@file_uploads = GenericFile.new files if files.fetch(:name) != ""
end
params.fetch(:flu_vaccine){{}}.delete :generic_files
# vaccine specific code
@vaccine = FluVaccine.new(params.fetch(:flu_vaccine){{}})
if @file_uploads
@vaccine.generic_files << @file_uploads
saved = ->(){@vaccine.save && @file_uploads.save}
else
saved = ->(){@vaccine.save}
end
redirect_to admin_flu_vaccine_path(@vaccine) and return if saved.call
render :form
end
# test
describe "POST 'create'" do
context "with invalid params" do
before { post :create }
it "doesnt pass validations" do
response.should render_template("admin/general_vaccines/form")
end
end
context "with valid params" do
before { post :create, post: attributes_for(:general_vaccine) }
it "redirects to show action" do
response.should redirect_to(admin_flu_vaccines_path(assigns[:vaccine][:_id]))
end
end
end
# returns
# Failure/Error: response.should redirect_to(admin_flu_vaccines_path(assigns[:vaccine][:_id]))
# Test::Unit::AssertionFailedError:
# Expected response to be a <:redirect>, but was <200>.
# (eval):2:in `flunk'
# ./spec/controllers/admin/flu_vaccines_controller_spec.rb:54:in `block (4 levels) in <top (required)>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment