Skip to content

Instantly share code, notes, and snippets.

@kattak
Last active June 30, 2017 21:08
Show Gist options
  • Save kattak/eef9eac5cab6f7e059032ac11b83f1ca to your computer and use it in GitHub Desktop.
Save kattak/eef9eac5cab6f7e059032ac11b83f1ca to your computer and use it in GitHub Desktop.

April 22, 2017

Recreating RSpec

Idea from WAT guy

describe MeetupUser do
...
end 

Could also have a string - if you want a bunch of tests for a concept.

describe "hi there" do
...
end 

Lazy vs. Eager

Pupr

let(name){value} #lazy
let!(:name2){value2} #eager
  • lazy: makes it when you want it
  • eager: runs it first

The order in which Ruby parases it.

  • Only when it sees
expect(name2).to eq("Harry")

eq("Harry") is the predicate

Generally

  • Want to use let(:name)
  • Runs once
  • Caches name2

Two reasons:

  • Want to do some setup before defining
  • Performance issues

let!

Need some thing to exist in the database for some side-effect.

Example: There are disaster zones in Airbnb. Would want to call with let! because then need to see effects in the database.

Before

  • Good for setup, i.e.Redis.set key
  • Good for something that you only need a side effect for
  • BUT if you need a reference to something you've created before -Then you would want to use let! so you can keep a reference to it

Subject

  • Another way to have let
subject(:hero) { Hero.first }

Allow

Example: never in Rails.env.production?

allow(Rails.env).to receive(:production?).and_return(true)

Stubbing

Stubbing: " programs that simulate the behaviors of software components (or modules) that a module undergoing tests depends on"

Example: VCR, gem that makes external API calls, saves response. Now deterministic. Hits Github API - saves response.

Service Oriented Architecture

  • Everything you're talking to is an API
  • For every single service, you're talking to - have to stub out the response.

Chron Job

Runs a test at a scheduled time.

  • Heroku equivalent: Heroku Scheduler In production: When you're working on a codebase, set up chron jobs to monitor performance.

Might also want to stub external classes

  • Unit tests: test one particular component, testing a SINGLE component

  • Integration tests: test components as integrated, "end-to-end testing" "Stub out all of the dependencies"

  • Not testing external dependencies, testing sending out correct methods

  • Controller specs

Everything that happens in your app, should go through your controller

  • Cannot have expect without stubbing*

Overwrite method

  • RSpecs is doing this for us under the hood

Core Features

  • Nested describe, before, let blocks
  • Expect

Thanks to Hafseb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment