Skip to content

Instantly share code, notes, and snippets.

@dangalipo
Created September 30, 2011 03:35
Show Gist options
  • Select an option

  • Save dangalipo/1252577 to your computer and use it in GitHub Desktop.

Select an option

Save dangalipo/1252577 to your computer and use it in GitHub Desktop.
describe '#download_invoice' do
let(:user) { Factory.create(:user) }
context 'as a user with a valid token' do
before :each do
stub.proxy(User).where(anything)
stub(User).where(:api_token => 'token') { [ user ] }
@params = {:id => 1, :token => 'token'}
@invoice = Factory(:invoice)
mock(Invoice).find(1) { @invoice }
end
context "invoice exists" do
before :each do
@invoice.to_pdf
mock(File).exists?(anything) { true }
end
it 'should not call to_pdf on the invoice' do
dont_allow(@invoice).to_pdf
get :download_invoice, @params
end
end
context "invoice does not exist" do
before :each do
mock(File).exists?(anything) { false }
end
it 'should not call to_pdf on the invoice' do
@invoice.should_receive(:to_pdf)
get :download_invoice, @params
end
end
it 'should be successful' do
get :download_invoice, @params
response.should be_successful
end
it 'should send back a pdf' do
get :download_invoice, @params
response.content_type.should == 'text/pdf'
end
it 'should send the correct file' do
get :download_invoice, @params
response.headers["Content-Disposition"].should == "attachment; filename=\"invoice_#{@invoice.id}.pdf\""
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment