w2d3 Flashcards
What’s the difference between RSpec’s ‘eq’ and ‘be’ expectation matchers?
‘eq’ uses == to test equality
‘be’ compares object_id’s
In RSpect, wat are the #eq and #be methods called?
expectation matchers
How do you set up a context to run in RSpec?
with a ‘before’ block
Specifically, a before(:each) block.
Why do we avoid using a before(:all) block?
The test context is shared and can make tests brittle in that they become dependent on each other.
How do you write out a bunch of descriptions for specs without writing the bodies?
leave out the block on the ‘it’ blocks:
it ‘does something awesome’
it ‘does another great thing’
How does the RSpec::describe method work? How does it do this on the backend?
It takes a classname as an argument and stores a reference to it within the context’s metadata hash.
How do you do a comparison in a spec?
expect(actual).to be < expected
How do you do a delta comparison in a spec?
expect(actual).to be_within(delta).of(expected)
How do you test an exception in a spec?
expect { … }.to raise_error(ErrorClass)
What’s the relationship between subjects and lets?
a subject is the main subject of the test. There can be only one (unnamed) subject.
a let is a helper object. There can be many let’s in a test.
How do we set up a mock object?
double
Difference between unit tests and integration tests?
unit tests test objects in vitro
integration tests test groups of objects in vivo
How would you set up a double for a User object that has a name of “Jim” and an address of “123 main st” ?
person = double(“person”, :name => ‘Jim’, :address => ‘123 main st’)
How would you ensure that a particular method within a double is called, with a particular result? How would you write the test?
person = double(:name => ‘Jim’)
Renamer.do_rename(person)
expect(person).to receive(:name).with(‘Jim’)
expect(person).to receive(:name).and_return(‘Jim’)
Where do you declare the subject?
Outside of your it block.
The same subject will be used for all it blocks within a describe block.