1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
Feature: Contract tests with spies
Whenever you spy on method invocations, it creates a contract specifying that some method can be called with a particular set of arguments. It can be automatically verified by Bogus, whether the method was actually called with those arguments.
Background:
Given a file named "library.rb" with:
"""ruby
class Library
def checkout(book)
end
end
"""
Given a file named "student.rb" with:
"""ruby
class Student
def read(book, library = Library.new)
library.checkout(book)
# ...
end
end
"""
And a spec file named "student_spec.rb" with:
"""ruby
require_relative 'student'
require_relative 'library'
describe Student do
fake(:library)
it "reads books from library" do
student = Student.new
student.read("Moby Dick", library)
expect(library).to have_received.checkout("Moby Dick")
end
end
"""
Scenario: Stubbing methods that exist on real object
Then spec file with following content should pass:
"""ruby
require_relative 'library'
describe Library do
verify_contract(:library)
it "checks out books" do
library = Library.new
library.checkout("Moby Dick")
# ...
end
end
"""
Scenario: Verifing that stubbed methods are tested
Then spec file with following content should fail:
"""ruby
require_relative 'library'
describe Library do
verify_contract(:library)
end
"""
Scenario: Verifying that methods are tested with right arguments
Then spec file with following content should fail:
"""ruby
require_relative 'library'
describe Library do
verify_contract(:library)
it "checks out books" do
library = Library.new
library.checkout("Moby Dick 2: The ulitmate")
# ...
end
end
"""
|