Rspec - how to compare with actual DateTime pending?

My rspec:

it "can show the current month name" do
  expect(Calendar.create_date_using_month(1)).to eq '2000-01-01 00:00:00 -0500'
end

failure:

expected: "2000-01-01 00:00:00 -0500"
     got: 2000-01-01 00:00:00 -0500

For my code:

def self.create_date_using_month(n)
  Time.new(2000,n,1)
end

Should / can I modify RSpec to match the actual string, not the date?

I tried: Date.strptime("{ 2000, 1, 1 }", "{ %Y, %m, %d }")

but it gives me

   expected: #<Date: 2000-01-01 ((2451545j,0s,0n),+0s,2299161j)>
        got: 2000-01-01 00:00:00 -0500
+4
source share
3 answers

I'm a little confused about what exactly you are testing here. If create_data_using_monthcreating an object Time, you must compare it with the object Time.

This post is:

expected: "2000-01-01 00:00:00 -0500"
     got: 2000-01-01 00:00:00 -0500 

tells you that he was expecting a letter string with a date, but received an object whose to_s is the same.

So, I think you could β€œfix” it by changing this:

it "can show the current month name" do
  expect(Calendar.create_date_using_month(1).to_s).to eq '2000-01-01 00:00:00 -0500'
end

, , ? , .

:

it "can show the current month name" do
  expect(Calendar.create_date_using_month(1)).to eq Time.new(2000, 1, 1)
end

.

+4

, .

to_i , ( ).

Time.now().to_i.should == Time.now().to_i

,

Time.now().should.eql?(Time.now())

:

RSpec::Matchers.define :be_equal_to_time do |another_date|
  match do |a_date|
    a_date.to_i.should == another_date.to_i
  end
end

Time.now().should be_equal_to_time(Time.now())
+3

DateTime Class http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/DateTime.html

DateTime.parse('2000-01-01 00:00:00 -0500') == DateTime.new(2000,1,1,0,0,0,'-5')
#=> true

, , . , to_s - am, .

+2

All Articles