Here we have a Customer model with multiple validations, and one of the validation access an external service to validate if the person exists.
And here we test all validations:
describe Customer do
it 'is not valid without a name' do
customer = build_stubbed(:customer, name: nil)
customer.valid?
asserts...
end
it 'is not valid without a full name' do
customer = build_stubbed(:customer, name: 'Test')
customer.valid?
asserts...
end
if 'should check if user really exists' do
customer = build_stubbed(:customer, doc_id: '00000000')
customer.valid?
asserts....
end
other tests...
The last item test the external service validation using doc_id to check if the person exists, but here every test runs all validations so should i stub_request the service request with before or stub it within each test?
You should use a context to specify which test uses the stup and which does not. That's advised here for example. This way you will be able to have a context where the method is stubbed and to test the validation with external service in an other context.