On Tuesday, 23 October 2018 16:15:26 UTC+2, belgoros wrote:
>
> 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.
>
I think I have an idea.
I'm using services and declare them in an initializer created in
*config/initilizers/service_provider.rb* as follows:
class ServiceProvider
@services = {}
def self.register(key, klass)
return false if @services.key?(key) && !Rails.env.test?
@services[key] = klass
end
def self.get(key)
@services[key]
end
def self.[](key)
get(key)
end
def self.finished_loading
@services.freeze unless Rails.env.test?
end
end
... #other services being declared here as well
ServiceProvider.register :sports_service, SportsService
ServiceProvider.finished_loading
And in *sports_controller* I'm getting the service like that:
module V1
class SportsController < ApplicationController
def index
json_response sports_service.call
end
private
def sports_service_class
@sports_service_class ||= ServiceProvider.get(:sports_service)
end
def sports_service
@sports_service ||= sports_service_class.new(user: current_user)
end
end
end
I think I'll have to create and register for such a test a dummy
SportsService directly in my rspec file testing the service, somethig like
that (*spec/services/sports_service_spec*):
require 'rails_helper'
SuccessfulUserInfoService = Struct.new(:user, keyword_init: true) do
include ServiceClient
def call
true
end
end
RSpec.describe SportsService do
let(:user) { create(:user)}
describe '#call' do
after(:each) do
ServiceProvider.register(:sports_service, SportsService) # putting
back the original service
end
...
This is just an idea, have not tested it yet. May be I'm testing the wrong
thing ? I'd like to test the result returned by service's #call method
--
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/55bcadd8-b1db-4940-b948-517ca13f8420%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.