概要
visitで訪れたpageの中に、新規登録という文字列があるかどうかを確認するためシステムスペックのテストを実行したところエラーが発生
テストコード
it '正しい情報を入力すれば、新規登録できる' do
# トップページに遷移
visit root_path
# ヘッダーに新規登録ボタンがある
expect(page).to have_content('新規登録')
エラー文
Failure/Error: expect(page).to have_content('新規登録')
expected to find text "新規登録" in "Blocked host: www.example.com\nTo allow requests to www.example.com make sure it is a valid hostname (containing only numbers, letters, dashes and dots), then add the following to your environment configuration:\nconfig.hosts << \"www.example.com\""
解決手順
initializers
内にblocked_hosts.rb
というファイルを作成
以下を記載
config/initializers/blocked_hosts.rb
Rails.application.configure do
config.hosts << "www.example.com"
end
するとエラーが変わりました
Failure/Error: <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
ActionView::Template::Error:
Webpacker can't find application.js in /One_Trip/public/packs-test/manifest.json. Possible causes:
1. You want to set webpacker.yml value of compile to true for your environment
unless you are using the `webpack -w` or the webpack-dev-server.
2. webpack has not yet re-run to reflect updates.
3. You have misconfigured Webpacker's config/webpacker.yml file.
4. Your webpack configuration is not creating a manifest.
Your manifest contains:
{
}
webpacker.yaml の test の設定を development に合わせてみたら成功するようになりました。
↓デフォルト設定
webpacker.yaml
test:
<<: *default
compile: true
# Compile test packs to a separate directory
public_output_path: packs-test
developmentと全く同じ設定に変更
test:
<<: *default
compile: true
# Reference: https://webpack.js.org/configuration/dev-server/
dev_server:
https: false
host: localhost
port: 3035
public: localhost:3035
hmr: false
# Inline should be set to true if using HMR
inline: true
overlay: true
compress: true
disable_host_check: true
use_local_ip: false
quiet: false
pretty: false
headers:
'Access-Control-Allow-Origin': '*'
watch_options:
ignored: '**/node_modules/**'
以上でエラーが解消されテストが通るようになりました。