Rails 6 API Tutorial - API Tests with RSpec p.9

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi there so far in the rails api tutorial we've made good progress building out our apis and so far we've been testing those api endpoints using curl um but over time that becomes problematic because one it's hard to time consuming to form call requests and b we need to go back and test every single api when making a code change to make sure that we don't break things a better way to handle that would be unit testing so in this video i'm going to show you how to write unit tests with our spec our spec will allow us to write unit tests for all of these api endpoints and we can just run the unit tests to verify that we haven't broken any old functionality and we can also write unit tests as we are creating new endpoints to make sure that our new functionality works before committing it to source control or before pushing it to production so the first thing we need to do is in the gem file we need to install a gem for our spec now there's a there's a generic r spec gem but the one we want to use is our spec rails which is a version of our spec specifically made to work with rails so i'll take that and we want to install it under the development and test group so i'll go ahead and install that with with bundle now if i run our spec it actually runs the tests but we have zero tests at the moment so we get zero examples and zero failures the other thing we need to do is run rails generator spec this will create a spec directory and create the initial spec helper and rails helper files so these helper files are essentially used by all the tests that we create and they do things like uh include the correct rails directories uh things like that there we go now with that in place we should be in a position to create our first test and when when writing unit tests or integration tests over time you'll build up a structure in in spec so we could have model tests controller tests but what i'm going to write today is a request request spec so i'm going to create a directory requests let's create a file called bookspec.rb so this is the file where we're going to write our unit tests the first thing we want to do is require rails helper and so that we want to require the rails helper for all our specs essentially and now i can start writing the test so the first thing i'm going to do is wrap the create the high level describe block so i'll say describe books api and our spec allows you to optionally specify the the type of test you're writing so this is a request spec and inside i'm going to have a it's block which is the actual test so this should return all books so you can think of the describers containing all of the tests about the book api and then we can have it blocks for specific tests that we want to write and what we want to do is get make a get request to api v1 books which is our index controller and it should actually return all the books in the system and then we can write the expectation so we expect the response to have we can use this have hd http status success and i can now run that by doing r spec so when you run aspect it should run all of the tests in the spec directory and there you can see we have one spec was run zero failures so this is the one test in our system at the moment and that passes now all we're doing at the moment is calling this endpoint checking the response and checking that the response status is a 200 but that doesn't check whether there are any books actually being returned so what we want to do now is write another expectation where we say the [Music] response body dot size and we want to say that should equal two so we want to write a test where we say check the response and then check the number of books in the response body and check that that's equal to two the other thing i need to do with this is convert it from json because this this response will be a adjacent object so i need to do json parse response body so if i run this test we get a failure so we're expecting two objects and we get zero and that's what i would expect because we haven't actually created any books when we run aspect tests or any unit tests in rails it uses a separate test database and that database has to be seeded each time or we need to create some mock data there so right now the database is completely empty now the best way to create test data well there's a couple of ways you can do that but my favorite favorite way is to use factory bot which is another gem and it's a testing library that allows you to create factories of models essentially so i'm going to grab that i'm going to add that to the gem file in the same place as we added our spec so that's installed i don't think there's any initialization required for this gem so now all i need to do is inside spec i'll create a new folder called factories and inside here i'll create a book rb so this will have our factory bot definition and i'm going to keep it really simple for now just have a an empty factory definition now what we can do over time is build up the factory with defaults so we can say whenever a new factory is initialized we create a default email address or something like that but right now the book is so simple i'm just going to leave it leave it empty and then in the test what i can do is factory bot create book and then because i haven't added any defaults i need to specify the author and the name of the book so i can say 1984 author george apologies if i make any typos here and then i need to create two books to satisfy the test let's say sorry the time machine author h g wells okay so let's run these tests again oops okay so i get an error message factory not registered and i think that's because like our spec factory bot has a rails version that i should be using which means that i don't have to worry about including it in all the tests oh yeah factory about rails so i'll replace that bundle and i'll run the test again that looks better and let's jump back over here oh sorry it should be author and title so this is a title let's run the tests again there we go we've got one failing we've got one passing test so now we are running these two factory bot crate methods which create two books in our test database we call the api we check the api return successfully and then we check that the response body has two objects in it thanks for watching that's all for this video in the next one we'll be building out the tests for each of the other api endpoints
Info
Channel: Tom Kadwill
Views: 7,657
Rating: undefined out of 5
Keywords: Rails, Rails API, Rails API-only, Rails API FactoryBot, Rails API-only FactoryBot, Rails API FactoryGirl, Rails API RSpec, Rails API-only RSpec, GoRails, Traversy Media, Deanin, RSpec have_http_status, Rails RSpec expect, Rails API request specs, Rails API-only request specs, Rails 6 API, Rails 6 API-only, Rails 6 API Rspec, Rails 6 API-only Rspec
Id: -aO0TlPTHhA
Channel Id: undefined
Length: 11min 49sec (709 seconds)
Published: Sat Aug 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.