small medium large xlarge

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("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.

2. The have_attributes matcher

Ever written a spec that ended like this:

expect(user.name).to eq(&quot;Noel Rappin&quot;)</br> expect(user.height).to eq(140)</br> expect(user.handedness).to eq(&quot;left&quot;)

It's dreary and becomes hard to read.

RSpec's have_attributes matcher to the rescue.

expect(user).to have_attributes(name: &quot;Noel Rappin&quot;, height: 140, handedness: &quot;left&quot;)

You'll get a nice error message telling you which attribute or attributes don't match.

3. Compound Matchers

RSpec 3 allows you to chain matchers using the and method to chain two matchers that both must pass, and the or method to chain two matchers where only one needs to pass.

For example, let's say you were checking that an inputted value had a couple of different characteristics. You might have in the past have done this:

expect(name).to start_with(&quot;Noel&quot;)</br> expect(name).to end_with(&quot;Rappin&quot;)

In RSpec 3, you can also express the same expectation with and:

expect(name).to start_with(&quot;Noel&quot;).and end_with(&quot;Rappin&quot;)

If only one of those expectations needs to be true, which would be somewhat awkward to express before RSpec 3, use or:

expect(name).to start_with(&quot;Noel&quot;).or end_with(&quot;Rappin&quot;)

Now available from pragprog.com/book/nrtest2.

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:

Tell your friends! Tweet this

Follow us on Twitter: @pragprog, @pragpub, Andy @PragmaticAndy and Dave @pragdave.

Coming Soon:

  • The VimL Primer
  • Arduino: A Quick Start Guide, Second Edition
  • Hello, Android, Fourth Edition
  • Metaprogramming Elixir

Recently Released:

Thanks for your continued support,

Andy & Dave The Pragmatic Programmers

Books • eBooks • PragPub Magazine • Audiobooks and Screencasts
PragProg.com