Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7065615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:58:40+00:00 2026-05-28T04:58:40+00:00

In my spec I need to verify whether strings are being converted correctly to

  • 0

In my spec I need to verify whether strings are being converted correctly to other types. Among these are Time, Date and DateTime.

The tests work fine with all types including Date, but it fails when Time is involved:

let(:type) { DateTime }
let(:date_time) { type.now }
let(:params) { [ date_time.to_s ] }

it 'should convert the parameters to dates/times' do
  # converted basically calls params.map { |param| type.parse param }
  converted.should == [date_time]
end

This actually almost works:

expected: [#<DateTime: 2012-01-17T15:03:27-02:00 ((2455944j,61407s,568873440n),-7200s,2299161j)>]
     got: [#<DateTime: 2012-01-17T15:03:27-02:00 ((2455944j,61407s,0n),-7200s,2299161j)>] (using ==)

The diff is off by only one value, but I don’t know what exactly it means. The result of the Time test is even more confusing:

expected: [2012-01-17 15:03:27 -0200]
     got: [2012-01-17 15:03:27 -0200] (using ==)

The only differences between the tests are the type and the description. After some tracing, it appears to me that the parsed times have no microsecond precision; usec always returns 0.

Any tips on how to make these specs pass?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:58:40+00:00Added an answer on May 28, 2026 at 4:58 am

    Try comparing their string representations using a formatting which excludes the fields that you do not want to compare (e.g. millis, micros, etc).

    converted[0].iso8601(0).should == date_time.iso8601(0)
    

    [Edit] Note that the above example compares the two DateTimes without any fractional seconds (see the linked iso8601 docs above).

    If you really want to compare the DateTime objects themselves then your test code is complete, you’ll just have to fix your implementation to adjust the fractional seconds part to some fixed value (e.g. zero).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to encrypt / decrypt passwords for a new application. The spec requires
I am using Clearcase dynamic view with a complex config spec. I need a
I need to test an API of following spec URL: http://myapp.com/api/upload/ Method: POST Parameters:
I'm writing a spec and need to describe some JSON objects. Big JSONs tend
To conform with the <boolean> spec of Xml-RPC I need to transform my xs:boolean
I have one spec: require 'spec_helper' # hmm... I need to include it here
We're being asked to spec out production database hardware for an ASP.NET web application
i need equivalent c# code VssSdkClientId VSS_SDK_SPEC startVssSdkClientEcho(const VssSdkXChar *host, const VssSdkPort port, NotifyFunc
While the C# spec does include a pre-processor and basic directives (#define, #if, etc),
Does the functional spec help or hinder your expectations? Do programmers who practice the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.