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:
- Dart 1 for Everyone: Fast, Flexible, Structured Code for the Modern Web (pragprog.com/book/csdart1)
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.
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
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
Ever written a spec that ended like this:
expect(user.name).to eq("Noel Rappin")</br>
It's dreary and becomes hard to read.
have_attributes matcher to the rescue.
expect(user).to have_attributes(name: "Noel Rappin", height: 140, handedness: "left")
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:
In RSpec 3, you can also express the same expectation with
expect(name).to start_with("Noel").and end_with("Rappin")
If only one of those expectations needs to be true, which would be somewhat awkward to express before RSpec 3, use
expect(name).to start_with("Noel").or end_with("Rappin")
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
- The VimL Primer
- Arduino: A Quick Start Guide, Second Edition
- Hello, Android, Fourth Edition
- Metaprogramming Elixir
- Your Code As a Crime Scene
- Pragmatic Unit Testing in Java 8 with JUnit
- Dart 1 for Everyone
- Fire in the Valley
Thanks for your continued support,
Andy & Dave
The Pragmatic Programmers
Books • eBooks • PragPub Magazine • Audiobooks and Screencasts