简体   繁体   English

使用rspec存根数组响应

[英]Stubbing array responses with rspec

I'm currently working on spec'ing out a rake task that's meant to populate a database using the google_drive gem. 我目前正在制定一项rake任务,该任务旨在使用google_drive gem填充数据库。 The gem allows users to access a spreadsheet ws like so: 宝石允许用户访问电子表格ws像这样:

# Gets content of A2 cell.
p ws[2, 1]  #==> "hoge"

The function that I'm trying to spec looks like this: 我尝试指定的功能如下所示:

def get_tags(ws, row)
  tags = []
  [1, 21, 22, 23, 26, 29, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40].each do |col|
    tags << ws[row, col] unless ws[row, col].blank?
  end
  tags
end

Fairly self explanatory, it gets passed a row and then adds the column data to tags for each column specified in the array. 不言而喻,它传递了一行,然后将列数据添加到数组中指定的每个列的tags中。

My ws array consists of this: 我的ws数组包含以下内容:

ws = [[nil, 'Good', 'Tultex', '0241', 'Men\s Blend Tee',
            'XS - 3XL', '$3.40', '$4.50', '$4.50', '--', '--', '--',
            'TSC', 'y', 'Adult Unisex', 'Tee Shirt', 'Short Sleeve',
            'Crewneck', '3.2', 'Cotton/Poly (35/36)', 'Fitted', '11',
            '0240 (ladies)', '', 'Std Fitted Crew', 'Tees and Tanks',
            'Nicer - Fashion Fitted', 'Blend', '', '', '', 'Fashionable',
            '', '', '']]

Therefore, I need get_tags to return this: 因此,我需要get_tags返回:

resultant = ['Good', 'Tee Shirt', 'Short Sleeve', 'Crew Neck',
             'Standard', '', 'Tees and Tanks', 'Least Expensive',
             'Regular Cotton', '', '', '', '', '', '', 'Least Expensive']

My problem is that I have no idea how I can spec an array to accept the type of indexing that ws does ( ws[val, val] ), since that's usually a range index. 我的问题是我不知道如何指定一个数组来接受ws的索引类型( ws[val, val] ),因为这通常是范围索引。 I tried creating a two dimensional array but that obviously didn't work. 我尝试创建一个二维数组,但这显然行不通。 Also tried stubbing it out by doing: 还尝试通过执行以下操作将其存根:

allow(ws).to receive(:[]) do |arg1, arg2|
  ws[arg1][arg2]
end

Which created an infinite loop of stubbiness, so then I tried storing ws into another array called temp and doing this (almost the same thing): 这创建了一个无限的顽固循环,因此我尝试将ws存储到另一个名为temp数组中,并执行此操作(几乎是同一件事):

allow(ws).to receive(:[]) do |arg1, arg2|
  temp[arg1][arg2]
end

But I still end up with the same infinite stubbing loop. 但是我仍然会遇到相同的无限存根循环。 Any advice on what steps to pursue next would be greatly appreciated! 任何有关下一步措施的建议都将不胜感激! Thanks in advance :) 提前致谢 :)

How about creating a class which handles the needed interface: 如何创建一个处理所需接口的类:

class DBStub
  def initialize(data_store)
    @data_store = data_store
  end

  def [](arg1, arg2)
    @data_store[arg1][arg2]
  end
end

ws = DBStub.new(temp)

What values do you expect there to be in ws? 您期望ws中包含哪些值? If you don't care about what the values are and just want to test that they're added you could do 如果您不关心值是什么,而只想测试是否添加了值,则可以执行

   allow(ws).to receive(:[]) { rand(1000) } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM