Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What is going on with this article?
@mugioka

『Ruby on Rails』統合テストにて発生したエラーの解消法

はじめに

統合テストにて、以前までは正常に動作していたコードがエラーを吐き出していました。その原因が意外な所だったので記事として残すことにしました(備忘録でもなんでもないけど)

エラー内容

ターミナル
Failures:

  1) user ユーザー登録前 会員情報入力ができるか
     Failure/Error: expect(page).to have_content("お届け先情報入力")

     Capybara::Ambiguous:
       Ambiguous match, found 2 elements matching visible xpath "/html"
     # ./spec/features/user_spec.rb:20:in `block (3 levels) in <top (required)>'

  2) user ユーザー登録前 ログインできないこと
     Failure/Error: expect(page).to have_content("アカウントをお持ちでない方はこちら")

     Capybara::Ambiguous:
       Ambiguous match, found 2 elements matching visible xpath "/html"
     # ./spec/features/user_spec.rb:29:in `block (3 levels) in <top (required)>'

  3) user ユーザー登録前 商品出品ボタンを押すとログインページに遷移すること
     Failure/Error: expect(page).to have_content("アカウントをお持ちでない方はこちら")

     Capybara::Ambiguous:
       Ambiguous match, found 2 elements matching visible xpath "/html"
     # ./spec/features/user_spec.rb:35:in `block (3 levels) in <top (required)>'

  4) user ユーザー登録後 ログインできているとヘッダーにマイページが表示されること
     Failure/Error: expect(page).to have_content "マイページ"

     Capybara::Ambiguous:
       Ambiguous match, found 2 elements matching visible xpath "/html"
     # ./spec/features/user_spec.rb:50:in `block (3 levels) in <top (required)>'

  5) user ユーザー登録後 ログアウトができること
     Failure/Error: expect(page).to have_content "新規会員登録"

     Capybara::Ambiguous:
       Ambiguous match, found 2 elements matching visible xpath "/html"
     # ./spec/features/user_spec.rb:56:in `block (3 levels) in <top (required)>'

  6) user ユーザー登録後 商品出品ボタンを押すと出品ページにに遷移するか
     Failure/Error: expect(page).to have_content("出品画像")

     Capybara::Ambiguous:
       Ambiguous match, found 2 elements matching visible xpath "/html"
     # ./spec/features/user_spec.rb:61:in `block (3 levels) in <top (required)>'

  7) Item#create can not save 画像(image)がない場合は登録できないこと
     Failure/Error: expect(item.errors[:images]).to include("を入力してください")
       expected [] to include "を入力してください"
     # ./spec/models/item_spec.rb:33:in `block (4 levels) in <top (required)>'

解決策

before

application.html.haml
!!!
%html
%head
%meta{content: "text/html; charset=UTF-8", "http-equiv": "Content-Type"}/
%title FreemarketSample68d
%script{src: "https://js.pay.jp/", type: "text/javascript"}
%meta{:content => "summary_large_image", :name => "twitter:card"}/
%meta{:content => "@yGZqMc5IqNbVsw0", :name => "twitter:site"}/
%meta{:content => "http://52.198.247.0/", :property => "og:url"}/
%meta{:content => "freemarket_sample", :property => "og:title"}/
%meta{:content => "チーム開発用のサンプルです!是非ご覧になってください!", :property => "og:description"}/
%meta{:content => "logo-white.png", :property => "og:image"}/
= favicon_link_tag 'favicon.ico'
= csrf_meta_tags
= csp_meta_tag
= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload'
= javascript_include_tag 'application', 'data-turbolinks-track': 'reload'
  %body
  = yield

after

application.html.haml
!!!
%html
  %head
    %meta{content: "text/html; charset=UTF-8", "http-equiv": "Content-Type"}/
    %title FreemarketSample68d
    %script{src: "https://js.pay.jp/", type: "text/javascript"}
    %meta{:content => "summary_large_image", :name => "twitter:card"}/
    %meta{:content => "@yGZqMc5IqNbVsw0", :name => "twitter:site"}/
    %meta{:content => "http://52.198.247.0/", :property => "og:url"}/
    %meta{:content => "freemarket_sample", :property => "og:title"}/
    %meta{:content => "チーム開発用のサンプルです!是非ご覧になってください!", :property => "og:description"}/
    %meta{:content => "logo-white.png", :property => "og:image"}/
    = favicon_link_tag 'favicon.ico'
    = csrf_meta_tags
    = csp_meta_tag
    = stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload'
    = javascript_include_tag 'application', 'data-turbolinks-track': 'reload'
  %body
    = yield

まとめ

beforeの状態でも、アプリケーションとしてはエラー発生はしなかったのですが統合テストだとエラーが発生。

理由としては、application.html.hamlにて規定ネストがされていないことが原因らしいです。
metaタグとか、favicon作るときはネストしとかないと他のとこでエラーが出ちゃうので気をつけよう!

1
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
mugioka
システム開発会社インターン中

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
1
Help us understand the problem. What is going on with this article?