ruby on rails - Rspec surrounding shared_example with timecop -


i trying wrap timecop around shared_example spec wrote

describe "timecop wrapping shared example"    timecop.freeze(1.day.ago)       it_behaves_like "a shared example test using timecop"    end end  shared_examples "a shared example test using timecop"    before :all       puts "the current time #{time.now}"    end     ... expect ... end 

but running spec still uses real time , not frozen one

can timecop work this?

how else can wrap large pieces of test file change time running

timecop needs execute within or before block.

the following change fix problem.

describe "timecop wrapping shared example" before { timecop.freeze(1.day.ago) } it_behaves_like "a shared example test using timecop" end

rspec reset environment(including timecop) after running each example.

a couple of tips i've found time travel in tests might useful:

  • timecop.travel more reflective of how code work in real life time never stays still. can use same way freeze, swap out freeze method travel.

  • if have rails 4.1 or newer there great time travel tools built in , can drop timecop gem. here blog post , the docs if want learn more.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -