Question

When I run rspec to run all tests, they all pass, but when I give a specific filename, it fails.

$ 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'

I have it mocked & stubbed already, so why is it failing? The only thing I can see is the existing stub is '/test/' and the request is '/default/'. How did that change?

Ok I ran into this problem again on another spec. Here is the spec: https://gist.github.com/starrychloe/1d79d9925f9b79ae5c01

I did find this 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>

It's like rspec is running in production environment, even if I explicitely give the environment

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

I think it is an rspec problem. rspec -v: version 2.14.7.

Was it helpful?

Solution

I added stub_request(:post, "http://localhost:8981/solr/default/update?wt=ruby").to_return(:status => 200, :body => "", :headers => {}) before login_admin_user but I don't think that's the root solution.

OTHER TIPS

It is probably interacting with another test.

Try running the suite in random order

rspec spec --order rand

and/or try running all the tests for that controller or or controllers.

You are using webmock, which disables all HTTP connections by default.

You can modify you Gemfile to disable auto require as followed:

gem "webmock", :require => false

And require webmock where you need it (e.g., require 'webmock/rspec'). After disabling HTTP connections for certain tests, remember to WebMock.allow_net_connect! to allow real http connections for other tests.

Another alternative is to WebMock.disable_net_connect!(:allow_localhost => true) to disable external requests while allowing localhost.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top