By developers for developers.

This website uses cookies for account and order processing. By using this site you understand and agree to our use of cookies, our Terms Of Use, and Privacy Policy

Rails 4 Test Prescriptions in print; 50% off select paperback books

December 10, 2014

Now in print and shipping: Rails 4 Test Prescriptions: Build a Healthy Codebase, available from pragprog.com/book/nrtest2. Get ahead of the pack and whip your Rails apps into well-oiled figthing machines for the new year. Get a copy for everyone on your team!

And don't forget it's day two of our special Ten Days of Gerbils:

Save 50% on the featured paperback holiday gift book of the day. Today's selection is:

Finally, be sure to read this month's issue of PragPub magazine: all about Resource Oriented Computing. Don't miss it at theprosegarden.com (note the correct link this time!)

Rails 4 Test Prescriptions: Build a Healthy Codebase

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(&quot;Fred&quot;) 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(&quot;User&quot;) 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.

Don't Get Left Out

Are your friends jealous that you get these spiffy email newsletters and they don't? Clue them in that all they need to do is create an account on pragprog.com (email address and password is all it takes) and select the checkbox to receive newsletters.

Are you following us on Twitter and/or Facebook? Here's where you can find us and keep up with the latest news and commentary, and occasional discounts: