2015-02-06 3 views
0

나는 내일의 날짜 (1.date.from_now)를 보내는 방법이 있습니다. 내 사양에서 동일한 방법을 테스트하고있다. 누구든지 그 날짜를 스텁하는 법을 말해 줄 수 있어요.rspec에서 내일 스텁하기

지금 내가 1.date.from을 통과하면. 다음 오류가 발생했습니다. 아래

<S3Client (class)> received :upload_csv_by_path with unexpected arguments 
    expected: ("/home/projects/sh/tmp/test_exporter.csv", nil, {:expires=>Sat, 07 Feb 2015 11:36:39 UTC +00:00}) 
      got: ("/home/projects/sh/tmp/test_exporter.csv", nil, {:expires=>Sat, 07 Feb 2015 11:36:39 UTC +00:00}) 

내 방법입니다

다음
S3Client.upload_csv_by_path(file_name, nil, expires: 1.day.from_now) 

내 사양은,

S3Client.should_receive(:upload_csv_by_path).with("#{Rails.root}/tmp/scalar_test_exporter.csv", nil, expires: 1.day.from_now) 

사람이 내가 Time.now를 사용하여 해결이 문제

+0

해결했습니다. 감사합니다 – user3655415

+0

보석을보세요 [timecop] (https://github.com/travisjeffery/timecop) – gotva

답변

0

문제는 시간이 정확히 볼 수 있다는 동일하지만, 타임 스탬프,하지 (이 마이크로 초 단위의 경우에도) 두 개의 문을 실행 사이에 소요되는 시간이다에, 시간이 다른 점 당신은 예를 들어, 두 문장에서 같은 시간 변수를 사용해야합니다 그것을 해결 :

time = Time.now 
S3Client.should_receive(:upload_csv_by_path).with("#{Rails.root}/tmp/scalar_test_exporter.csv", nil, expires: time) 
S3Client.upload_csv_by_path(file_name, nil, expires: time) 

당신은 시간이 모두 문에서 동일한 지 확인하십시오 수, 또는 당신은 당신을 도울 timecop 같은 보석을 사용할 수있는이 방법 @gotva가 제안한 것과 같이 timecop은 시간을 멈추고 앞뒤로 움직일 수 있기 때문에 매우 유용합니다. 시간이 지나면 멋지 네요.이 예제를 쓰려면

Time.freeze 
S3Client.should_receive(:upload_csv_by_path).with("#{Rails.root}/tmp/scalar_test_exporter.csv", nil, expires: 1.day.from_now) 
S3Client.upload_csv_by_path(file_name, nil, expires: 1.day.from_now) 
Time.return 
0

를 해결할 수있는 방법을 말해 줄 수 + 1. 일 내 방법.

그리고 Time.now를 스터핑하고 매개 변수를 추가 한 날은 아래와 같이 1 일을 추가했습니다.

current_time = Time.now 
Time.stub(:now) { current_time } 
S3Client.should_receive(:upload_csv_by_path).with("#{Rails.root}/tmp/scalar_test_exporter.csv", nil, expires: current_time + 1.day) 
관련 문제