kanishka

kanishka

I’m debating between using exvcr stubs or just generating lots of custom cassettes to exercise edge cases of an http get request against an external endpoint. I’m curious about the forum’s thoughts on the pros and cons of each.

Showing Posts 1 to 10

BartOtten

BartOtten

kanishka

kanishka OP

Interesting but too heavyweight for my tastes.

dimitarvp

dimitarvp

That article actually re-convinced me to use mock and not to roll my own mock server. :003:

I have already used all of them: exvcr, mock, mox and bypass. For testing code that relies on underlying HTTP client libraries I liked mock the most with bypass a close second.

mox is amazing for a lot of testing but it doesn’t help me mock the underlying HTTP client library. I’ll use mox when I write higher level code that tests working with my API client itself e.g. I will make it return {:error, :too_many_retries} and see if the higher-level code reacts properly to that.

exvcr I tried to use just today but it was too opaque for me and even though the docs / GitHub page are rich on details I was unable to achieve a simple scenario – namely have two use_cassette blocks one inside the other (as I was testing pagination and wanted two different requests mocked, and yes I used its query parameter distinction parameter). Plus it wasn’t obvious how to use recorded (custom) cassettes. I simply gave up after 10 minutes and reached for mock which took the same amount of time and worked right away with zero surprises (NOTE: I only used mock twice before and the last time was more than a year ago).


Not to incline future readers too much in any particular direction as libraries evolve and get better (though some don’t) but so far mock was the best for testing code that relies on a lower level library (by mocking the said lower-level library seamlessly) and mox for testing everything in your own code where you have full control.

dorgan

dorgan

Folks should really give patch v0.16.0 — Documentation try(also see the cheatsheet)
The ergonomics and guarantees you get with Patch are far better than Mock or Mox in many cases. There is a comparison if you want to know how each mocking library behaves under different scenarios

kanishka

kanishka OP

I am liking exvcr a lot now. For error cases, I started with stubs, but I am going to switch to a combination of stubs and recorded cassettes with shorten timeouts in the code under test.


Once you get in direct call, delete, and record workflow, it gets pretty efficient doing happy path tests with exvcr. The only thing that can be tricky is remembering to not use random data generation when creating input data, so that your requests match fixed data in cassettes. (I wish there was a way to specify only generating new random data when exvcr is recording new cassettes.)

Workflow:

  • Edit code under test
  • Comment out use_cassette calls
  • Adjust tests and confirm updates work with live calls
  • Delete cassette, enable use_cassette again
  • Run tests triggering recording of new cassettes for deleted cassettes
dimitarvp

dimitarvp

Well I still have no clue how to pre-record cassettes so there’s that. :smiley:

kanishka

kanishka OP

You can use one cassette and record multiple requests in that cassette.


For custom cassettes, you just specify the location to the custom cassette. It’s usually easier to record a cassette and then modify it into a custom cassette.

dimitarvp

dimitarvp

Maybe I can’t talk properly but again, I seriously don’t know what to do to record a cassette EXPLICITLY – not implicitly by running a test and allowing ExVCR to store it, not that – so I can reuse it later with the custom cassettes path.

Can you give me a working example?

The workflow I am interested in:

  1. Run $mysterious_command to record an HTTP response
  2. Store file under custom/cassettes/path
  3. Specify the above path in my tests

I don’t know how to do #1.

kanishka

kanishka OP

You select an adapter from the exvcr tutorial. Then add the appropriate setup to an exunit test for your adapter. Then wrap your call in “use_casette” with your desired cassette name. Then run the test once. After the test has run, you will find the recorded cassette in your cassettes directory.

When I am at my computer tomorrow, I will try to detail out every step above with code snippets. We use hackney/httpoison at work, in case you are using a different adapter which may have issues that I am unaware of.


For the explicit case, there is an iex helper, but I haven’t used it.

dimitarvp

dimitarvp

OK so it’s how I thought – you have to run the test once for that. And then just point ExVCR to the place where you moved the file afterwards (the so-called custom cassettes path). Alright.

Haven’t looked up the IEx helper but you might be right that it could do explicit cassette recording. Hm, I might give it a try.

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
caslu
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
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
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
marciol
It would be helpful to have a list of companies worldwide that hire engineers without prior experience in Elixir. Often, it can be quite ...
New
durvia
Anyone running long-lived stateful processes on BEAM? We’re building an AI agent runtime and would love to compare notes. We’re a small ...
New

Other Trending Topics Top

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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews