Testing validations with custom contexts

Is there a way to test a validation with a custom context using shoulda matchers?

For example, given:

class Bookmark < ActiveRecord::Base
  validates :short_url, presence: true, format: { with: URL::REGEXP }, on: :fetch

I want to test that the validations only run within the :fetch context.

@dwaynecrooks, I think shoulda matchers support

it { should validate_presence_of(:x).on(:y) }

(judging from this pull request)

If you don’t mind me asking, :fetch hints at a read operation… If you are trying to validate that the :short_url is present when retrieving a bookmark, you are deviating from the purpose of validations and behaviour will not be what is expected. If not, I would reconsider renaming :fetch to something that indicates that a value is being inserted/updated.

Evidently, I don’t know your codebase and I just trying to help so I hope no offence is taken!

Yeah, I just went ahead and guessed that that should be the API if it was supported and it worked. But, thanks for a reference.

None taken. Great advice, naming is important. I did change the name albeit to another (possibly) bad name, :shorten.

Here’s the bit of code that uses it, if you’re interested. Here are the tests and this is where it’s ultimately called. Code is in it’s early stages but plan to throw in some background jobs eventually.