W4D3 Flashcards

(35 cards)

1
Q

Raise

A

Generic way to end runtime. Hardcodes an error given a condition.

raise “Something Went wrong” if some condition

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Begin/Rescue>Retry Block

A

Protects against errors resulting from User input.

Wrap a function call in a Begin/Rescue block. Include Retry to loop back to Begin

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

class CustomError < StandardError

raise CustomError.new(“Something went wrong”)

A

Custom error class ‘CustomError’ is created and inherits from StandardError

creates a new instance to include with a Rescue block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Ensure

A

Ensure block only runs after a Begin/Rescue block completes. It will ALWAYS run regardless

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Begin > Rescue > Retry

Interrupting the Retry Loop

A

Raise a conditional error before the Rescue.

example:
if raise “too many tried” if password_attempts > 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Require bank

A

require multiple files in a separate file and keep your main class clean

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Singleton Module

A

@sentinel = Singleton.instance

can be used to reference a single instance value for any filler spaces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

loop do

break unless

A

loop continually and check for break conditions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

rspec files

A

require ‘rspec’
require ‘file’

project
lib/
   file.rb
spec/
   file_spec.rb
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

rspec test
describe block
it block

A

Organizes your test and gives them lables

describe “#method_name” do
it “returns ‘Something!” do
expect(method_name).to eq(“Something!”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

describe

A

DESCRIBE the “Subject” > do
IT “tests the case” do
EXPECT( {CODE} ).TO

Class do
“::class_method” “#instance_method” do

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Nesting describe

A

Nest ‘describe’ or its alias ‘context’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

expect

A

does the work of testing your code

Match between a value generated and an expected value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

expect values

A

expect(test_value).to

expect(test_value).to_not

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

expect() AND expect{ }

A

expecting operations vs expecting errors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

RSPEC matchers

A

.to eq(expected_val) and .to be(expected_val)

== or is the same object

17
Q

Rspec Expectations

A

lets you express expected outcomes on an obect in an example

install rspec

18
Q

Want to run against the main branch? You’ll need to include the dependent RSpec repos as well. Add the following to your Gemfile:

A

%w[rspec-core rspec-expectations rspec-mocks rspec-support].each do |lib|
gem lib, :git => “https://github.com/rspec/#{lib}.git”, :branch => ‘main’
end

19
Q

Built-in Matchers -Equivalence

A

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))

20
Q

Built-in Matchers -

Identity

A

expect(actual).to be(expected) # passes if actual.equal?(expected)
expect(actual).to equal(expected) # passes if actual.equal?(expected)

21
Q

Built-in Matchers - Comparisons

A
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)
22
Q

Built-in Matchers - Types/classes

A

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

23
Q

Built-in Matchers - Truthiness

A

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

24
Q

Built-in Matchers - Expecting errors

A

expect { … }.to raise_error
expect { … }.to raise_error(ErrorClass)
expect { … }.to raise_error(“message”)
expect { … }.to raise_error(ErrorClass, “message”)

25
Built-in Matchers - Expecting throws
expect { ... }.to throw_symbol expect { ... }.to throw_symbol(:symbol) expect { ... }.to throw_symbol(:symbol, 'value')
26
Built-in Matchers others
Yielding, Predicate matchers, Ranges (Ruby >= 1.9 only),
27
Built-in Matchers - Collection membership
``` # exact order, entire collection expect(actual).to eq(expected) ``` exact order, partial collection (based on an exact position) expect(actual).to start_with(expected) expect(actual).to end_with(expected) ``` # any order, entire collection expect(actual).to match_array(expected) ``` ``` # You can also express this by passing the expected elements # as individual arguments expect(actual).to contain_exactly(expected_element1, expected_element2) ``` ``` # any order, partial collection expect(actual).to include(expected) ``` expect([1, 2, 3]).to eq([1, 2, 3]) # Order dependent equality check expect([1, 2, 3]).to include(1) # Exact ordering, partial collection matches expect([1, 2, 3]).to include(2, 3) # expect([1, 2, 3]).to start_with(1) # As above, but from the start of the collection expect([1, 2, 3]).to start_with(1, 2) # expect([1, 2, 3]).to end_with(3) # As above but from the end of the collection expect([1, 2, 3]).to end_with(2, 3) # expect({:a => 'b'}).to include(:a => 'b') # Matching within hashes expect("this string").to include("is str") # Matching within strings expect("this string").to start_with("this") # expect("this string").to end_with("ring") # expect([1, 2, 3]).to contain_exactly(2, 3, 1) # Order independent matches expect([1, 2, 3]).to match_array([3, 2, 1]) # Order dependent compound matchers expect( [{:a => 'hash'},{:a => 'another'}] ).to match([a_hash_including(:a => 'hash'), a_hash_including(:a => 'another')])
28
Built-in Matchers - Compound Matcher Expressions
expect(alphabet).to start_with("a").and end_with("z") | expect(stoplight.color).to eq("red").or eq("green").or eq("yellow")
29
RSPEC - Before block
describe Chess do let(:board) { Board.new } ``` describe '#checkmate?' do context 'when in checkmate' do before(:each) do board.make_move([3, 4], [2, 3]) board.make_move([1, 2], [4, 5]) board.make_move([5, 3], [5, 1]) board.make_move([6, 3], [2, 4]) end ```
30
before(:each) do
execute the block of code before each spec in that describe block
31
describe '#valid_move?' do it 'should return false for wrong colored pieces' it 'should return false for moves that are off the board' it 'should return false for moves that put you in check' end
Leave off the do...end from the it. for pending specs
32
RSPEC, instantiate an instance of an Object
Subject and It describe Robot do subject(:robot) { Robot.new } it "position should start at [0, 0]" do expect(robot.position).to eq([0, 0]) end ``` describe "move methods" do it "moves left" do robot.move_left expect(robot.position).to eq([-1, 0]) end end end ```
33
Subject block
defines the subject of the test subject also accepts a block that constructs the subject. You can do any necessary setup inside the block. there can only be one unnamed subject
34
Let block
defines the helper objects that interact with the subject there can be multiple helper objects
35
let does not persist state
You might read that let memoizes its return value. Memoization means that the first time the method is invoked, the return value is cached and that same value is returned every subsequent time the method is invoked within the same scope. Since every it is a different scope, let does not persist state between those specs.