I have the following factory:
FactoryGirl.define do
factory :location do |f|
f.descrizione { Faker::Company.name }
f.indirizzo { 'Yellow submarine lane, 1'}
f.citta { 'Nowhereland' }
f.cap { '0100' }
f.provincia { 'ZZ' }
end
end
and the following spec:
describe "/api/v1/clients/:client_id/locations.json", :type => :api do
let(:client) { FactoryGirl.create(:client) }
let(:url) { "/api/v1/locations" }
describe 'Locations index' do
it_behaves_like "requires a client"
def do_verb
get url+".json", client_id: client.id
end
describe "fetches all locations for a given client" do
it "returns an empty array of locations when client has no locations" do
do_verb
body = JSON.parse(last_response.body)
body.should eq([])
end
it "returns an array with client's locations" do
location = FactoryGirl.create(:location)
client.locations << location
client.save
do_verb
body = JSON.parse(last_response.body)
location_params = location.attributes
body.should eq([location_params])
end
end
end
...
Now, everything works as expected (no pun intented) except the comparisons between :created_at and :updated_at fields of what's inside the body and what comes from FactoryGirl (location_params).
The error I get back from running the spec follows:
Diff:
@@ -5,6 +5,6 @@
"cap"=>"01000",
"citta"=>"Nowhereland",
"provincia"=>"ZZ",
- "created_at"=>Sat, 15 Sep 2012 16:39:13 UTC +00:00,
- "updated_at"=>Sat, 15 Sep 2012 16:39:13 UTC +00:00}]
+ "created_at"=>"2012-09-15T16:39:13Z",
+ "updated_at"=>"2012-09-15T16:39:13Z"}]
As you can see both created_at and updated_at in response's body is represented differently from what gets returned by FactoryGirl.
What's it that I'm obviously missing here?
Thanks in advance for your help
JSON doesn't have a date/time type - all date/times are represented as strings in ISO8601 format. Therefore, once the data has been encoded and decoded from JSON, you end up comparing a DateTime
object with a string.