w2d5 Flashcards

1
Q

What is a DSL?

A

A domain-specific language; used for a specific problem domain.

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

What is the logic behind RSpec not testing private classes?

A

Private methods should support the public interface, so testing public methods should suffice.

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

What is the directory structure for projects with RSpec?

A
my_cool_project
  lib/
    hello.rb
  spec/
    hello_spec.rb
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What requires must spec files have?

A

They must require ‘rspec’ and the name of the file they are testing.

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

What is the ‘context’ keyword?

A

An alias for ‘describe’

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

What is the difference between ‘describe’ and ‘it’?

A

Both take strings as arguments, but describe takes the name of a method and it takes a description of the behavior being tested in the ‘it’ block

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

What are uses of the expect method?

A

expect(test_value).to eq(‘5’)

expect(test_value).to_not raise_error(ArgumentError)

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

How can we create a shared group of tests?

A

RSpec.shared_examples “collections” do |collection_class|

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

How can we use the ‘before’ method?

A

Use it to set up a context for testing:

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

T/F: Ruby supports ‘!=’

A

F

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

How can we define a subject for our tests?

A

subject(:robot) { Robot.new }

The symbol can then be accessed as an object:

‘robot.position’

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

What is the proper TDD workflow

A

Red, Green, Refactor

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

Where must the subject of an it block be declared?

A

Outside the it block.

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

Say we have a function ‘fibs_rec’, which takes an argument ‘num’, dictating the number of fibonaccis to return. How can we right rspec stating that ‘fibs_rec’ should return the first 2 fibonacci numbers?

A

it “returns first two fib numbers” do
expect(fibs_rec(2)).to eq([0, 1])
end

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

Let’s say ‘fibs_rec’ is a recursive function, and we expect it to call itself at least twice. What is the rspec to test this?

A

it “calls itself recursively” do
expect(self).to receive(:fibs_rec).at_least(:twice).and_call_original
fibs_rec(6)
end

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

What goes inside a describe ‘string’ do end block?

A

Various it blocks.

17
Q

How can we create a variable ‘array’ with a value, and where should it be declared?

A

let(:array) { [1, 2, 3, 4, 5].shuffle }

It should be inside a do block but before the if blocks that reference it.

18
Q

How can we create a larger variable to use in rspec, like a large array?

A

use let (:object_name) do end

let(:dictionary) do
# a small dictionary of words
[“a”, “am”, “an”, “pa”, “pan”, “panama”]
end

19
Q

What is the difference between ‘let’ and ‘subject’

A

Subject can only be used once and is the main object for the tests in a describe block, whereas let is used for helper objects and can be used many times, although each time with a different scope for each it block.

20
Q

What is “memoization”?

A

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.

21
Q

Does YAGNI apply when writing RSpec?

A

Absolutely; focus on getting down fundamental functionality first.

22
Q

How can we create a double with stub methods?

A

let(:customer) { double(“customer”, :email_address => “ned@appacademy.io”) }

23
Q

What is going on here, and how are method expectations set:

describe Order
let(:customer) { double(‘customer’) }
let(:product) { double(‘product’, :cost => 5.99) }
subject(:order) { Order.new(customer, product) }

it “subtracts item cost from customer account” do
expect(customer).to receive(:debit_account).with(5.99)
order.charge_customer
end
end

A

Here we want to test that when we call charge_customer on an Order object, it tells the customer to subtract the item price from the customer’s account. We also specify that we should check that we have passed #debit_account the correct price of the product.

Notice that we set the message expectation before we actually kick off the #charge_customer method. Expectations need to be set up in advance.

24
Q

What is distinct about integration tests?

A

Integration tests uses real objects instead of mocks in order to see if objects interact with one another correctly.