Learn how to test-drive your Rails application from scratch. This course covers everything you need to start your TDD journey: you'll learn the main principles behind test-driven development, and you'll see how to write acceptance, controller, and model specs. You'll also learn how to use Factory Girl gem to prepare data for your specs and how to test most common cases in web development. By the end of this course, you'll see how all these parts come together while we test-drive features for real Rails applications.
Rem is an independent Ruby consultant from Russia. He enjoys learning new stuff, is obsessed with efficiency, and loves to share his knowledge with others.
Section Introduction Transcripts
Section Introduction Transcripts
TDD 101 Hi, and welcome to Test Driven Development 101 module. My name's Rem Zolotykh, and in this module we'll learn everything we need to know about TDD and its red green refact magic. We start by learning about red green refactor cycle, what it is and how it's different from standard development approach and we'll see the cycle in action on a simple example. Then, building on our experience, we'll formalize a test structure. Then we'll dive into RSpec, we'll set it up and learn the basics we absolutely must know to continue with this course. After that, we'll combine everything we've learned so far about TDD and RSpec by solving famous bowling game kata. And in the end, we'll talk about different types of tests we'll write in this course. So I hope you're excited. Let's get started.
Acceptance Tests Hi, and welcome to Acceptance Tests module. In this module, I stop boring you with theory at last, and we'll start coding. So here is a quick overview of topics we'll cover. We install and set up Rails project with RSpec and Capybara. We write complete feature spec by using Capybara helpers to navigate pages and fill in forms. We'll learn about Page Object pattern, which can help us to make our test code clean and eliminate implications in it. We'll learn how to use Factory Girl gem to create data for our tests. And we'll have a glimpse in Cucumber, and we'll see how it works on a simple example. I hope you're excited, so let's get started.
Controllers Tests Hi, and welcome to Controllers Tests module. In this module, we'll see in practice how to test all seven standard controller actions in Rails. So first, we start with an overview of controller's responsibilities. Then we get down to business and write tests for all seven actions, Index, New, Create, Show, Edit, Update, and Destroy. And then we see how to test that controller actions to be protected against unauthorized requests. So, let's get started.
Models Tests Hi, and welcome to model spec module. After all we came through together in previous modules, this module will be a breeze. You already know everything you need to know to test active record models. So in this module, first we will have quick talk about model's responsibilities. Then we'll see how to test validation rules, and we'll do it manually. And then we see how to use Shoulda Matchers gem to make our tasks very concise, readable, and easy to write. Then we'll see how to test associations between models. And then we'll see how to test instance methods on model, which often hold business logic of our application. And in the end, we'll see how to test database queries, which are made from model's class methods. So, let's get started.
Testing in Isolation In the beginning of this course, we talked about three types of tests: acceptance, integration and unit tests. And, attentive listener, notice that by far, we tested all Rails' parts using acceptance and integration tests. And that's totally okay because they are much easier to write, but they are much slower to execute. In this module, we'll see how testing in isolation works. First, we'll learn what mocks and stubs are and why we need them to be able to test in isolation. Then, we'll take some time to understand how dependency injection works and why it's good. Then we'll talk about two types of messages objects usually send to each other. Then we'll try these concepts out on a simple non-Rails example. We'll learn how to mock and stub using our spec. And in the end, we'll return to Rails and see how we can apply this knowledge to test Rails' controllers in isolation.
Testing Other Cases Hi, and welcome back to test-driven Rails course. In this module, we'll learn how to test other not so Rail-specific features, but extremely common ones in web development. So, we will start with emails, and learn how to test email delivery, as well as how to test Rails' mailers in isolation. Then, we'll see how to test file uploads, and as a result, we'll implement actual file upload in for our achievements, which doesn't work now. Then, we'll see how to test features that use third-party APIs. In our example, we'll use Twitter API to tweet automatically about our achievement when we create one. And in the last clip, we'll see how to test our own API and learn about active model serializers gem and JSON API's pack. I hope you're excited, so let's get started.
Test-drive Complete Feature Hi, and welcome to the last module of the test-driven Rails course where we'll develop complete feature from start to finish using test-driven development approach. Here is the feature we want to have (mumbles). We want users to encourage other users' achievements. Let me show you how it works. So on Achievement page, you can see here, the Encouragements section, and right now, we have 0 Encouragements, and Encouragement link. So when I click on it, I have an error, You can't encourage yourself, because I'm the author of this Achievement. So, let me log out, and Sign up for another test02 account, and now, if I go back to that particular Achievement, the first one, and try to encourage it, I see this Message, Say something nice. . . , You rock! I encourage, and now I can see that there is 1 Encouragement, and I can see the Encouragement itself, and if I try to Encourage again, I fail because You already encouraged it. You can't be so generous! So this is the feature that we're going to work on in this module. So without further ado, let's get started.
Closing Thoughts I hope you've learned a lot in this course. But you can know all theory in the world, but without practice, it's useless, so I encourage you to try things out on your own throw away projects. What I like to do when I'm learning new technology, language or framework is to take problem that I already solved using different technology and try to solve it again using this new one. This way, I do not need to think hard about the problem, but instead, I can focus on mastering new things. So, if you're new to TDD approach, it could be very scary to introduce it into your current project. Instead, practice a little bit by creating in TDD way, throw away applications. Here are a couple of examples of what you can practice on. Book Collector application. These are the simple crod application to collect books and organize them by categories and genres. This way, you'll practice to test whole stack, validations and model associations. Another here is Github issues. Fetch issues for a particular repository and make stuff to them like eden nodes or favortin or anything like that that strikes your fancy. This small project will help you to practice test in third party API's. The thing with testing is, the better you know and understand object oriented design, the more sense TDD will make for you. And that's why I highly recommend Sandy Mat's book, Practical Object-Oriented Design In Ruby. This book also has a great chapter on testing. And that's it for this course. Thank you very much for your time. I hope you learned something new from it, and see you next time. Take care.