![]() You may think that you can achieve this by always using relative times, but this proves much more difficult than initially perceived. As mentioned previously, this is particularly important for creating static test cases. This will cause your whole application to run at a static time. To achieve a static date for your entire test environment, you can simply drop the following into config/environments/test.rb (or config/test/environment.rb if you use environmentalist): # Setting "now" to 10:00:00 AM an ordering and delivery system, or any system that encapsulates the concept of "availability" at a particularly fine-grained level), you may find it useful to "root" your test data at a particular point in time, allowing you to write very specific tests that don't fall victim to the unstoppable march of real time. Whenever time-sensitivity is a major priority for your application (e.g. Also, if you're a big fan of the chronic gem, then you can easily combine these two: avel(Chronic.parse('this tuesday 5:00')) doįor some applications, you may find it useful to completely re-base the concept of "now" for your test environment. ![]() ![]() You can pass a Time instance, a Date instance, a DateTime instance, or individual time parameters (the same arguments Time.local()) takes. What's nice about this API is that the #travel function will take several different object types. Consider the following test case: require 'timecop' The most basic example is to utilize this within a test. The gem is hosted on RubyForge and can be installed by simply running: sudo gem install timecop Nested time travel : I wanted to provide the ability to nest traveling, allowing the state to be kept within each block (we'll see an example later).This is particularly helpful when setting up a rails test environment along with the test data. Long-range time travel : I wanted to expose the ability to change the concept of "now" for an indeterminate period of time.Short-term time travel : I wanted to expose the ability to temporarily change the concept of "now." This is particularly helpful when writing tests where time needs to pass.Library independence : I could have utilized mocha to achieve the mocking functionality found under the hood, but because I wanted this to work with plain vanilla ruby, libraries like mocha are out.Environment independence : I wanted the gem to work (a) w/ rails (ActiveSupport actually), (b) w/ plain ruby when the 'date' library has been loaded, and (c) w/ plain ruby when the 'date' library had not been loaded.No overloading of functions with optional arguments (a la today=Date.today) just so you can write test cases. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |