In greeter.rb (wherever that goes in your project):
class Greeter
def greet
"Hello, world!"
end
end
In spec/greeter_spec.rb:
require_relative '../greeter.rb'
RSpec.describe Greeter do
describe '#greet' do
it "says hello" do
expect(Greeter.new.greet).to eq("Hello, world!")
end
end
end
So our file structure looks like:
$ tree .
.
├── greeter.rb
└── spec
└── greeter_spec.rb
1 directory, 2 files
Output
$rspec greeter_spec.rb
Finished in 0.00063 seconds (files took 0.06514 seconds to load)
1 example, 0 failures
In RSpec terminology, the file is a "spec" of Greeter
and the it
block is an "example". The line with expect
is an expectation. If the expectation is met, nothing happens and the test passes. If not, the test fails.
This example also shows that describe
blocks can be nested, in this case to convey that the greet
method is part of the Greet
class. The #
in #greet
is only a convention to show that greet
is an instance method (as opposed to '.' for a class method). RSpec doesn't interpret the string at all, so you could use a different string or omit that describe
block entirely.