W4D3 Flashcards
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