vendredi 6 février 2015

Replace repeated timestamp with variable in tests


I have couple hundred tests and I work with date/time a lot. In some tests, I aim for output format, elsewhere, I check date ranges. Therefore, my tests have lots of these:



FactoryGirl.create(:foo, ended_at: Time.zone.local(2014, 5, 5, 22, 15))


I thought, maybe there's a shortcut to these, since in some test files, I only work with few timestamps.



FactoryGirl.create(:foo, ended_at: may_5_22_15)


Where may_5_22_15 would be defined in method or using let.


Is this a good way to go?


Update: More context (thanks @Snowman for pointing out).


Motivation for this is to improve readability of testsuite. It makes it easier to me to read "Foo ended at May 5th by 22:15" rather than parsing a bunch of numbers in Time constructor.


The model has bunch of date/time attributes (ended_at, started_at, closed_at...) and the more specs I write, the more Time constructors I use and re-use. So I thought maybe I could do something like this:



let(:may_5_22_15) { Time.zone.local(2014, 5, 5, 22, 15) }


What I'd like to know is whether this is a good practice or not, in terms of test suite maintenance, readability or if there are any reasons why not to do it.





Aucun commentaire:

Enregistrer un commentaire