Reputation: 2497
I have an object called a work order that has the following schema:
# == Schema Information
#
# Table name: work_orders
#
# id :integer not null, primary key
# due_on :date
# name :string(255)
I also have a Json output of the object when it is displayed:
{
id: 1,
due_on: "2015-06-08",
name: "my work order"
}
I am attempting to use rspec to confirm that date displayed by the json, matches the date of the object. This is something I have done 50 times for other objects...
I have the following:
exemplar = FactoryGirl.create(:work_order)
...
puts "Test: #{exemplar.due_on}"
expect(exemplar.name).to eq(hash['name']) #This works fine!
expect(exemplar.due_on).to eq(hash['due_on']) #This blows up...
The output I get is:
Test: 2015-06-08
expected: "2015-06-08"
got: Mon, 08 Jun 2015
What I do not understand is where the "Mon, 08 Jun 2015" is coming from. when I print out the date with the puts statement, it shows it as 2015-06-08...
Upvotes: 4
Views: 7482
Reputation: 114208
Rails converts the string to a date object. 2015-06-08
is the output from Date#to_s
:
d = Date.today
#=> Thu, 17 Jul 2014
puts d
#=> 2014-07-17
You can compare the date object to your string with either:
expect(exemplar.due_on.to_s).to eq(hash['due_on'])
or:
expect(exemplar.due_on).to eq(Date.parse(hash['due_on']))
or using Active Support Core Extensions:
expect(exemplar.due_on).to eq(hash['due_on'].to_date)
Upvotes: 5