w2d5 Flashcards
What is a DSL?
A domain-specific language; used for a specific problem domain.
What is the logic behind RSpec not testing private classes?
Private methods should support the public interface, so testing public methods should suffice.
What is the directory structure for projects with RSpec?
my_cool_project lib/ hello.rb spec/ hello_spec.rb
What requires must spec files have?
They must require ‘rspec’ and the name of the file they are testing.
What is the ‘context’ keyword?
An alias for ‘describe’
What is the difference between ‘describe’ and ‘it’?
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
What are uses of the expect method?
expect(test_value).to eq(‘5’)
expect(test_value).to_not raise_error(ArgumentError)
How can we create a shared group of tests?
RSpec.shared_examples “collections” do |collection_class|
How can we use the ‘before’ method?
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
T/F: Ruby supports ‘!=’
F
How can we define a subject for our tests?
subject(:robot) { Robot.new }
The symbol can then be accessed as an object:
‘robot.position’
What is the proper TDD workflow
Red, Green, Refactor
Where must the subject of an it block be declared?
Outside the it block.
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?
it “returns first two fib numbers” do
expect(fibs_rec(2)).to eq([0, 1])
end
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?
it “calls itself recursively” do
expect(self).to receive(:fibs_rec).at_least(:twice).and_call_original
fibs_rec(6)
end