How to mock dry-rb (used inf Reform contract) validation configure method

387 views Asked by At

My problem is that I want to mock my custom validation method that returns some data from DB ( list of ids - to check if given id is in my DB ).

So less talk, more code: in my Newsletter::Contract::Create class I have

validation do
  configure do
    config.messages_file = "config/error_messages.yml"
    def publisher_exists?(value)
      Publisher.where(id: value).present?
    end
  end
  required(:publisher_id).filled(:publisher_exists?)
end

And in test, I try to run

expect(Newsletter::Contract::Create).to receive(:publisher_exists?).and_return(true)

but obviously i receive

Newsletter::Contract::Create does not implement: publisher_exists?

So the question is what object calls for my custom validate methods, so I can mock it?;]

1

There are 1 answers

0
Adam Piotrowski On

So far what i understand is that there probably is some eval/anynoumus classes, so instead of mocking this method, i will stub AR that is used inside of method. expect(Publisher).to receive_message_chain(:where, :present?).and_return(true)