3

I have a test that returns TypeError: no impliciit conversion of String into Array, when it hits a certain section of my code. If I run the code outside of rspec it runs just fine, so I'm not sure why this is happening.

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
....

It's testing the following code

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

The type error is coming from client.sizes.all.each. The code runs fine, I only get the type error when it's from rspec.

1
  • Can you make a SSCCE for the error? With all of the special classes and requires there's no way for me to test your code for myself. Commented Nov 9, 2014 at 1:24

1 Answer 1

5

Change

size_list << size.slug.to_s

To

size_list << [size.slug.to_s]

In my case, the error occurred because the value I had put to array doesn't have [], so I wrap it with [] and it works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.