Skip to content

Instantly share code, notes, and snippets.

@durexlw
Last active February 16, 2016 10:49
Show Gist options
  • Save durexlw/39b6cfd4319a3e69852d to your computer and use it in GitHub Desktop.
Save durexlw/39b6cfd4319a3e69852d to your computer and use it in GitHub Desktop.
Trying to test scope with factorygirl
# model/contract.rb
class Contract < ActiveRecord::Base
scope :koppen_per_maand, -> { select("jaar, maand, count(persnr) as aantal_koppen").group("maand_teller") }
def self.serialize_for_graph
ContractSerializer.koppen_per_maand(self.koppen_per_maand).to_json
end
end
# lib/contract_serializer.rb
def self.koppen_per_maand(contract)
contracts = contract.koppen_per_maand
contracts.map { |c| ["#{c.jaar}/#{c.maand}", c.aantal_koppen] }
end
class ContractSerializerKoppenPerMaand < ActiveSupport::TestCase
include FactoryGirl::Syntax::Methods
attr_reader :contract
setup do
@contract = create_list(:contract, 12)
@stats = ContractSerializer.koppen_per_maand(@contract)
end
it "should retreive the data in the form of an array" do
assert false
assert_includes @stats.keys, :aantal_koppen
end
end
# Running:
E
Finished in 0.167948s, 5.9542 runs/s, 0.0000 assertions/s.
1) Error:
ContractSerializerKoppenPerMaand#test_0001_should retreive the data in the form of an array:
NoMethodError: undefined method `koppen_per_maand' for #<Array:0x00000006277350>
app/lib/contract_serializer.rb:3:in `koppen_per_maand'
test/units/contract_serializer/koppen_per_maand.rb:10:in `block in <class:ContractSerializerKoppenPerMaand>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment