Skip to content

Instantly share code, notes, and snippets.

@morhekil
Forked from anonymous/index.html.haml
Created February 10, 2009 17:58
Show Gist options
  • Save morhekil/61499 to your computer and use it in GitHub Desktop.
Save morhekil/61499 to your computer and use it in GitHub Desktop.
%h1 Listing invoices
%table
%tr
%th Amount
%th Info
- for invoice in @invoices
%tr
%td= invoice.amount
%td= invoice.info
%td= link_to 'Show', invoice
%td= link_to 'Edit', edit_invoice_path(invoice)
%td= link_to 'Destroy', invoice, :confirm => 'Are you sure?', :method => :delete
%br
= link_to 'New invoice', new_invoice_path
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe InvoicesController do
before(:each) do
@invoice = Factory(:invoice)
end
describe "responding to GET index" do
it "should expose all invoices as @invoices" do
get :index
assigns[:invoices].should == [@invoice]
end
describe "with mime type of xml" do
it "should render all invoices as xml" do
request.env["HTTP_ACCEPT"] = "application/xml"
Invoice.should_receive(:find).with(:all).and_return(invoices = mock("Array of Invoices"))
invoices.should_receive(:to_xml).and_return("generated XML")
get :index
response.body.should == "generated XML"
end
end
end
describe "responding to GET show" do
it "should expose the requested invoice as @invoice" do
get :show, :id => @invoice.id
assigns[:invoice].should == @invoice
end
describe "with mime type of xml" do
it "should render the requested invoice as xml" do
request.env["HTTP_ACCEPT"] = "application/xml"
Invoice.should_receive(:find).with("37").and_return(@invoice)
@invoice.should_receive(:to_xml).and_return("generated XML")
get :show, :id => "37"
response.body.should == "generated XML"
end
end
end
describe "responding to GET new" do
it "should expose a new invoice as @invoice" do
invoice = Factory.build(:invoice)
Invoice.should_receive(:new).and_return(invoice)
get :new
assigns[:invoice].should equal(invoice)
end
end
describe "responding to GET edit" do
it "should expose the requested invoice as @invoice" do
get :edit, :id => @invoice.id
assigns[:invoice].should == @invoice
end
end
describe "responding to POST create" do
before(:each) do
@new_invoice = Factory.build(:invoice)
end
describe "with valid params" do
it "should expose a newly created invoice as @invoice" do
Invoice.should_receive(:new).with({'these' => 'params'}).and_return(@new_invoice)
post :create, :invoice => {:these => 'params'}
assigns(:invoice).should equal(@new_invoice)
end
it "should redirect to the created invoice" do
Invoice.stub!(:new).and_return(@new_invoice)
post :create, :invoice => {}
response.should redirect_to(invoice_url(@new_invoice))
end
end
describe "with invalid params" do
before(:each) do
@new_invoice.stub!(:save => false)
end
it "should expose a newly created but unsaved invoice as @invoice" do
Invoice.stub!(:new).with({'these' => 'params'}).and_return(@new_invoice)
post :create, :invoice => {:these => 'params'}
assigns(:invoice).should equal(@new_invoice)
end
it "should re-render the 'new' template" do
Invoice.stub!(:new).and_return(@new_invoice)
post :create, :invoice => {}
response.should render_template('new')
end
end
end
describe "responding to PUT update" do
before(:each) do
Invoice.should_receive(:find).with(@invoice.id.to_s).and_return(@invoice)
end
it "should update the requested invoice" do
@invoice.should_receive(:update_attributes).with({'these' => 'params'})
put :update, :id => @invoice.id, :invoice => {:these => 'params'}
end
describe "with valid params" do
before(:each) do
@invoice.stub!(:update_attributes => true)
end
it "should expose the requested invoice as @invoice" do
put :update, :id => @invoice.id
assigns(:invoice).should equal(@invoice)
end
it "should redirect to the invoice" do
put :update, :id => @invoice.id
response.should redirect_to(invoice_url(@invoice))
end
end
describe "with invalid params" do
before(:each) do
@invoice.stub!(:update_attributes => false)
end
it "should expose the invoice as @invoice" do
put :update, :id => @invoice.id
assigns(:invoice).should equal(@invoice)
end
it "should re-render the 'edit' template" do
put :update, :id => @invoice.id
response.should render_template('edit')
end
end
end
describe "responding to DELETE destroy" do
it "should destroy the requested invoice" do
Invoice.should_receive(:find).with(@invoice.id.to_s).and_return(@invoice)
@invoice.should_receive(:destroy)
delete :destroy, :id => @invoice.id
end
it "should redirect to the invoices list" do
delete :destroy, :id => @invoice.id
response.should redirect_to(invoices_url)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment