Rajkumar P
Rajkumar P

Reputation: 179

Get secure random value in rspec

In my controller, after downloading xls file that will be redirect to the some location. redirect path contains the securerandom value. How can I get random value as same as to the controller random value.

I have tried by getting random value from spec/controller file. But that value different from the controller value.

This is my controller

def xls_job

  ref = SecureRandom.hex

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

end

I got error like this


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>.

How can i get random value same as to the controller..?

Upvotes: 1

Views: 1262

Answers (1)

morissetcl
morissetcl

Reputation: 564

You have to stub your ref variable like below:

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

source: question on SO

Should do the trick.

Upvotes: 4

Related Questions