【发布时间】:2019-01-22 00:13:33
【问题描述】:
我正在尝试为以下模型问题编写单元测试...
require 'active_support/concern'
module Streamable
extend ActiveSupport::Concern
def stream_query_rows(sql_query, options = 'WITH CSV HEADER')
conn = ActiveRecord::Base.connection.raw_connection
conn.copy_data("COPY (#{sql_query}) TO STDOUT #{options};") do
binding.pry
while row = conn.get_copy_data
binding.pry
yield row
end
end
end
end
到目前为止,我一直在与以下规范作斗争...
context 'streamable' do
it 'is present' do
expect(described_class.respond_to?(:stream_query_rows)).to eq(true)
end
context '#stream_query_rows', focus: true do
let(:sql_query) { 'TESTQRY' }
let(:sql_query_options) { 'WITH CSV HEADER' }
let(:raw_connection) do
Class.new do
def self.copy_data(args)
yield
end
def self.get_copy_data
return Proc.new { puts 'TEST' }
end
end
end
before do
allow(ActiveRecord::Base).to receive_message_chain(:connection, :raw_connection).and_return(raw_connection)
described_class.stream_query_rows(sql_query)
end
it 'streams data from the db' do
expect(raw_connection).to receive(:copy_data).with("COPY (#{sql_query}) TO STDOUT #{sql_query_options};")
end
end
end
虽然我可以让第一个期望通过,这意味着,我可以触发第一个 binding.pry,无论我尝试什么,我似乎都无法通过第二个。
这是错误...
LocalJumpError:
no block given (yield)
我只是想对此进行单元测试,理想情况下不打数据库,只测试对象的通信。这也可以并且将在许多模型中用作流数据的选项。
参考文章:https://shift.infinite.red/fast-csv-report-generation-with-postgres-in-rails-d444d9b915ab
是否有人对如何完成此存根和/或调整规范有一个指示,以便我涵盖以下块?
while row = conn.get_copy_data
binding.pry
yield row
end
回答
在查看了下面的 cmets 和建议后,我能够重构规范,现在有 100% 的覆盖率。
context '#stream_query_rows' do
let(:sql_query) { 'TESTQRY' }
let(:sql_query_options) { 'WITH CSV HEADER' }
let(:raw_connection) { double('RawConnection') }
let(:stream_query_rows) do
described_class.stream_query_rows(sql_query) do
puts sql_query
break
end
end
before do
allow(raw_connection).to receive(:copy_data).with("COPY (#{sql_query}) TO STDOUT #{sql_query_options};"){ |&block| block.call }
allow(raw_connection).to receive(:get_copy_data).and_return(sql_query)
allow(ActiveRecord::Base).to receive_message_chain(:connection, :raw_connection).and_return(raw_connection)
end
it 'streams data from the db' do
expect(raw_connection).to receive(:copy_data).with("COPY (#{sql_query}) TO STDOUT #{sql_query_options};")
stream_query_rows
end
it 'yields correct data' do
expect { stream_query_rows }.to output("#{sql_query}\n").to_stdout_from_any_process
end
end
【问题讨论】:
标签: ruby-on-rails postgresql rspec tdd