2012-09-15 33 views
2

我有以下工廠:的Rails 3.2.8 + FactoryGirl + Rabl的:如何日期時間的差異返回

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 

及以下規格:

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 
... 

現在,一切都按預期工作(沒有雙關語)除了比較 :created_at和:updated_at字段之間的內容是什麼,以及什麼 來自FactoryGirl(location_params)。

我回來從運行規範的錯誤如下:

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"}] 

正如你可以看到響應的身體都created_at和的updated_at表示 不同於什麼得到由FactoryGirl返回。

這是什麼,我明顯在這裏失蹤?

在此先感謝您的幫助

回答

1

JSON沒有一個日期/時間類型 - 所有的日期/時間表示爲ISO8601格式字符串。因此,一旦數據已經從JSON編碼和解碼,您最終會將一個DateTime對象與一個字符串進行比較。