Test Doubles & Hooks Flashcards

1
Q

Say you want to write a application that has a class for the ClassRoom and a class for Student, you want to test that the list_student_names method works, use a test double to get them green lights!

class ClassRoom
  def initialize(students)
    @students = students
  end
  def list_student_names
    @students.map(&:name).join(',')
  end

end

A

describe ClassRoom do
it “the list_student_names method should work correctly” do
student1 = double(‘student’)
student2 = double(‘student’)

allow(student1).to receive(:name) { ‘John Smith’ }
allow(student2).to receive(:name) { ‘Jill Smith’ }

subject = described_class.new([student1,student2])
expect(subject.list_student_names).to eq { ‘John Smith,Jill Smith’}
end

end

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

What is the meaning of a Double in RSpec?

A

A Double can stand in for another object. For example a Double can be used when testing the function of a class method that involves the instance of a class that does not exist.

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

If I have two clases, say Person and Library. Person has a method named enter_library which changes the library attribute to be an instance of Library. How can you check that when the method is called on it changes the value of library to be an instance of Library?

A

describe Person do

describe 'can create library'
before { subject.enter_library }
it 'of Library class' do
expect(subject.library).to be_an_instance_of Library
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly