Youtubeのoneboxingのための簡単な単体テストの追加で問題が発生

こんにちは!

/shorts/ の YouTube リンクのサポートを追加したいです。

YoutubeOnebox クラスの修正は機能していますが、youtube_onebox.rb にテストを追加する必要がありますが、私のテストは機能していません。

“can parse youtube embed results” テスト と同様に、このコードを追加しました。

it "can parse youtube shorts results" do
  preview = expect(Onebox.preview('https://www.youtube.com/watch?v=wi2jAtpBl0Y').placeholder_html)
  preview.to match(/From which sound/)
  preview.to match(/hqdefault/)
end

また、ファイルの先頭の before do に次を追加しました。

stub_request(:get, "https://www.youtube.com/shorts/wi2jAtpBl0Y").to_return(status: 200, body: onebox_response("youtube-shorts"))

単体テストは初めてなので、この作業の全体像をあまり理解していません。

テストの失敗:

Failures:

  1) Onebox::Engine::YoutubeOnebox can parse youtube shorts results
     Failure/Error:
               http.request(request) do |response|

                 if cookie = response.get_fields('set-cookie')
                   # HACK: If this breaks again in the future, use HTTP::CookieJar from gem 'http-cookie'
                   # See test: it "does not send cookies to the wrong domain"
                   redir_header = { 'Cookie' => cookie.join('; ') }
                 end

                 redir_header = nil unless redir_header.is_a? Hash

     WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Unregistered request: GET https://www.youtube.com/embed/wi2jAtpBl0Y with headers {'Accept' => '*/*', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent' => 'Discourse Forum Onebox v2.9.0.beta12'}

       You can stub this request with the following snippet:

       stub_request(:get, "https://www.youtube.com/embed/wi2jAtpBl0Y").
         with(
           headers: {
          'Accept' => '*/*',
          'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
          'User-Agent' => 'Discourse Forum Onebox v2.9.0.beta12'
           }).
         to_return(status: 200, body: "", headers: {})

       registered request stubs:

       stub_request(:get, "https://www.youtube.com/shorts/wi2jAtpBl0Y")
       stub_request(:get, "https://www.youtube.com/embed/KCyIfcevExE")
       stub_request(:get, "https://www.youtube.com/playlist?list=PL5308B2E5749D1696")
       stub_request(:get, "http://www.youtube.com/user/googlechrome")
       stub_request(:get, "https://www.youtube.com/channel/UCL8ZULXASCc1I_oaOT0NaOQ")
       stub_request(:get, "https://www.youtube.com/watch?v=21Lk4YiASMo")
       stub_request(:get, "http://www.youtube.com/watch?v=21Lk4YiASMo")
       stub_request(:get, "https://www.youtube.com/embed/21Lk4YiASMo")
       stub_request(:get, "https://youtu.be/21Lk4YiASMo")
       stub_request(:get, "https://www.youtube.com/watch?feature=player_embedded&v=21Lk4YiASMo")

       ============================================================
     # ./lib/onebox/helpers.rb:83:in `block in fetch_response'
     # ./lib/onebox/helpers.rb:69:in `fetch_response'
     # ./lib/onebox/helpers.rb:28:in `fetch_html_doc'
     # ./lib/onebox/engine/youtube_onebox.rb:21:in `parse_embed_response'
     # ./lib/onebox/engine/youtube_onebox.rb:42:in `placeholder_html'
     # ./lib/onebox/preview.rb:28:in `placeholder_html'
     # ./spec/lib/onebox/engine/youtube_onebox_spec.rb:110:in `block (2 levels) in <main>'
     # ./spec/rails_helper.rb:328:in `block (2 levels) in <top (required)>'

Finished in 0.89418 seconds (files took 5.04 seconds to load)
17 examples, 1 failure

Failed examples:

rspec ./spec/lib/onebox/engine/youtube_onebox_spec.rb:109 # Onebox::Engine::YoutubeOnebox can parse youtube shorts results

何か原因の手がかりはありますか?

また、before do のこれらの部分はどのような役割を果たしますか?

onebox_response("youtube-embed")

“youtube-embed” という文字列は、Discourse のコードではここ以外では使用されていないように見えます。その目的は何ですか?

なぜ /embed/ リンクが 2 つあり、onebox_response には異なる文字列パラメータがあるのですか?

stub_request(:get, "https://www.youtube.com/embed/21Lk4YiASMo")
.to_return(status: 200, body: onebox_response("youtube"))
stub_request(:get, "https://www.youtube.com/embed/KCyIfcevExE")
.to_return(status: 200, body: onebox_response("youtube-embed"))
「いいね!」 1

私もあまり得意ではありませんが、そのスタブはYouTubeが返すものと全く同じ内容にする必要があります。つまり、wget https://www.youtube.com/embed/wi2jAtpBl0Y が返す内容の一部が必要になります。それが、あなたの仕様が必要とする入力です。

wc wi2jAtpBl0Y
    6   251 65245 wi2jAtpBl0Y

これは、意味を理解してスタブに含めるにはかなりの量のテキストです。

「いいね!」 1

現在の変更内容でドラフトPRを作成していただけますか?そうすれば、フォローしやすく、お手伝いしやすくなります。現在の情報では、スタブファイルを作成し、スタブとアウトゴーイング リクエストを作成する必要があるようです。

「いいね!」 2

はい、どうぞ!

「いいね!」 3

修正した新しいPRを作成しました。共同著者としてあなたの名前も記載されています。

テストスイートが動作中のインターネット接続(および動作中のYoutube)に依存しないように、すべてのインターネットリクエストをスタブし、キャッシュされた応答で呼び出しを返します。onebox_response 関数は、discourse/spec/fixtures/onebox at main · discourse/discourse · GitHub フォルダから渡されたパラメータと同じ名前のファイルに呼び出されます。

ショート動画用の新しい .response ファイルを追加したので、良いテストができます。それ以外は必要ありませんでした。PRありがとうございます!

「いいね!」 3

ショートのワンボックスをテスト:\n\nhttps://youtube.com/shorts/VvoFuaLAslw \n\n\nhttps://youtube.com/shorts/VvoFuaLAslw

「いいね!」 1

「この動画はあなたの国では利用できません」

それ以外は、正常に動作するはずです :smile:

「いいね!」 1

これについてはどうですか?

私には動作します。

簡単な質問です。受け取ったメール通知は次のとおりです。

ここでのURLは /shorts/ ではなく /embed/ ですが、これは正常なことだと思いますか?

「いいね!」 1

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.