【问题标题】:Why does my rspec test pass by itself but fails when I give a specific test to run?为什么我的 rspec 测试自行通过,但在我运行特定测试时失败?
【发布时间】:2014-05-07 23:37:10
【问题描述】:

当我运行 rspec 运行所有测试时,它们都通过了,但是当我给出特定的文件名时,它会失败。

$ rspec spec/controllers/refinery/books/admin/books_controller_spec.rb


  2) Refinery::Books::Admin::BooksController GET :new responds 200 success
     Failure/Error: login_admin_user #Gives 404
     WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Unregistered request: POST http://localhost:8981/solr/default/update?wt=ruby with body '<?xml ...' with headers {'Content-Type'=>'text/xml'}

       You can stub this request with the following snippet:

       stub_request(:post, "http://localhost:8981/solr/default/update?wt=ruby").
         with(:body => "<?xml ...",
              :headers => {'Content-Type'=>'text/xml'}).
         to_return(:status => 200, :body => "", :headers => {})

       registered request stubs:

       stub_request(:post, "http://localhost:8983/solr/test/update?wt=ruby").
         with(:headers => {'Content-Type'=>'text/xml'})

       ============================================================
     # ./lib/rsolr/connection.rb:15:in `execute'
     # (eval):2:in `post'
     # ./spec/support/controller_macros.rb:21:in `login_admin_user'

我已经对其进行了嘲笑和存根,为什么它会失败?我唯一能看到的是现有存根是“/test/”,请求是“/default/”。发生了什么变化?

好的,我在另一个规范上再次遇到了这个问题。这是规格: https://gist.github.com/starrychloe/1d79d9925f9b79ae5c01

我确实找到了这个 solr/solr.xml

<?xml version="1.0" encoding="UTF-8" ?>
<solr persistent="false">
  <cores adminPath="/admin/cores" host="${host:}" hostPort="${jetty.port:}">
    <core name="default"     instanceDir="." dataDir="default/data"/>
    <core name="development" instanceDir="." dataDir="development/data"/>
    <core name="test"        instanceDir="." dataDir="test/data"/>
  </cores>
</solr>

这就像 rspec 在生产环境中运行,即使我明确给出环境

$ RAILS_ENV=test rspec spec/controllers/refinery/books/books_controller_spec.rb

我认为这是一个 rspec 问题。 rspec -v:版本 2.14.7。

【问题讨论】:

  • 您好 Chloe,一种可能是您的环境没有被正确拾取。如果您有一个 spec_helper 类(通常这些设置环境名称),请检查您是否在此特定规范的顶部有“需要 spec_helper”。我的猜测是 SOLR 配置为在“测试”环境中使用 /test/,但是当您单独运行此测试时,您不会获得“测试”配置
  • 更具体地说,这是您的 spec_helper 中可能没有被拉入的行:ENV["RAILS_ENV"] ||= 'test'
  • 是的,我在 _spec 文件的顶部确实有 require 'spec_helper'spec_helper.rb 的第一行是ENV["RAILS_ENV"] ||= 'test'
  • 您必须在具有不同 SOLR 核心名称“test”和不同端口“8983”的存根中添加新行这一事实表明您正在选择不同的配置。建议您拥有的其他规范之一是在某处更改您的 SOLR 配置,但是当您单独运行此规范时它不会被执行。
  • 哇,我没有看到不同的端口。我在整个项目目录中搜索了 8981,它只显示在它是存根请求的地方。我不知道它是从哪里来的。

标签: ruby-on-rails rspec sunspot webmock


【解决方案1】:

我在login_admin_user 之前添加了stub_request(:post, "http://localhost:8981/solr/default/update?wt=ruby").to_return(:status =&gt; 200, :body =&gt; "", :headers =&gt; {}),但我认为这不是根本解决方案。

【讨论】:

    【解决方案2】:

    它可能正在与另一个测试交互。

    尝试以随机顺序运行套件

    rspec spec --order rand
    

    和/或尝试为该控制器运行所有测试。

    【讨论】:

    • 我运行了它,它仍然通过了。
    【解决方案3】:

    您正在使用 webmock,它默认禁用所有 HTTP 连接。

    您可以修改 Gemfile 以禁用自动要求,如下所示:

    gem "webmock", :require => false
    

    并在需要的地方要求 webmock(例如,require 'webmock/rspec')。在为某些测试禁用 HTTP 连接后,请记住 WebMock.allow_net_connect! 以允许其他测试使用真正的 http 连接。

    另一个替代方法是 WebMock.disable_net_connect!(:allow_localhost =&gt; true) 禁用外部请求,同时允许本地主机。

    【讨论】:

      猜你喜欢
      • 2012-02-17
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多