为 Youtube oneboxing 添加简单单元测试时遇到问题

您好!

我想添加对 /shorts/ YouTube 链接的支持。

我对 YoutubeOnebox 类的修改有效,但需要在 youtube_onebox.rb 中添加一个测试,而我的测试不起作用。

“可以解析 youtube 嵌入结果”测试 类似,我添加了以下代码:

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/ 链接,为什么 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 个赞

我也不太擅长这个,但你需要让这个存根(stub)拥有与 YouTube 返回的完全相同的东西。所以它需要包含 wget https://www.youtube.com/embed/wi2jAtpBl0Y 返回内容的一个子集。这就是你的规范(spec)所需要的输入。

wc wi2jAtpBl0Y 
    6   251 65245 wi2jAtpBl0Y

要理解这么多文本并将其放入你的存根中,这可不是件容易的事。

1 个赞

您可以打开一个草稿 PR 来展示您当前的更改吗?这样更容易跟进和提供帮助。根据目前的信息,您似乎需要创建存根文件并存根传出的请求。

2 个赞

好的,这是!

3 个赞

我在一个新的 PR 中修复了它,该 PR 正确地将您列为联合作者:

为了使我们的测试套件不依赖于有效的互联网连接(以及有效的 Youtube),我们会通过让这些调用从文件夹 discourse/spec/fixtures/onebox at main · discourse/discourse · GitHub 返回缓存的响应来存根所有互联网请求。onebox_response 函数将调用一个文件名与从该文件夹传递的参数匹配的文件。

我为 shorts 添加了一个新的 .response 文件,以便我们有一个好的测试,但这仅是必需的。感谢您的 PR!

3 个赞

测试短视频 onebox:

https://youtube.com/shorts/VvoFuaLAslw

1 个赞

视频不可用
上传者未在您所在的国家/地区提供此视频

除此之外,它应该能正常工作 :smile:

1 个赞

关于

对我来说是有效的。

快速提问。这是我收到的电子邮件通知:

这里的 URL 是 /embed/ 而不是 /shorts/,但我猜这很正常?

1 个赞

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