Rspec-rails: Failure/Error: expect(response).not_to be_empty

Created on 25 Mar 2017  路  1Comment  路  Source: rspec/rspec-rails

Hi,
I'm testing a feature as follows:

RSpec.describe Api::V1::PlacesController, type: :request do
  let!(:places) { create_list(:place, 10) }
  let(:place_id) { places.first.id }

  describe 'GET /v1/places' do
    before { get '/v1/places' }

    it 'should return places' do
      expect(response).not_to be_empty
      expect(response.size).to eq(10)
    end

    it 'should return status code 200' do
      expect(response).to have_http_status(200)
    end
  end
...
end

I'm using FactoryGirl and Faker as follows:

FactoryGirl.define do
  factory :place do
    tag { Faker::Address.street_name } #string
    lat { Faker::Address.latitude } #float
    lon { Faker::Address.longitude } #float
  end
end

lat and lon happen to be floats

When I run bundle exec rspec I got this message I can't figure to understand well:

1) Api::V1::PlacesController GET /v1/places should return places
     Failure/Error: expect(response).not_to be_empty
       expected #<ActionDispatch::TestResponse:0x0055928491b4c0 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::Mu..., @method=nil, @request_method=nil, @remote_ip=nil, @original_fullpath=nil, @fullpath=nil, @ip=nil>> to respond to `empty?`
     # ./spec/requests/api/v1/places_spec.rb:11:in `block (3 levels) in <top (required)>'
     # ./spec/rails_helper.rb:78:in `block (3 levels) in <top (required)>'
     # ./spec/rails_helper.rb:77:in `block (2 levels) in <top (required)>'

Now if I use the real service (rails s), and I POST a new record, it works, as well as GETting them.

I've googled a lot about how to implement my specs to test my functionalities but I find nothing. The only thing I can notice is that if I use only strings in my models, I have no errors at all. But this one was the first model where I had to use floats.

Here's some info about my environment:

Linux acheron 4.4.0-66-generic #87-Ubuntu SMP Fri Mar 3 15:29:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
LSB Version:    core-9.20160110ubuntu0.2-amd64:core-9.20160110ubuntu0.2-noarch:printing-9.20160110ubuntu0.2-amd64:printing-9.20160110ubuntu0.2-noarch:security-9.20160110ubuntu0.2-amd64:security-9.20160110ubuntu0.2-noarch
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.2 LTS
Release:        16.04
Codename:       xenial

ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-linux]
Rails 5.1.0.rc1

gem 'rspec-rails', '~> 3.5'
gem 'factory_girl_rails', '~> 4.0'
gem 'faker'

Most helpful comment

You've used a matcher be_empty that requires the target to respond to a method, empty? it doesn't so it fails, you can see this in the error a bit clearer if you remove the response internals:

expected #<ActionDispatch::TestResponse:0x0055928491b4c0 ...> to respond to `empty?`

Prehaps you mean response.body rather than response?

>All comments

You've used a matcher be_empty that requires the target to respond to a method, empty? it doesn't so it fails, you can see this in the error a bit clearer if you remove the response internals:

expected #<ActionDispatch::TestResponse:0x0055928491b4c0 ...> to respond to `empty?`

Prehaps you mean response.body rather than response?

Was this page helpful?
0 / 5 - 0 ratings