【问题标题】:Get secure random value in rspec在 rspec 中获取安全的随机值
【发布时间】:2019-08-21 20:44:20
【问题描述】:

在我的控制器中,下载 xls 文件后,该文件将被重定向到某个位置。重定向路径包含安全随机值。如何获得与控制器随机值相同的随机值。

我尝试从规范/控制器文件中获取随机值。但该值与控制器值不同。

这是我的控制器

def xls_job

  ref = SecureRandom.hex

  redirect_to polymorphic_url([:xls_job, :client, booking_type.tableize], { job_id: @job.id, job_ref: ref } )

end

我遇到了这样的错误


Expected response to be a redirect to 
<http://test.host/client/interpretings/xls_job?job_id=15433&job_ref=1db823fef6b892e6c1cc362bf8fe5e87> 

but was a redirect to 
<http://test.host/client/interpretings/xls_job?job_id=15433&job_ref=b8c9464466b90e65492627b77e121346>.

如何获得与控制器相同的随机值..?

【问题讨论】:

  • 你能展示你的测试代码吗?

标签: rspec


【解决方案1】:

你必须像下面这样存根你的 ref 变量:

before { allow(SecureRandom).to receive(:hex).and_return('myrandomhex89809') }

来源:question on SO

应该做的伎俩。

【讨论】:

    猜你喜欢
    • 2014-10-16
    • 1970-01-01
    • 2012-01-30
    • 2015-10-12
    • 2020-10-02
    • 2017-08-13
    • 2011-10-13
    • 1970-01-01
    • 2012-07-13
    相关资源
    最近更新 更多