tanweerdev

tanweerdev

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. Any help will be really appreciated(if I only mock DateTime.utc_now everything is ok)

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)

Actual test: I have two dates, start date and end date as input to a function which I am trying to test. Before calling the function for test purpose I insert some data relevent to the current week(current dat to next seven days). 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).
e.g one record applies for mon → 2:12 to 3:13

Showing Posts 1 to 5

idi527

idi527

Is it possible for you to make the current time an argument into the function, just like start date and end date? Then you probably wouldn’t need to mock it.

tanweerdev

tanweerdev OP

Its possible(not very good option but yes) and I think the only solution (but it will fail in some cases e.g controllers shouldn’t send current date as option to this context method. but for testing context itself it will work)

nresni

nresni

Or define your own function/service MyDateTime.utc_now/0 and mock it in your tests.

idi527

idi527

not very good option but yes
but it will fail in some cases e.g controllers shouldn’t send current date as option to this context method.

Some other function in your context can do that.

# in controller
def post(conn, params) do
  # ...
  Context.do_stuff(with: command)
  # ...
end

# in your context
def do_stuff(with: %Command{...} = command) do
  do_stuff(with: command, time: DateTime.utc_now())
end

# false, since it's used directly only in tests
@doc false
def do_stuff(with: command, time: time) do # this is what you test
  # ... your logic
end
tanweerdev

tanweerdev OP

Thank you sooo much @idi527 for giving me great options. But @nresni 's answer suits best for my needs.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews