1

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.

Chloe
  • 25,162
  • 40
  • 190
  • 357
  • Hi Chloe, one possibility is that your environment isn't being picked up correctly. If you have a spec_helper class (usually these set the environment name), check that you have 'require spec_helper' at the top of this particular spec. My guess is that SOLR is configured to use /test/ when you are in your 'test' environment, but when you are running this test alone, you are not getting the 'test' configuration – Dave Satch May 08 '14 at 00:24
  • More specifically, this is the line you are likely to have in your spec_helper that isn't getting pulled in: ENV["RAILS_ENV"] ||= 'test' – Dave Satch May 08 '14 at 00:29
  • Yes I do have `require 'spec_helper'` at the top of the _spec file. The first line of `spec_helper.rb` is `ENV["RAILS_ENV"] ||= 'test'`. – Chloe May 08 '14 at 00:50
  • The fact that you have had to add a new line to your stubs that has a different SOLR core name 'test' and also a different port '8983' shows that you are picking up a different config. Suggest that one of the other specs you have is changing your SOLR config somewhere but it is not getting executed when you run this spec alone. – Dave Satch May 08 '14 at 01:03
  • Wow I didn't see the different port. I searched the entire project directory for 8981 and it only showed up where it was a stub request. I don't know where it came from. – Chloe May 08 '14 at 03:38

3 Answers3

2

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.

Chloe
  • 25,162
  • 40
  • 190
  • 357
2

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.

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
0

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.

konyak
  • 10,818
  • 4
  • 59
  • 65