GithubHelp home page GithubHelp logo

simple-stubs's Introduction

Test Doubles / Simple Stubs

Hey there! We're thoughtbot, a design and development consultancy that brings your digital product ideas to life. We also love to share what we learn.

This coding exercise comes from Upcase, the online learning platform we run. It's part of the Test Doubles course and is just one small sample of all the great material available on Upcase, so be sure to visit and check out the rest.

Exercise Intro

When testing one object, you may find that the other objects it interacts with get in the way. For example, when testing a controller, it can be cumbersome to ensure the model is used correctly:

describe PostsController do
  describe "#index" do
    it "finds the 10 latest published posts" do
      10.times { |i| create(:post, :published, title: "published#{i}") }
      create(:post, published: false, title: "unpublished")

      get :index

      expect(assigns[:posts].map(&:title)).to match_array(%w(
        published0 published1 published2 published3 published4 published5
        published6 published7 published8 published9
      ))
    end
  end
end

In this example, the setup and verification phases become complex and hard to follow because we need to re-test the logic to find 10 published posts, even though this logic is already implemented and tested in the model layer.

We can use stubs to clean it up:

describe PostsController do
  describe "#index" do
    it "finds the 10 latest published posts" do
      posts = double("latest_published")
      allow(Post).to receive(:latest_published).and_return(posts)

      get :index

      expect(assigns[:posts]).to eq(posts)
    end
  end
end

The posts variable above is called a "stub," which is a kind of "test double."

You can create test doubles in RSpec by using the double method:

posts = double("latest_published")

This creates an object that stands in for an actual list of published posts; it's like a stunt double for your actual object. Using the double method is very similar to calling Object.new, but tests that use doubles will fail with much clearer error messages. The "latest_published" string above is simply a name which will be used in test failure messages.

You can stub out a method on any object (including a test double) by using allow:

allow(Post).to receive(:latest_published).and_return(posts)

This changes the Post class to return posts whenever latest_published is called. Stubbed methods will revert to their regular behavior after each test runs.

Using the double and allow methods, you can create simple stubs.

In this exercise, you'll use simple stubs to clean up some tests.

If you'd like to see a quick intro to test doubles, check out the related episode of the Weekly Iteration.

Instructions

To start, you'll want to clone and run the setup script for the repo

git clone [email protected]:thoughtbot-upcase-exercises/simple-stubs.git
cd simple-stubs
bin/setup

After running bin/setup, edit spec/dashboard_spec.rb and look for test logic which is duplicated from spec/post_spec.rb. Use double and allow to create a stub for posts and eliminate this duplication.

Tips and Tricks

Useful links:

Featured Solution

Check out the featured solution branch to see the approach we recommend for this exercise.

Forum Discussion

If you find yourself stuck, be sure to check out the associated Upcase Forum discussion for this exercise to see what other folks have said.

Next Steps

When you've finished the exercise, head on back to the Test Doubles course to find the next exercise, or explore any of the other great content on Upcase.

License

simple-stubs is Copyright © 2015-2018 thoughtbot, inc. It is free software, and may be redistributed under the terms specified in the LICENSE file.

Credits

thoughtbot

This exercise is maintained and funded by thoughtbot, inc.

The names and logos for Upcase and thoughtbot are registered trademarks of thoughtbot, inc.

simple-stubs's People

Contributors

jferris avatar christoomey avatar r00k avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.