I have a weird behaviour when running a spec separately passes bu t fails 
when running all the tests.

Here is the spec to test a service:

require 'rails_helper'


RSpec.describe SportsService do
  let(:user) { create(:user)}


  describe '#call' do
    it 'raises an error if the request is failed' do
      service = described_class.new(user: user)
      allow(service).to receive(:execute_request).with(anything).and_return(
failed_response)


      expect { service.call }.to raise_error ExceptionHandler::
AuthenticationError
    end


    it 'send successful response' do
      service = described_class.new(user: user)
      allow(service).to receive(:execute_request).with(anything).and_return(
successful_response)
      sports_values_to_check = service.call.map {|sport| {id: sport.id, 
label: sport.label}}
      expect(sports_values_to_check).to include(id: 1, label: 'sport-1') 
    end
  end
end


def failed_response
  double(:response, code: 100)
end


def successful_response
  double(:response, code: 200, body: '[{"sport_id": 1, "label": "sport-1"}, 
{"sport_id": 2, "label": "sport-2"}]')
end


The failing example is "send successful response" that fails with a message:

 1) SportsService#call send successful response

     Failure/Error: create_sports(response.body)

       #<Double :response> received unexpected message :body with (no args)

     # ./app/services/sports_service.rb:17:in `call'

     # ./spec/services/sports_service_spec.rb:17:in `block (3 levels) in 
<main>'



Why RSpec does not find body defined on my double ? Thank you.

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/126d3ab2-151c-40a9-8e6b-e7f5e46933d3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to