代码之家  ›  专栏  ›  技术社区  ›  Meltemi

日期前后的RSpec Timecop故障似乎与时区有关

  •  0
  • Meltemi  · 技术社区  · 6 年前

    我们有一些测试(规格)在日期/时间前后失败。猜测这是UTC的问题,但不知道为什么这些规格通过了上一次接触项目(约8个月前)!?!

    #规范/特征/注释/创建_规范rb

    feature 'Comment creation', type: :feature, js: true do
      include CommentsPageHelpers
      ...
      let!(:current_date) { Date.parse('2017-01-03') }
      ...
    
      background do
        Timecop.freeze(current_date)
        ...
      end
    
      after do
        Timecop.return
      end
    
      shared_examples 'added comment' do |position:, text:|
        scenario 'adds single comment' do
            ...
            expect(page).to have_text 'January 3rd, 2017'
          end
        end
      end
    

    视图(角度模板) #应用程序/视图/模板/注释.html.slim

    .comment
      ...
      {{ comment.createdAt | moment: 'MMMM Do, YYYY' }}
    

    Comment creation for image behaves like added comment adds single comment
         Failure/Error: expect(page).to have_text 'January 3rd, 2017'
           expected to find text "January 3rd, 2017" in "John Snow First comment message January 2nd, 2017Remove"
         Shared Example Group: "added comment" called from ./spec/features/comments/creation_spec.rb:76
         # ./spec/features/comments/creation_spec.rb:42:in `block (4 levels) in <top (required)>'
         # ./spec/features/comments/creation_spec.rb:39:in `block (3 levels) in <top (required)>'
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Meltemi    6 年前

    更改 let! 下面的两行似乎有固定的东西。不知道它可能会中断什么,或者它是否会中断其他时区的开发人员?!?

    Time.zone = 'Pacific Time (US & Canada)'
    let!(:current_date) { Time.parse('2017-01-03').in_time_zone }