![]() ![]() Then run our tests again to record new ExVCR cassettes. $ MIX_ENV=test mix vcr.delete price_fetch_success Then let’s go back to the command line and delete our other cassette. Let’s use a directory named “coin_cassettes”.Į_library_dir("fixture/coin_cassettes") In our setup block let’s add a call to _library_dir passing in the new directory we want these cassettes stored in. Let’s try one configuration option.īack in our coin_service_test.exs let’s change the directory that we store the cassettes for these tests in. $ MIX_ENV=test mix vcr.delete price_fetch_errorĮxVCR has a few different options as well, which you can find in the README. Let’s use mix vcr.delete to delete our “price_fetch_error” cassette. There’s mix vcr to list all of the cassettes, mix vcr.delete to delete a given cassette, mix vcr.check to check the cassette use for a given test, and mix vcr.show to show the contents of a given cassette. ![]() We can use $ MIX_ENV=test mix vcr -help to list all the different tasks provided. Now let’s look at some other features of VCR. Let’s open the “fixtures/vcr_cassettes” directory and great, our “price_fetch_error.json” cassette was recorded. It looks like our test passed, but let’s check that a new cassette was recorded. $ mix test test/teacher/coins/coin_service_test.exs:18 The we’ll go back to the command line and let’s run only our new test, which we can do by adding the line that our test starts on. Use ExVCR.Mock, adapter: ĭescribe "CoinService.fetch_current_price/1" do ![]() And then once the cassette is recorded every subsequent time we run our test it will then use that cassette instead of calling the API.ĭefmodule do The first time we run our test, this will call the API and record the request and response as a cassette. This takes a name, so let’s call the cassette for this test price_fetch_success. To do that we’ll wrap the inside of our test with the use_cassette macro we get from ExVCR. Because the price returned from this function changes depending on what’s returned from the API, we need to tell ExVCR to record the API request and response. To get the current price, let’s run the fetch_current_price function and use the current price that’s returned. We’ll call our function to get the current price of a Bitcoin, pattern matching on the OK tuple that’s returned and then assert that the returned price matches the current price. We’ll add a describe block for CoinService.fetch_current_price, then let’s test that we are able to fetch the current price of a cryptocurrency. Then let’s add a setup block and start HTTPoison from it. So instead of bringing in another dependency let’s configure ExVCR to use Hackney. We’ll include use ExVCR.Mock and by default ExVCR uses ibrowse, but we are already using the HTTPoison package, which uses Hackney. Now let’s setup our test to use ExVCR to mock the API call. Then we’ll create our new test file coin_service_test.exs and define our test module, using ExUnit.Case. We’ll create a new “teacher” directory and then a “coins” directory inside of it. Since our CoinService module is in the “coins” directory, let’s match that in our “test” directory. Then let’s go to the command line and install it. Then let’s open our mixfile and add it to our list of dependencies. To start let’s go to Hex and get the exvcr package. This way we wont hit the API every time we run our test and we get the full response we get in production. ExVCR allows us to record HTTP requests and responses and then replay them in our tests. Let’s use the ExVCR package to help test this. Because this functions calls to the CoinCap API to get the current price or a cryptocurrency we need to determine how we want to handle this in our test. In this episode let’s add some tests for our fetch_current_price function. We see we get an OK tuple returned if we’re able get the price successfully and an error tuple if it’s not able to fetch the price. We’ll start an iex session with our project. Here here we have a module that fetches the current price of a cryptocurrency. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |