ruby on rails - Using RSpec to test a method relying on an external Cassandra call -


i trying improve test coverage on activerecord model creates csv file querying cassandra database. use rspec. i'm having hard time figuring out how test cassandra_file method shown below, since calls cdbh (also shown below), creates live connection cassandra database.

i tried doing like:

it 'copys cassandra'   cdbh = cassandra.stub(:connect)   date = '2013/12/27'    expect(device).to receive(:save_csv).with(date, cdbh.execute(options))   device.raw_file(date.new(2013,12,27)) end 

but error:

failures:    1) device raw_file generic device copys cassandra      failure/error: unable find matching line backtrace      nomethoderror:        undefined method `stub' cassandra:module 

i looked @ other questions dealing stubbing api calls, of suggested gems vcr or webmock seem aren't useful specific use case, since i'm not trying replicate http request. there better way test this? that's going reasonable test, or more trouble it's worth?

thanks in advance help!

model methods:

def cassandra_file(date)   if customer.name.downcase == 'customer name'     q = 'select * readings device = ? , date in (?, ?, ?)'     return save_csv(              date,              cdbh.execute(q, guid, (date.to_date - 1).iso8601, date.to_date.iso8601, (date.to_date + 1).iso8601))   else     q = 'select * readings device = ? , date = ?'     return save_csv(              date,              cdbh.execute(q, guid, date.to_date.iso8601))   end end  ...  def cdbh   return unless env['cassandra_hosts']   @cdbh ||= cassandra.connect(hosts: env['cassandra_hosts'].split(/,/)).connect('hurricane') end 

in opinion you're on right track stubbing--model/unit tests "should" test message-passing (while database interaction covered integration/acceptance tests).

however think method, while not overly complicated, trying much, , impacting ability write clean tests. (to me code saying "where stick stub?," , kind of hammering in there!)

one simple recommendation might extract methods or classes, seems cassandra_file (raw_file?) has multiple inputs/outputs or side effects, , has become bit tangled.

it brings mind single responsibility principle solid fame (though can't described of methods in ruby). i'm seeing conditional query generation, database (external system) call, , file output. though it's relatively short method, there's still lot going on!

one current school of thought has models handling none of this--it's poros, "outsourced" /lib or elsewhere, while letting activerecord models models. (so skinny controllers/fat models considered first step, , may next.)

if have time , inclination i'd recommend spending more time researching topic, there's number of articles interesting presentations ruby conferences discussing it. (at moment i'm digging sandi metz.)

if not, i'd you're close, maybe extract few methods suggested , simplify model bit make testing little easier on yourself.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -