当我遇到代码的某个部分时,我有一个返回TypeError: no impliciit conversion of String into Array
的测试。如果我在rspec之外运行代码它运行得很好,所以我不确定为什么会发生这种情况。
require 'spec_helper'
require 'digital_ocean_size_list'
describe Chef::Knife::DigitalOceanSizeList do
subject { Chef::Knife::DigitalOceanSizeList.new }
let(:access_token) { 'FAKE_ACCESS_TOKEN' }
before :each do
Chef::Knife::DigitalOceanSizeList.load_deps
Chef::Config['knife']['digital_ocean_access_token'] = access_token
allow(subject).to receive(:puts)
end
describe "#run" do
it "should validate the Digital Ocean config keys exist" do
expect(subject).to receive(:validate!)
subject.run
end
....
它正在测试以下代码
require 'chef/knife/digital_ocean_base'
class Chef
class Knife
class DigitalOceanSizeList < Knife
include Knife::DigitalOceanBase
banner 'knife digital_ocean size list (options)'
def run
$stdout.sync = true
validate!
size_list = [
ui.color('Slug', :bold)
]
client.sizes.all.each do |size|
size_list << size.slug.to_s
end
puts ui.list(size_list, :uneven_columns_across, 1)
end
end
end
end
类型错误来自client.sizes.all.each。代码运行正常,我只从rspec获得类型错误。
答案 0 :(得分:4)
更改
size_list << size.slug.to_s
到
size_list << [size.slug.to_s]
在我的情况下,发生错误是因为我放入数组的值没有[]
,所以我用[]
包装它并且它有效。