Testing custom validators presence in params in ruby grape API

235 views
Skip to first unread message

Michał Krzyzanowski

unread,
Mar 12, 2020, 10:57:31 AM3/12/20
to Grape Framework Discussion
I have a very simple grape-powered API. Lets say that it looks like this:

class MyApi < Grape::API
 
params do
    requires
:name, type: String
    requires
:id, type: Integer, positive_value: true
 
end
 
 
get '/' do
   
# whatever...
 
end
end



I have custom PositiveValue validator, which works just fine for id.


I would like to create spec that makes sure that my params has correct options passed. I would like to avoid making full request specs, but instead of that I'd like to check if name param has type: String, and make sure it's required:

# my_api_spec.rb
describe
'params' do
  let
(:params) { described_class.new.router.map['GET'].first.options[:params] }
  specify
do
    expect
(params['name']).to include(type: 'String') # this one works fine
    expect
(params['id']].to include(type: 'Integer', positive_value: true) # this one fails
 
end
end

It turns out that this params have {:required=>true, :type=>"Integer"} hash. How can I test and make sure that my custom validators are being used for given param?


Daniel D.

unread,
Mar 13, 2020, 10:04:04 AM3/13/20
to Grape Framework Discussion
I feel like this might be digging into implementation details of a route parameter validation, which may very well change in the future. I would write a request spec and expect that any instance of the custom validator is being called with a specific argument.

--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-grape/6690afe0-47ab-4b60-96ad-e0426733f13f%40googlegroups.com.


--

Michał Krzyzanowski

unread,
Mar 16, 2020, 10:50:36 AM3/16/20
to Grape Framework Discussion
Hi Daniel, thanks for your response.

I think that response specs should serve different purpose.
In my ideal scenario, I'd like to:

1. Test validator in separation
2. Test my params in separation (so make sure that my params have correct types, and correct validators were specified)
3. Test request to make sure that correct services have been called, and perhaps - if the response is the one I've been expecting.

While one might argue that having declared `my_custom_validator: true` in params does not guarantee that it will end up actually running that validator, I think that it's frameworks' responsibilty, and I won't want to test grapes' internals.

I found a way how I can test it, but it indeed uses a lot of internal methods, question is it shouldn't be more exposed.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-...@googlegroups.com.

Daniel D.

unread,
Mar 16, 2020, 4:56:16 PM3/16/20
to Grape Framework Discussion
So, given params you want to make sure the entire validator stack does what it's supposed to do and that some specific validator is called, without making actual requests?

That's a great goal! What would you like the developer code to look like? Try to come up with a proposal of what you'd want to write in a few cases and open a feature request. We can discuss it there and see if we can actually implement it (hopefully with your help).

To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ruby-grape/10fc1f4f-3b38-417a-b1ff-6915862f0d52%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages