Search code examples
ruby-on-railsrspecsunspotwebmock

Why does my rspec test pass by itself but fails when I give a specific test to run?


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.


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.