Before adding more functionality lets dig a bit more deeply into how RSpec

Before adding more functionality lets dig a bit more

This preview shows page 320 - 322 out of 517 pages.

Before adding more functionality, let’s dig a bit more deeply into how RSpec works. RSpec’s should is a great example of the use of Ruby language features to improve readability and blur the line between tests and documentation. The following screencast explains in a bit more detail how an expression such as value.should == 5 is actually handled. Screencast 8.5.1: How Ruby's dynamic language features make specs more readable RSpec mixes a module containing the should method into the Object class. should expects to be passed a matcher that can be evaluated to the condition being asserted. RSpec methods such as be can be used to construct such a matcher; because of Ruby’s flexible syntax and optional parentheses, an assertion such as value.should be < 5 can be understood by fully parenthesizing and de-sugaring it to value.should(be.<(5)) . In addition, RSpec uses Ruby’s method_missing feature (described in Chapter 3 ) to detect matchers beginning with be_ or be_a_ , allowing you to create assertions such as cheater.should be_disqualified . (Note: The spec shown at the beginning of this Beta Edition screencast doesn’t correspond to the example being developed in this section. However, this doesn’t affect the main point of the screencast, which is to illustrate in detail how should works in RSpec.) (Further Note: you may need the command require ’rspec/expectations’ to get the examples in this screencast to work.)
Image of page 320
Summary When a test needs to operate on a real object rather than a mock, the real object can be created on the fly by a factory or preloaded as a fixture. But beware that fixtures can create subtle interdependencies between tests, breaking I ndependence. Tests are a form of internal documentation. RSpec exploits Ruby language features to let you write exceptionally readable test code. Like application code, test code is there for humans, not for the computer, so taking the time to make your tests readable not only deepens your understanding of them but also documents your thoughts more effectively for those who will work with the code after you’ve moved on. ELABORATION: New expectation syntax As of RSpec version 2.11, a new and somewhat different expectation syntax is also supported. For example, rather than writing foo.should==5 , we can now write expect(foo).to eq(5) . This article describes both the rationale for the change and the reason we cannot write expect(foo).to==5 ; both arguments are subtle. While the “classic” should syntax is still supported, the new expect - style syntax more closely resembles how expectations for JavaScript tests are written in Jasmine (Section 6.7 ). Figure 8.19 shows a partial list of correspondences between the “classic” and new syntaxes, based on the complete RSpec documentation . Self-Check 8.5.1. Suppose a test suite contains a test that adds a model object to a table and then expects to find a certain number of model objects in the table as a result. Explain how the use of fixtures may affect the Independence of the tests in this suite, and how the use of Factories can remedy this problem.
Image of page 321
Image of page 322

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors