Rails 4 Test Prescriptions

Build a Healthy Codebase

by Noel Rappin

Pages

350

Published

2014-12-06

Release

P1.0 (2014-12-10)

ISBN

978-1-94122-219-5

Does your Rails code suffer from bloat, brittleness, or inaccuracy? Cure these problems with a regular dose of test-driven development. Rails 4 Test Prescriptions is a comprehensive guide to how tests can help you design and write better Rails applications. In this completely revised edition, you’ll learn why testing works and how to test effectively using Rails 4, Minitest 5, and RSpec 3, as well as popular testing libraries such as factory_girl and Cucumber. Do what the doctor ordered to make your applications feel all better. Side effects may include better code, fewer bugs, and happier developers.

Choose Your Format(s)

eBook + Paper$48.00In Stock

Save $15.00 on the combo pack.

Paper Book$38.00In Stock

eBook$25.00In Stock

Ebooks are
DRM free.

Ebook delivery options.

About This Title

This BookBeginnerExpert

Your Ruby on Rails application is sick. Deadlines are looming, but every time you make the slightest change to the code, something else breaks. Nobody remembers what that tricky piece of code was supposed to do, and nobody can tell what it actually does. Plus, it has bugs. You need test-driven development, a process for improving the design, maintainability, and long-term viability of software.

Containing both practical code examples and discussion of why testing works, this book starts with the most basic features delivered as part of core Ruby on Rails. Once you’ve integrated those features into your coding practice, you’ll learn how to use popular third-party testing tools such as RSpec, Mocha, Cucumber, and factory_girl. You’ll test the component parts of a Rails application, including the back-end model logic and the front-end display logic. Using Rails examples, you’ll learn how to use testing to enable your code to respond better to future change. Plus, you’ll see how to handle real-world testing situations.

This completely revised edition contains a new tutorial, as well as new examples throughout the book. Many chapters, including the JavaScript chapter, have undergone major changes to reflect new tools and new practices. And there are brand new chapters on testing for security, and testing external services.

Three RSpec 3 Features You Should Try

RSpec 3 is a major upgrade. In addition to solidifying changes in RSpec’s syntax and internals, it also added a number of new features. With these new features, you can specify the behavior of your application more flexibly and more exactly then before. Here are three new features of RSpec 3 or 3.1 that can make your tests more clear and precise.

For much more information on how to use RSpec in the context of test-driven development of a Rails application, read Rails 4 Test Prescriptions!

1. verifying doubles

A typical RSpec test double (sometimes called a mock object) is used to stand in for a real object in your system. However, the double does not know anything about the object it is replacing. This leads to a common problem where you specify that a double should respond to a method that is not actually defined in the code. As a result, the test passes, but the code calling that non-existent method fails when the code is executed outside the test.

Verifying doubles solves this problem by allowing you to give RSpec a little bit of information about the object being replaced so that RSpec can fail the test if an undefined method is called. With the configuration option `verify_partial_doubles` set, then any double you set on an existing object will validate that the object responds to the message. For example, if you say `allow(user).to recieve(:name).and_return(“Fred”)` then the verifying double will cause the test to fail if `User#name` is not defined.

You can also create verifying doubles that are not extensions of existing instances by using variants of the RSpec `double` method. These extensions include `instance_double(“User”)` and `object_double(User.new)`. In both cases, the resulting double will insist that any message expectation sent to it be already defined -in this case, already defined in the `User` class.

Brought to You By

Noel Rappin is a senior developer and director of talent at Table XI, and the author of multiple technical books. Before joining Table XI, Noel ran internal training at Groupon, and has a Ph.D. in educational technology and user-centered design from the GVU Lab at the Georgia Institute of Technology. Find Noel online at noelrappin.com.

Rails 4 Test Prescriptions is quite simply the best book on the market on the topic
of testing Rails applications. It’s full of distilled wisdom from Noel’s many years
of experience. I especially love the emphasis on thinking through the tradeoffs
involved in picking a test strategy; in my experience, thinking intentionally about
those tradeoffs is one of the most important steps you can take toward building
an effective test suite.

Myron Marston, Lead maintainer of RSpec and creator of the VCR gem

Rails 4 Test Prescriptions will benefit both developers new to test-driven development
and those who are more experienced with it. Noel Rappin presents concepts
like mocking and stubbing in a very detailed but also approachable and entertaining
way. I loved the first edition of this book, and the second is even better. I
highly recommend it!

Nell Shamrell-Harrington, Senior developer, PhishMe

If anyone asks me how to master testing in Rails applications, I will tell them to
read this book first.

Avdi Grimm, Head chef, RubyTapas

Sometimes testing sucks. This book magically makes testing not suck; it makes
it easy and rewarding with well-written explanations. It is the essential resource
for any developer testing Rails applications. It’s more than just a testing primer;
developers will learn how to create optimal and efficient test suites for Rails. A
must-read for beginners and seasoned programmers alike.