我的问题是,我想模拟我的自定义验证方法,该方法从DB返回一些数据( id列表-检查给定的id是否在我的数据库中)。
所以少说点,多写点代码:在我的新闻通讯::Contract::Create class中
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在测试中,我试着运行
expect(Newsletter::Contract::Create).to receive(:publisher_exists?).and_return(true)但很明显我收到了
Newsletter::Contract::Create does not implement: publisher_exists?所以问题是什么对象调用了我的自定义验证方法,这样我就可以模拟它了?;]
发布于 2018-11-16 23:28:07
到目前为止,我所理解的是可能有一些eval/anynoumus类,所以我不是模仿这个方法,而是将在方法内部使用的AR存根。expect(Publisher).to receive_message_chain(:where, :present?).and_return(true)
https://stackoverflow.com/questions/53340343
复制相似问题