【问题标题】:How to mock dry-rb (used inf Reform contract) validation configure method如何模拟dry-rb(使用inf改革合约)验证配置方法
【发布时间】:2018-11-16 15:01:37
【问题描述】:

我的问题是我想模拟从数据库返回一些数据的自定义验证方法(id 列表 - 检查给定的 id 是否在我的数据库中)。

少说话,多写代码: 在我的 Newsletter::Contract::Create 类中我有

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?

所以问题是我的自定义验证方法需要什么对象,所以我可以模拟它?;]

【问题讨论】:

    标签: rspec trailblazer reform dry-rb


    【解决方案1】:

    到目前为止,我的理解是可能有一些 eval/anynoumus 类,所以我不会模拟这个方法,而是将在方法内部使用的 AR 存根。 expect(Publisher).to receive_message_chain(:where, :present?).and_return(true)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 2016-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多