3

さまざまなタイムゾーンやさまざまな時間に、いくつかの締め切りがユーザーに正しく表示されることをテストしたいと考えています。私のテストでは capybara+rspec+phantomjs を使用しています。

ブロックを Timecop.travel(datetime) に渡しています。そのブロック内のテストのコードはモックされた日時を正しく取得していますが、PhantomJS / モックされたブラウザーがモックされた時間を取得していないようです。

PhantomJS を Timecop で動作させる既知の方法はありますか? または、テスト目的で時間をモックまたは操作する他の方法はありますか?

これは、私が何を意味するかを説明するための簡単な例です。

time_spec.rb:

it "should show the Time travel date" do
  # current date is 2017-01-24
  Date.today.should == Date.parse("2017-01-24")
  Timecop.travel( Time.parse("2001-01-01 01:01") ) {
    sign_in(user)
    visit "/#{user.username}"

    Date.today.should == Date.parse("2001-01-01")
    page.should have_text("Today is 2001-01-01")
    page.should have_text("Javascript says 2001-01-01")
  }
end

user.html.erb:

<p>Today is <%= Time.now.iso8601 %></p>
<script>
  var now = moment().format()
  $('p').append("<p>Javascript says "+now+"</p>")
</script>

テスト実行の出力:

Failures:

  1) Dashboard should show the time travel date
     Failure/Error: page.should have_text("Javascript says 2001-01-01")
       expected to find text "Javascript says 2001-01-01" in 
       "Today is 2001-01-01T01:01:00-08:00 Javascript says 2017-01-24T12:36:02-08:00"
     # ./spec/features/time_spec.rb:67:in `block (3 levels) in <top (required)>'
     # /gems/ruby-2.2.0/gems/timecop-0.8.0/lib/timecop/timecop.rb:147:in `travel'
     # /gems/ruby-2.2.0/gems/timecop-0.8.0/lib/timecop/timecop.rb:121:in `send_travel'
     # /gems/ruby-2.2.0/gems/timecop-0.8.0/lib/timecop/timecop.rb:62:in `travel'
     # ./spec/features/time_spec.rb:59:in `block (2 levels) in <top (required)>'
4

1 に答える 1