From 7412c8d1c824b4ce30bd5330d1a83e54cc1e8443 Mon Sep 17 00:00:00 2001 From: Rodrigo Ra Date: Thu, 25 Jun 2015 15:13:11 -0300 Subject: [PATCH] Fix transient tests failures --- active_model_serializers.gemspec | 1 + test/action_controller/serialization_test.rb | 44 +++++++++++--------- test/test_helper.rb | 1 + 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/active_model_serializers.gemspec b/active_model_serializers.gemspec index 3ae91fc15..a0279e3d6 100644 --- a/active_model_serializers.gemspec +++ b/active_model_serializers.gemspec @@ -22,5 +22,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency "rails", ">= 4.0" spec.add_development_dependency "bundler", "~> 1.6" + spec.add_development_dependency "timecop", ">= 0.7" spec.add_development_dependency "rake" end diff --git a/test/action_controller/serialization_test.rb b/test/action_controller/serialization_test.rb index a8c8f8279..82da8a674 100644 --- a/test/action_controller/serialization_test.rb +++ b/test/action_controller/serialization_test.rb @@ -1,3 +1,4 @@ + require 'test_helper' module ActionController @@ -56,7 +57,7 @@ def render_json_array_object_without_serializer end def update_and_render_object_with_cache_enabled - @post.updated_at = DateTime.now + @post.updated_at = Time.now generate_cached_serializer(@post) render json: @post @@ -112,7 +113,7 @@ def render_fragment_changed_object_with_relationship generate_cached_serializer(like) like.likable = comment2 - like.time = DateTime.now.to_s + like.time = Time.now.to_s render json: like end @@ -224,9 +225,6 @@ def test_render_array_using_implicit_serializer_and_meta end def test_render_with_cache_enable - ActionController::Base.cache_store.clear - get :render_object_with_cache_enabled - expected = { id: 1, title: 'New Post', @@ -246,11 +244,16 @@ def test_render_with_cache_enable } } - assert_equal 'application/json', @response.content_type - assert_equal expected.to_json, @response.body + ActionController::Base.cache_store.clear + Timecop.freeze(Time.now) do + get :render_object_with_cache_enabled - get :render_changed_object_with_cache_enabled - assert_not_equal expected.to_json, @response.body + assert_equal 'application/json', @response.content_type + assert_equal expected.to_json, @response.body + + get :render_changed_object_with_cache_enabled + assert_equal expected.to_json, @response.body + end ActionController::Base.cache_store.clear get :render_changed_object_with_cache_enabled @@ -306,20 +309,23 @@ def test_render_with_fragment_except_cache_enable def test_render_fragment_changed_object_with_relationship ActionController::Base.cache_store.clear - get :render_fragment_changed_object_with_relationship - response = JSON.parse(@response.body) - expected_return = { - "id"=>1, - "time"=>DateTime.now.to_s, - "likeable" => { + Timecop.freeze(Time.now) do + get :render_fragment_changed_object_with_relationship + response = JSON.parse(@response.body) + + expected_return = { "id"=>1, - "body"=>"ZOMG A COMMENT" + "time"=>Time.now.to_s, + "likeable" => { + "id"=>1, + "body"=>"ZOMG A COMMENT" + } } - } - assert_equal 'application/json', @response.content_type - assert_equal expected_return, response + assert_equal 'application/json', @response.content_type + assert_equal expected_return, response + end end def test_cache_expiration_on_update diff --git a/test/test_helper.rb b/test/test_helper.rb index a8b483553..1ba903b35 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,5 +1,6 @@ require 'bundler/setup' +require 'timecop' require 'rails' require 'action_controller' require 'action_controller/test_case'