简体   繁体   English

如何在长生不老药中模拟当前时间

[英]How to mock current time in elixir

I have some tests which depends on current time and am not able to find a solution for it so far.我有一些取决于当前时间的测试,到目前为止我无法找到解决方案。 I have tried some mocking libraries like mock but it mocks the whole module and it fails.我尝试了一些mock库,如mock但它mock了整个模块并且失败了。 Any help will be really appreciated(if I only mock DateTime.utc_now everything is ok)任何帮助将不胜感激(如果我只模拟DateTime.utc_now一切正常)

Note: tests depends heavily on other DateTime and Date functions so mocking whole modules is not a very good option(I have tried this also but failed due to very complex cases and I need this in many tests)注意:测试在很大程度上依赖于其他DateTimeDate函数,因此模拟整个模块不是一个很好的选择(我也尝试过,但由于非常复杂的情况而失败,我在许多测试中都需要这样做)

Actual test: I have two dates, start date and end date as input to a function which I am trying to test.实际测试:我有两个日期, start dateend date作为我要测试的函数的输入。 Before calling the function for test purpose I insert some data relevent to the current week(current dat to next seven days).在调用该函数进行测试之前,我插入了一些与当前周(当前数据到接下来的 7 天)相关的数据。 Now the function will get current datetime and check for specific days(each record will tell if it applies to current day of the week and for current time period range on which being iterated -> start and end dates).现在该函数将获取当前日期时间并检查特定日期(每条记录将说明它是否适用于一周中的当前日期以及迭代的当前时间段范围 - > 开始和结束日期)。 eg one record applies for mon -> 2:12 to 3:13例如,一条记录适用于mon -> 2:12 到 3:13

The solution which best suits my needs(simple, works well and according to the requirements described above) is:最适合我的需求的解决方案(简单、运行良好并符合上述要求)是:

define your own function/service MyDateTime.utc_now/0 and mock it in your tests.定义您自己的函数/服务MyDateTime.utc_now/0并在您的测试中模拟它。 Reference . 参考

NB this answer is obsoleted since Elixir v1.8 Now the default parameters are not evaluated at compile time.注意这个答案自 Elixir v1.8 起已过时现在默认参数不在编译时评估。 Credits @HentikN.积分@HentikN。

Now the function will get current datetime and check for specific days [...]现在该函数将获取当前datetime并检查特定日期 [...]

This is where the real issue sits.这才是真正的问题所在。 You made your function not pure for no reason.您无缘无故地使您的功能不纯。 Usually the purity means the function has no side effects, but blindly changing the outcome depending on the outside world does not sound as a robust approach either.通常纯度意味着函数没有副作用,但盲目地根据外部世界改变结果听起来也不是一种稳健的方法。

That said, you should make this function to accept a parameter now or like (it might be defaulted to now for the sake of brevity):也就是说,你应该让这个函数now或像这样接受一个参数(为了简洁起见,它可能默认为now ):

- def my_func(param) do
+ def my_func(param, dt \\ nil) do
+   dt = if is_nil(dt), do: DateTime.utc_now(), else: dt

(Naïve dt \\\\ DateTime.utc_now() won't work because function heads are evaluated at the compile time .) (Naïve dt \\\\ DateTime.utc_now()将不起作用,因为函数头是在编译时评估的。)

Now in your tests you might call this function passing the time you want and (which is even more important) your function is not a blackbox depending on the unrelated conditions from the outside anymore.现在,在您的测试中,您可能会根据您想要的时间调用此函数,并且(这更重要)您的函数不再是依赖于外部无关条件的黑盒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM