rinoue73
@rinoue73 (井上 涼太)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Ruby on Rails5 速習実施ガイド Rspecのエラーについて

解決したいこと

Ruby on Rails5 速習実施ガイドでタスク一覧のWebアプリをつくっています。
$bundle exec rspec spec/system/tasks_spec.rbを叩くとエラーが発生しました。
解決方法を教えて下さい。

発生している問題・エラー

An error occurred while loading ./spec/system/tasks_spec.rb. - Did you mean?
                    rspec ./spec/factories/tasks.rb

Failure/Error: require File.expand_path('../config/environment', __dir__)

LoadError:
  cannot load such file -- rexml/document
# ./config/application.rb:7:in `<top (required)>'
# ./config/environment.rb:2:in `require_relative'
# ./config/environment.rb:2:in `<top (required)>'
# ./spec/rails_helper.rb:5:in `require'
# ./spec/rails_helper.rb:5:in `<top (required)>'
# ./spec/system/tasks_spec.rb:1:in `require'
# ./spec/system/tasks_spec.rb:1:in `<top (required)>'
No examples found.


Finished in 0.00002 seconds (files took 0.4515 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

該当するソースコード

rspec ./spec/factories/tasks.rb
FactoryBot.define do
    factory :task do
      name { 'テストを書く' }
      description { 'RSpec&Capybara&FactoryBotを準備する' }
      user
    end
end

その他のコード

spec./spec/factories/users.rb
FactoryBot.define do
    factory :user do
        name { 'テストユーザー' }
        email { 'test1@example.com' }
        password { 'password' }
    end
end

その他のコード

spec/system/tasks_spec.rb
require 'rails_helper'

describe 'タスク管理機能', type: :system do
    describe '一覧表示機能' do
        before do
          user_a = FactoryBot.create(:user, name: 'ユーザーA', email: 'a@example.com')
          FactoryBot.create(:task, name: '最初のタスク', user: user_a)
        end

        context 'ユーザーAがログインしているとき' do
           before do
             visit login_path
             fill_in 'メールアドレス', with: 'a@example.com'
             fill_in 'パスワード', with: 'password'
             click_button 'ログインする'
           end

           it 'ユーザーAが作成したタスクが表示される' do
             expect(page).to have_content '最初のタスク'
           end
        end
    end
end

どの部分が間違っているか教えてください
よろしくお願い致します。

0

2Answer

rexml ライブラリが見つからないというエラーです。 Gemfile に gem 'rexml' と追記して bundle install を実行すれば解決します。

詳細はエラーメッセージ cannot load such file -- rexml/document でネットを検索すると出てくると思います。 rexml は Ruby 2 系まで Ruby 本体と一緒に配布されていましたが、 Ruby 3.0 からは gem に切り出されて別個にインストールするようになりました。

1Like

Your answer might help someone💌