Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/9718479 to your computer and use it in GitHub Desktop.
Save JoshCheek/9718479 to your computer and use it in GitHub Desktop.
require 'active_record'
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
ActiveRecord::Schema.define do
self.verbose = false
create_table(:invoices) { |t| t.float :amount }
end
class Invoice < ActiveRecord::Base
def self.total_outstanding
sum :amount
end
end
describe Invoice do
specify '.total_outstanding is the sum of the invoice amounts' do
expect(Invoice.total_outstanding).to eq 0
Invoice.create! amount: 1
expect(Invoice.total_outstanding).to eq 1
Invoice.create! amount: 2.2
expect(Invoice.total_outstanding).to eq 3.2
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment