Topic: RSpec testing module methods

Hello,

Working on testing a pure-ruby application I have with rspec. I have some methods that are in a module, not a class. When I run my rspec code, it cannot find the methods I test.

# spec/copyprocess/copyprocess_spec.rb

require 'spec_helper'

module CopyProcess
  describe CopyProcess do
    describe "#contains_valid_headers" do
      it "should return false if invalid" do
        CopyProcess::contains_valid_headers('').should == false
      end
    end
  end
end

The above contains_valid_headers method cannot be found.

I declare it in the following file:

# lib/copyprocess.rb

require 'copyprocess/content_element'
require 'copyprocess/content_row'

module CopyProcess  
  def get_inner_index(value, arr)
    idx = nil
    arr.each_with_index do |e, i|
      if e[0] == value
        idx = i
      end
    end
    return idx
  end
  
  def includes_inner?(value, arr)
    bool = false
    arr.each { |e| bool = true if e[0] == value  }
    return bool
  end
  # This method confirms the content of the text file has valid HEADER values.
  # @param [String] value - a string containing the contents from the text file
  # @return [Array]
  def contains_valid_headers(value)
    split_value = value.split(/\n/)
    if(split_value[0].index('/*').nil? || split_value[4].index('*/').nil?)
      return false
    else
      start_index = value.index('/*')
      return false if start_index.nil?
      end_index = value.index('*/', start_index+2)
      return false if end_index.nil?
      value = value[start_index+2..end_index-1]
    
      type_i = value.index('Type:')
      layer_i = value.index('Layer:')
      variation_i = value.index('Variation:')
      return false if type_i.nil? || layer_i.nil? || variation_i.nil?
      # sets the type, layer, variation as an array
      headers = [value[type_i+5..layer_i-1], value[layer_i+6..variation_i-1], value[variation_i+10..value.size]]
      # return it with each value stripped of nextline characters and extra white space
      return headers.collect { |c| c.gsub(/\n|\t/, '').strip }
    end
  end

 # ...
  
  
  class CopyFile
    #...
  end
  # end of CopyFile class
end

I have require 'copyprocess'
in the spec_helper file


Any ideas as to why that is? Feel free to view my full code and structure: https://github.com/agmcleod/Copy-Proces … perimental

Re: RSpec testing module methods

I think you refer to the method like MyModule::my_method in case it is defined as def self.my_method. In your case I think you probably have to mix your module into test class to test it... Right below the describe do include CopyProcess and refer to methods as they were local...

Re: RSpec testing module methods

That did the trick, thanks