Git Product home page Git Product logo

verifying-expectations-with-spies's Introduction

Upcase: Test Doubles

Verifying Expectations with Spies

Verifying expectations with spies for the Test Doubles course.

Before

# signup_spec.rb

describe Signup do
  describe "#save" do
    it "creates an account with one user" do
      signup = Signup.new(email: "[email protected]", account_name: "Example")

      result = signup.save

      expect(Account.count).to eq(1)
      expect(Account.last.name).to eq("Example")
      expect(User.count).to eq(1)
      expect(User.last.email).to eq("[email protected]")
      expect(User.last.account).to eq(Account.last)
      expect(result).to be(true)
    end
  end

  describe "#user" do
    it "returns the user created by #save" do
      signup = Signup.new(email: "[email protected]", account_name: "Example")
      signup.save

      result = signup.user

      expect(result.email).to eq("[email protected]")
      expect(result.account.name).to eq("Example")
    end
  end
end

After

# signup_spec.rb

describe Signup do
  describe "#save" do
    it "creates an account with one user" do
      email   = "[email protected]"
      account = double("account", name: "Example")

      stub_account_creation_with(account)
      stub_user_creation_with(account, email)

      signup = Signup.new(email: email, account_name: account.name)
      result = signup.save

      expect(Account).to have_received(:create!).with(name: account.name)
      expect(User).to have_received(:create!).with(account: account, email: email)
      expect(result).to be(true)
    end
  end

  describe "#user" do
    it "returns the user created by #save" do
      email   = "[email protected]"
      account = double("account", name: "Example")
      user    = double("user", account: account, email: email)

      stub_account_creation_with(account)
      stub_user_creation_with(account, email).and_return(user)

      signup = Signup.new(email: email, account_name: account.name)
      signup.save

      result = signup.user

      expect(result.email).to eq("[email protected]")
      expect(result.account.name).to eq("Example")
    end
  end

  def stub_account_creation_with(account)
    allow(Account).to receive(:create!)
      .with(name: account.name)
      .and_return(account)
  end

  def stub_user_creation_with(account, email)
    allow(User).to receive(:create!)
      .with(account: account, email: email)
  end
end

verifying-expectations-with-spies's People

Contributors

christoomey avatar jferris avatar lujanfernaud avatar

Watchers

 avatar  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.