W4D3 Flashcards
(35 cards)
Raise
Generic way to end runtime. Hardcodes an error given a condition.
raise “Something Went wrong” if some condition
Begin/Rescue>Retry Block
Protects against errors resulting from User input.
Wrap a function call in a Begin/Rescue block. Include Retry to loop back to Begin
class CustomError < StandardError
raise CustomError.new(“Something went wrong”)
Custom error class ‘CustomError’ is created and inherits from StandardError
creates a new instance to include with a Rescue block.
Ensure
Ensure block only runs after a Begin/Rescue block completes. It will ALWAYS run regardless
Begin > Rescue > Retry
Interrupting the Retry Loop
Raise a conditional error before the Rescue.
example:
if raise “too many tried” if password_attempts > 2
Require bank
require multiple files in a separate file and keep your main class clean
Singleton Module
@sentinel = Singleton.instance
can be used to reference a single instance value for any filler spaces
loop do
break unless
loop continually and check for break conditions
rspec files
require ‘rspec’
require ‘file’
project lib/ file.rb spec/ file_spec.rb
rspec test
describe block
it block
Organizes your test and gives them lables
describe “#method_name” do
it “returns ‘Something!” do
expect(method_name).to eq(“Something!”
describe
DESCRIBE the “Subject” > do
IT “tests the case” do
EXPECT( {CODE} ).TO
Class do
“::class_method” “#instance_method” do
Nesting describe
Nest ‘describe’ or its alias ‘context’
expect
does the work of testing your code
Match between a value generated and an expected value
expect values
expect(test_value).to
expect(test_value).to_not
expect() AND expect{ }
expecting operations vs expecting errors
RSPEC matchers
.to eq(expected_val) and .to be(expected_val)
== or is the same object
Rspec Expectations
lets you express expected outcomes on an obect in an example
install rspec
Want to run against the main branch? You’ll need to include the dependent RSpec repos as well. Add the following to your Gemfile:
%w[rspec-core rspec-expectations rspec-mocks rspec-support].each do |lib|
gem lib, :git => “https://github.com/rspec/#{lib}.git”, :branch => ‘main’
end
Built-in Matchers -Equivalence
expect(actual).to eq(expected) # passes if actual == expected
expect(actual).to eql(expected) # passes if actual.eql?(expected)
expect(actual).not_to eql(not_expected) # passes if not(actual.eql?(expected))
Built-in Matchers -
Identity
expect(actual).to be(expected) # passes if actual.equal?(expected)
expect(actual).to equal(expected) # passes if actual.equal?(expected)
Built-in Matchers - Comparisons
expect(actual).to be > expected expect(actual).to be >= expected expect(actual).to be <= expected expect(actual).to be < expected expect(actual).to be_within(delta).of(expected)
Built-in Matchers - Types/classes
expect(actual).to be_an_instance_of(expected) # passes if actual.class == expected
expect(actual).to be_a(expected) # passes if actual.kind_of?(expected)
expect(actual).to be_an(expected) # an alias for be_a
expect(actual).to be_a_kind_of(expected) # another alias
Built-in Matchers - Truthiness
expect(actual).to be_truthy # passes if actual is truthy (not nil or false)
expect(actual).to be true # passes if actual == true
expect(actual).to be_falsy # passes if actual is falsy (nil or false)
expect(actual).to be false # passes if actual == false
expect(actual).to be_nil # passes if actual is nil
expect(actual).to_not be_nil # passes if actual is not nil
Built-in Matchers - Expecting errors
expect { … }.to raise_error
expect { … }.to raise_error(ErrorClass)
expect { … }.to raise_error(“message”)
expect { … }.to raise_error(ErrorClass, “message”)