Hommer Smith
Hommer Smith

Reputation: 27862

Rspec with input and output file

I have a class that processes a file and generates an output file from the contents of that input file.

My question is pretty straight: How should I test that out?

Say in the input line I have a line like this:

"I love pets 1"

And I need to test that in the output file there is a line that is like this:

"I love pets 2"

THanks

Upvotes: 1

Views: 331

Answers (1)

Victor Deryagin
Victor Deryagin

Reputation: 12225

You can use fixture files for example outputs and check content of output files (using File.read), but more testable approach would be to have class that accepts input as string and returns result as string (which would be straightforward to test), and specialized one that works with files:

class StringProcessor
  def initialize(input)
    @input = input
  end

  def output
    # process @input and return string
  end
end

class FileProcessor < StringProcessor
  def initialize(file)
    super(File.read file)
  end

  def output(file)
    File.open(file, 'w') do |file|
      file.puts super()
    end
  end
end

Upvotes: 1

Related Questions