ruby on rails - RSpec: Can't seem to stub ActiveRecord::QueryMethods#select -
i'm trying write test computes average of many db records without overhead of reading/writing db.
here method:
class average::route def self.daily_flight_count route.active.map |r| times = r.flights.select('distinct depart_time').count (times / 7.0).round end.average end end
the method gets number of unique depart times per day routes , averages average route's daily flight count.
i'm setting test so:
require 'rails_helper' describe average::route describe '.daily_flight_count' 'returns average daily flight count of routes' flights1 = (1..7).inject([]) |arr, n| arr << double(flight, depart_time: time.now + n.days) end flights2 = (1..7).inject([]) |arr, n| arr << double(flight, depart_time: time.now + n.days) arr << double(flight, depart_time: time.now + n.days) end r1 = double(route, flights: flights1) r2 = double(route, flights: flights2) allow(route).to receive(:active).and_return([r1, r2]) allow(flights1).to receive(:select).and_return(flights1.map(&:depart_time)) allow(flights2).to receive(:select).and_return(flights2.map(&:depart_time)) expect(average::route.daily_flight_count).to eq(1) end end end
based on how r1
, r2
doubles of route set return arrays of flight doubles, ran problem since array responds #select
differently activerecord relation (how behave), tried stub flights1
, flights2
arrays return mapped depart times. however, when run test still error:
failures: 1) average::route.daily_flight_count returns average daily flight count of routes failure/error: expect(average::route.daily_flight_count).to eq(1) argumenterror: wrong number of arguments. expected 0, got 1. # ./app/models/average/route.rb:14:in `block in daily_flight_count' # ./app/models/average/route.rb:13:in `map'
the stubs flights1
, flights2
don't seem working. error still calling activerecord #select
method on array of doubles , failing because array#select
takes block , code gives argument (since in production collection relation).
any ideas on how working?
you'll need specify parameters you're expecting
allow(flights1).to receive(:select).with('distinct depart_time').and_return(flights1.map(&:depart_time))
Comments
Post a Comment