...

/

RSpec and Rails Features

RSpec and Rails Features

Learn more about the test file and the test file's features in the test.

The project_spec.rb file uses four basic RSpec and Rails features:

  1. It requires rails_helper.
  2. It defines a test suite with RSpec.describe.
  3. It creates an RSpec example with it.
  4. It specifies a particular state with expect.

Here we can see spec/models/project_spec.rb file:

Press + to interact
require "rails_helper"
RSpec.describe Project do
it "considers a project with no tasks to be done" do
project = Project.new
expect(project.done?).to be_truthy
end
end

Let’s discuss each feature one-by-one:

spec_helper.rb file

On the first line, the file rails_helper, which contains a Rails-related setup typical to all tests, is required. The rails_helper file, in turn, requires a file named spec_helper, which contains a non-Rails RSpec setup.

Note: We’ll peek into that file in the next chapter when we learn about more Rails-specific test features.

RSpec.describe

The RSpec.describe* method is used online. In RSpec, the describe method defines a suite of specs that can share a common setup. The first argument to describe is either a class name or a string. The first argument documents what the test suite is supposed to cover. We can then pass an optional number of metadata arguments, of which there are none at the moment. The metadata is used to ...