rbenv versions: lists all ruby versions managed byrbenvruby -v: shows active dir ruby set by.ruby-versionfilegem -v: shows the ruby gem version manager version
gem install rspec
rspec --init
-
example group block:
RSpec.describe 'Card' do end
-
examples are tests that sit inside an example group
- use
itblocks for examples themselves in the example group block specifyis an alias forit
- use
-
expectis anrspecmethod to set expectations within an example- these
expectstatements are called assertions
- these
-
a single example can have multilpe assertions/expectations
- three ways:
- run all tests in the root dir and the nested dirs i.e. the entire project by simply doing
rspecin the CLI - do
rspec spec/card_spec.rbto run all test groups in an individual file - do
rspec ./spec/card_spec.rb:3i.e. with the line number to run only that line's example
- run all tests in the root dir and the nested dirs i.e. the entire project by simply doing
- be careful of mutation of variables in the sequence of test example execution
- sometimes it makes more sense to have isolated duplicate instance varible creation in examples
-
code that runs automatically at the time of test suit execution
before do ... end
is the same as
before(:example) do ... end
- other parameters that
beforecan take are:suitand:context
- other parameters that
-
afteris another hook
- useful to reduce duplication of instance varibles for each example
- will be the go to method for reducing duplication
letis a way to memoize (cache) something given to it
let(:card) { Card.new('Ace','Spades')}letinstanciates a new item specifed before each example (i.e.itblock)letby default implments lazy-loading- i.e. creates a card only at the time it needs to exist
let!()bypasses lazy loading
.totakes a second argument (a string) that is the message to show when the assertion fails
- they are aliases of each other
- while
it/specifyshould be matter of fact statements- more abstract ideas should use
describe/context - do not try to stuff complex scenario descriptions in an
it(example's) docstring
- more abstract ideas should use
- use as many nested
describe/contextas necessary to structure the test in a readable manner
-
subject:- implicity an instance of the class passed to
RSpec.describe - explictly can be called with a
do..endblock to lazy load an explicit definition of an instance of the described class
- implicity an instance of the class passed to
-
described_class:- dynamically refers to the class passed in as an argument to
RSpec.describe
- dynamically refers to the class passed in as an argument to
-
is_expected:- one liner assertion with the
itkeyword - utilizes the
subjectdefined directly
RSpec.describe 'shorthand_syntax' do subject { 5 } context 'with one-liner syntax' do it { is_expected.to eq 5 } end end
- one liner assertion with the
RSpec.shared_examplesto create shared examples - in ado..endblockinclude_examples- to call the shared examples- refer to by the docstring used to define the shared examples block
RSpec.shared_contextto create shared context - in ado..endblockinclude_contextto load shared context- refer to by definiton docstring
eq: checks for value sameness with type coercioneql: checks for value sameness without type coercionequal/be: checks for same memory location (obviously along with value sameness)
-
methods with
?at the end of a method name- it always returns a boolean
-
the
?is not a technical specification per-se but a popular community standard -
examples
puts 0.zero? # true puts 15.zero? # false puts 2.even? # true puts 3.even? # false puts 3.odd? # true puts 2.odd? # false puts [].empty? # true puts [1, 2].empty? # false
be_<predicate_method_name>
RSpec.describe 'predicate methods and predicate matchers' do
it 'can be tested with Ruby methods' do
result = 16 / 2
expect(result.even?).to eq true
end
it 'can be tested with predicate matchers' do
expect(16 / 2).to be_even
expect(15).to be_odd
expect(0).to be_zero
expect([]).to be_empty
end
describe 0 do
it { is_expected.to be_zero}
end
end- in Ruby land, only two entites are falsy
falsenil
- eveything else is truthy
- strings, and empty strings
- arrays, and empty arrays
- numbers, positive and negative, and 0
- hashes and symbols