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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
Feature: minitest support
minitest is supported by Bogus both with the classic assert-style syntax and the minitest/spec expectation syntax.
Background:
Given a file named "library.rb" with:
"""ruby
class Library
def self.books
end
def checkout(book)
end
def return_book(book)
end
end
"""
Given a file named "book_index.rb" with:
"""ruby
class BookIndex
def self.by_author(author)
Library.books.select{|book| book[:author] == author}
end
end
"""
Given a file named "student.rb" with:
"""ruby
class Student
def initialize(library)
@library = library
end
def study(*book_titles)
book_titles.each do |book_title|
@library.checkout(book_title)
end
end
end
"""
Scenario: Auto-verification of unsatisfied mocks
Then minitest file "student_test.rb" with the following content should fail:
"""ruby
require 'minitest/autorun'
require 'bogus/minitest'
require_relative 'student'
require_relative 'library'
class StudentTest < MiniTest::Unit::TestCase
def test_library_checkouts
library = fake(:library)
student = Student.new(library)
mock(library).checkout("Moby Dick")
mock(library).checkout("Sherlock Holmes")
student.study("Moby Dick")
end
end
"""
Scenario: Spying on method calls with assert syntax
Then minitest file "student_test.rb" with the following content should pass:
"""ruby
require 'minitest/autorun'
require 'bogus/minitest'
require_relative 'student'
require_relative 'library'
class StudentTest < MiniTest::Unit::TestCase
def setup
@library = fake(:library)
end
def test_library_checkouts
student = Student.new(@library)
student.study("Moby Dick", "Sherlock Holmes")
assert_received @library, :checkout, ["Moby Dick"]
assert_received @library, :checkout, ["Sherlock Holmes"], "optional message"
refute_received @library, :return_book, ["Moby Dick"]
end
end
"""
Scenario: Spying on method calls with expectation syntax
Then minitest file "student_spec.rb" with the following content should pass:
"""ruby
require 'minitest/autorun'
require 'bogus/minitest/spec'
require_relative 'student'
require_relative 'library'
describe Student do
describe "#study" do
fake(:library)
it "studies using books from library" do
student = Student.new(library)
student.study("Moby Dick", "Sherlock Holmes")
library.must_have_received :checkout, ["Moby Dick"]
library.must_have_received :checkout, ["Sherlock Holmes"]
library.wont_have_received :return_book, ["Moby Dick"]
end
end
end
"""
Scenario: Describe-level class faking
Then minitest file "book_index_spec.rb" with the following content should pass:
"""ruby
require 'minitest/autorun'
require 'bogus/minitest/spec'
require_relative 'book_index'
require_relative 'library'
describe BookIndex do
fake_class(Library, books: [])
it "returns books written by author" do
BookIndex.by_author("Mark Twain").must_equal []
end
end
"""
Scenario: Negative contract verification
Then minitest file "student_and_library_spec.rb" with the following content should fail:
"""ruby
require 'minitest/autorun'
require 'bogus/minitest/spec'
require_relative 'student'
require_relative 'library'
describe Student do
describe "#study" do
fake(:library)
it "studies using books from library" do
Student.new(library).study("Moby Dick")
library.must_have_received :checkout, ["Moby Dick"]
end
end
end
describe Library do
verify_contract(:library)
end
"""
Scenario: Positive contract verification
Then minitest file "student_and_library_spec.rb" with the following content should pass:
"""ruby
require 'minitest/autorun'
require 'bogus/minitest/spec'
require_relative 'student'
require_relative 'library'
describe Student do
describe "#study" do
fake(:library)
it "studies using books from library" do
Student.new(library).study("Moby Dick")
library.must_have_received :checkout, ["Moby Dick"]
end
end
end
describe Library do
verify_contract(:library)
describe '#checkout' do
it "checks books out" do
Library.new.checkout("Moby Dick")
end
end
end
"""
Scenario: Custom verified class
Then minitest file "student_and_library_spec.rb" with the following content should pass:
"""ruby
require 'minitest/autorun'
require 'bogus/minitest/spec'
class NetworkLogger
def info(msg)
end
end
class OrderProcessor
def process(order, logger = NetworkLogger.new)
logger.info("#{order} processed")
end
end
describe OrderProcessor do
describe "#process" do
fake(:logger) { NetworkLogger }
it "processes orders" do
OrderProcessor.new.process("burger & fries", logger)
logger.must_have_received :info, ["burger & fries processed"]
end
end
end
describe "Using network logger" do
verify_contract(:logger) { NetworkLogger }
describe '#info' do
it "logs on info level" do
NetworkLogger.new.info("burger & fries processed")
end
end
end
"""
|