April 22, 2017
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
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
- Want to use let(:name)
- Runs once
- Caches name2
Two reasons:
- Want to do some setup before defining
- Performance issues
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.
- 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
- Another way to have let
subject(:hero) { Hero.first }
Example: never in Rails.env.production?
allow(Rails.env).to receive(:production?).and_return(true)
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.
- Everything you're talking to is an API
- For every single service, you're talking to - have to stub out the response.
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.
-
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*
- RSpecs is doing this for us under the hood
- Nested describe, before, let blocks
- Expect
Thanks to Hafseb