3

I'm new to both ruby and rspec and I'm wondering how to test a class that must parse a file. Here is the function I plan to test:

def self.get_description(event_code)
  # search through a file for the event_code and return
  # the associated description field
end

In production I'll hard code the file's path. How should I control the file contents from rspec?

jlunavtgrad
  • 997
  • 1
  • 11
  • 21

1 Answers1

2

Create a few specs which parse Tempfiles that you create containing valid and invalid input and ensure that the parse function returns the expected results (or exceptions).

maerics
  • 151,642
  • 46
  • 269
  • 291
  • Thanks for posting that link. So what is the best way to have my class open the temp file while it's being tested, but open the actual file while running? – jlunavtgrad May 09 '12 at 16:41
  • You'll need to parameterize the name of the file that gets parsed so that it can be overridden per environment; consider this approach: http://stackoverflow.com/questions/1450285/how-to-define-custom-configuration-variables-in-rails – maerics May 09 '12 at 17:05