Last active
February 2, 2020 21:04
-
-
Save rcoproc/758f51b153bc15ae9716c49300394965 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Padrâo xUnit 4 fases de teste(4 Standard test phases XUnit) | |
# Setup | |
# Exercise | |
# Verify | |
# Teardown | |
# "Stubs são para a fase de Setup" - "Stubs are for the Setup phase" | |
# "Stubs são usados para substituir estados." - " Stubs are used to replace state" | |
require 'student' | |
require 'course' | |
describe 'Stub' do | |
it '#has_finished?' do | |
student = Student.new | |
course = Course.new | |
# Permite(força) que o método has_finished da classe Student, com o parâmetro Course | |
# retorne true. | |
allow(student).to receive(:has_finished?) | |
.with(an_instance_of(Course)) | |
.and_return(true) | |
course_finished = student.has_finished?(course) | |
# Aqui é esperado que o resultado do método seja true | |
expect(course_finished).to be_truthy | |
end | |
it 'Argumentos Dinâmicos' do | |
student = Student.new | |
# Permite(força) que o método foo da classe Student, com os parâmetros(:hello ou :hi) | |
# retornem conforme os parâmetros enviados. | |
allow(student).to receive(:foo) do |arg| | |
if arg == :hello | |
"olá" | |
elsif arg == :hi | |
"Hi!!!" | |
end | |
end | |
# Aqui é esperado que retornem "olá" ou "Hi!!!" conforme o parâmetro passado(:hello ou :hi) | |
expect(student.foo(:hello)).to eq("olá") | |
expect(student.foo(:hi)).to eq("Hi!!!") | |
end | |
it 'Qualquer instância de Classe' do | |
student = Student.new | |
other_student = Student.new | |
allow_any_instance_of(Student).to receive(:bar).and_return(true) | |
expect(student.bar).to be_truthy | |
expect(other_student.bar).to be_truthy | |
end | |
it 'Erros' do | |
student = Student.new | |
# Permite(força) que o método bar da classe Student, | |
# dispare um RuntimeError. | |
allow(student).to receive(:bar).and_raise(RuntimeError) | |
# Ao chamar prestar atenção aqui nos {} que controlam a chamada da Exception | |
expect{ student.bar }.to raise_error(RuntimeError) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment