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

使用RSpec和Capybara测试Rails的创建时间(TimeWithZone)

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

    认为 这是因为我们在创建对象时传递了一个日期,它作为 2014-01-10T00:00:00.000Z

    但当呈现时

    但是正在测试的是呈现的页面,其中

    let(:attachment) { create :attachment, created_at: '2014-01-10' }
    
        background do
          user.article.attachments << [attachment]
          stub_image_convert(new_file[:url])
          stub_request(:put, new_file[:s3_request])
    
          visit article_path(user.article, as: user)
        end
    
        scenario 'uploaded files are added to the list' do
          expect(page).to have_css '.attachment-item', count: 1
          within file_group_selector(position: 1) do
            upload_date_present 'January 10, 2014'
            file_previews_present count: 1
          end
        ...
     ...
    

    帮手:

      def upload_date_present(date)
        expect(page).to have_css 'grouped-attachments .group-title', text: date
      end
    

    最终一页一页地显示出来 momentJS (javascript/Angular)显示日期:

      newAttachment.groupDate = moment().format('LL')
    

    Failure/Error: expect(page).to have_text 'January 10th, 2014'
       expected to find text "January 10th, 2014" in "John Snow First comment message January 9th, 2014"
    

    我们应该如何测试这个?FWIW Timecop帮不上忙。

    1 回复  |  直到 6 年前
        1
  •  1
  •   arieljuod    6 年前

    created_at 期待一个完整的约会,而不仅仅是一个约会。我猜Rails将字符串date解析为DateTime,00:00:00解析为time。尝试用正确的时区创建一个合适的DateTime对象,并将其作为值传递。这样你就不会像Rails那样给引擎盖下的魔法留下任何东西。