On Wednesday, 24 October 2018 09:13:39 UTC+2, belgoros wrote:
>
>
>
> On Wednesday, 24 October 2018 09:04:51 UTC+2, belgoros wrote:
>>
>>
>>
>> On Tuesday, 23 October 2018 21:52:38 UTC+2, Jon Rowe wrote:
>>>
>>> Its looks like your doubles are some how leaking between examples, I’d 
>>> wager its something to do with your service injector but I’m unsure without 
>>> a proper example.
>>>
>>> Cheers
>>> Jon Rowe
>>>
>>
>> I think the problem comes from #successful_response method, - I checked 
>> and discovered that I had 2 ones in different specs, both are declared 
>> outside of main *RSpec.describe* block like that:
>>
>> #spec_one_spec.rb
>>
>> RSpec.describe 'SpecOne' do
>> ...# some examples
>> end
>>
>> def successful_response
>>   double(:response, code: 200)
>> end
>>
>> #spec_two_spec.rb
>>
>> RSpec.describe 'SpecTwo' do
>> ...# some examples
>> end
>>
>> def successful_response
>>   double(:response, code: 200, body: '[{"sport_id": 1, "label": 
>> "sport-1"}, {"sport_id": 2, "label": "sport-2"}]')
>> end
>>
>> What is the rule of thumb to declare such a kind of helper methods ? 
>> Should we put them inside the main *RSpec.describe* block, outside, make 
>> them private ?
>>
>> Cheers
>>
>
> I moved the above methods inside the main *RSpec.describe* block and all 
> the tests passed. So should we always keep them there or there is another 
> best practice ?
> Thank you. 
>

I re-opened the *Effective Testing with RSpec-3* book by Myron Marston & 
Ian Dees, and on *page 11* you can already find the explanation:

Each example group is a Ruby class, which means that we can define methods 
on it. Right after the describe line, add the following code....

def sandwich 

  Sandwich.new('delicious', []) 

end 

So I refactored all the examples to fix that. Hope this helps.
 

>
>> ---------------------------
>>> [email protected]
>>> jonrowe.co.uk
>>>
>>> On 23 October 2018 at 15:58, belgoros wrote:
>>>
>>> On Tuesday, 23 October 2018 16:55:35 UTC+2, belgoros wrote:
>>> I forgot to put the rest of the example (*see the dummy service 
>>> initialization*):
>>>
>>> it 'sends a successful response with sports data' do
>>>
>>>       
>>> ServiceProvider.register(:sports_service, SuccessfulSportsService)
>>>
>>>       service 
>>> = ServiceProvider.get(:sports_service)
>>>
>>>       allow
>>> (service).to receive(:execute_request).with(anything).and_return(
>>> successful_response)
>>>
>>>       
>>>       service_data 
>>> = service.
>>> call
>>>       sports_values_to_check 
>>> = service_data.map {|sport| {id: sport.id, label: sport.label}}
>>>
>>>       expect
>>> (sports_values_to_check).to include(id: 1, label: 'sport-1')
>>>
>>>     
>>> end 
>>>
>>>

-- 
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/c1765fb9-19e4-4a36-8b4b-3f004e4f04f7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to