Rspec 存根 Object.map()
Rspec Stubbing Object.map()
在我的 rails 控制器中,我正在尝试为方法 search_backups
编写测试。我遇到的问题是 devices_ids_from_elastic = ConfigTextSearch.search search_term
returns 什么都没有,因此测试在 devices_ids_from_elastic
上运行 .map
,这是错误的 #<String:0x007f8a90b67ca0>
。
如何删除 devices_ids_from_elastic.map()
来解决这个问题?
Failures:
1) ReportsController access control allows architects to search backups
Failure/Error: post 'search_backups'
NoMethodError:
undefined method `each_pair' for #<String:0x007f8a90b67ca0>
# ./app/controllers/reports_controller.rb:19:in `map'
# ./app/controllers/reports_controller.rb:19:in `elastic_mongo_lookup'
# ./app/controllers/reports_controller.rb:32:in `search_backups'
# ./spec/controllers/reports_controller_spec.rb:123:in `block (3 levels) in <top (required)>'
测试:
describe "controller method test" do
before do
allow(CSV).to receive(:generate).and_return("1234, blah")
stub_request(:get, "http://localhost:9200/mongo_index/config_files/_search?q=").
with(:headers => {'Expect'=>'', 'User-Agent'=>'Faraday v0.9.1'}).
to_return(:status => 200, :body => '{lots of json stuff in here }', :headers => {})
it "allows users to search backup log files" do
reports = double(ReportsController)
reports.stub(:map).and_return("help")
post 'search_backups'
end
控制器:
search_backups
def elastic_mongo_lookup(search_term)
devices_ids_from_elastic = ConfigTextSearch.search search_term
device_ids = devices_ids_from_elastic.map { |device| device._source.device_id }
csv_string = CSV.generate do |csv|
Device.where(:_id.in => device_ids).each do |device|
csv << [device.logical_name, device.primary_ip]
end
end
return csv_string
end
def search_backups
authorize! :read, :custom_report
csv_string = elastic_mongo_lookup params[:search_term]
if csv_string.blank?
flash[:notice] = "No results were found"
redirect_to reports_path
else
render text: "DeviceID, primary_ip\n" + csv_string
end
end#search_backups
试试这个:allow(ConfigTextSearch).to receive(:search).with({}).and_return([])
在我的 rails 控制器中,我正在尝试为方法 search_backups
编写测试。我遇到的问题是 devices_ids_from_elastic = ConfigTextSearch.search search_term
returns 什么都没有,因此测试在 devices_ids_from_elastic
上运行 .map
,这是错误的 #<String:0x007f8a90b67ca0>
。
如何删除 devices_ids_from_elastic.map()
来解决这个问题?
Failures:
1) ReportsController access control allows architects to search backups
Failure/Error: post 'search_backups'
NoMethodError:
undefined method `each_pair' for #<String:0x007f8a90b67ca0>
# ./app/controllers/reports_controller.rb:19:in `map'
# ./app/controllers/reports_controller.rb:19:in `elastic_mongo_lookup'
# ./app/controllers/reports_controller.rb:32:in `search_backups'
# ./spec/controllers/reports_controller_spec.rb:123:in `block (3 levels) in <top (required)>'
测试:
describe "controller method test" do
before do
allow(CSV).to receive(:generate).and_return("1234, blah")
stub_request(:get, "http://localhost:9200/mongo_index/config_files/_search?q=").
with(:headers => {'Expect'=>'', 'User-Agent'=>'Faraday v0.9.1'}).
to_return(:status => 200, :body => '{lots of json stuff in here }', :headers => {})
it "allows users to search backup log files" do
reports = double(ReportsController)
reports.stub(:map).and_return("help")
post 'search_backups'
end
控制器:
search_backups
def elastic_mongo_lookup(search_term)
devices_ids_from_elastic = ConfigTextSearch.search search_term
device_ids = devices_ids_from_elastic.map { |device| device._source.device_id }
csv_string = CSV.generate do |csv|
Device.where(:_id.in => device_ids).each do |device|
csv << [device.logical_name, device.primary_ip]
end
end
return csv_string
end
def search_backups
authorize! :read, :custom_report
csv_string = elastic_mongo_lookup params[:search_term]
if csv_string.blank?
flash[:notice] = "No results were found"
redirect_to reports_path
else
render text: "DeviceID, primary_ip\n" + csv_string
end
end#search_backups
试试这个:allow(ConfigTextSearch).to receive(:search).with({}).and_return([])