2
votes

I followed the intro in the rspec page, and then added a test task to my rakefile to make a simple file read test:

#Rakefile
task default: %w[test]

task :test do
  ruby "spec/file_reader_spec.rb"
end

#spec/file_reader_spec.rb
require './lib/filereader'
require 'rspec'

RSpec.describe "file_reader" do
    context "with sample test input file" do

        it "reads a file and prints its contents" do
            @file_reader = FileReader.new
            expect(@file_reader.input_file('./files/test_input.txt')).to eq ('file text')
        end
    end
end

But when I run the rake command, it outputs nothing, only one line showing that the spec file was executed:

$rake
/Users/mrisoli/.rvm/rubies/ruby-2.1.1/bin/ruby spec/file_reader_spec.rb

Why is it not outputing the test described?

1

1 Answers

2
votes

You are running the spec with ruby, not rspec. This is why you don't see any output, your test will run as if it is a regular ruby script.

Change your Rakefile to run rspec instead:

begin
  require 'rspec/core/rake_task'

  RSpec::Core::RakeTask.new(:spec)

  task :default => :spec
rescue LoadError
  puts "RSpec is not installed!"
end

More details here.

UPDATE

If you want to pass parameters to rspec, you can do it like this:

RSpec::Core::RakeTask.new(:spec) do |t|
  t.rspec_opts = "--format documentation"
end

This will run the spec with the format as documentation.


Off topic

When you are describing a class, best practices say you should pass the class to describe method instead of a string. This way your code looks cleaner and rspec will take care of the instantiating it by itself (the instance will be available as subject).

For example:

RSpec.describe FileReader do
  context "with sample test input file" do
    it "reads a file and prints its contents" do
        expect(subject.input_file('./files/test_input.txt')).to eq ('file text')
    end
  end
end