Skip to content

Commit

Permalink
Merge 1b9b7bc into d818368
Browse files Browse the repository at this point in the history
  • Loading branch information
chastell committed Jul 1, 2013
2 parents d818368 + 1b9b7bc commit 61a962b
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 14 deletions.
10 changes: 6 additions & 4 deletions .gitignore
@@ -1,10 +1,12 @@
*.gem
.bundle
.rbx
.ruby-gemset
.ruby-version
.rvmrc
coverage/
gems.tags
pkg/*
tmp/*
.rvmrc
tags
.rbx
tags
gems.tags
tmp/*
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -2,3 +2,5 @@ source "http://rubygems.org"

# Specify your gem's dependencies in bogus.gemspec
gemspec

gem 'minitest', require: false
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -71,6 +71,7 @@ GEM
rb-fsevent (~> 0.9.1)
rb-inotify (~> 0.8.8)
mime-types (1.19)
minitest (5.0.6)
multi_json (1.7.3)
rake (0.9.2.2)
rb-fchange (0.0.5)
Expand Down Expand Up @@ -117,6 +118,7 @@ DEPENDENCIES
guard-cucumber
guard-rspec
libnotify
minitest
rake
relish
rr
Expand Down
2 changes: 2 additions & 0 deletions bogus.gemspec
Expand Up @@ -37,4 +37,6 @@ Gem::Specification.new do |s|

s.add_development_dependency 'activerecord'
s.add_development_dependency 'activerecord-nulldb-adapter'

s.add_development_dependency 'minitest', '>= 4.7'
end
8 changes: 4 additions & 4 deletions features/fakes/replacing_classes.feature
Expand Up @@ -83,7 +83,7 @@ Feature: Replacing classes with fakes
"""

When I run `rspec book_index_spec.rb library_spec.rb`
Then all the specs should pass
Then the specs should pass

Scenario: Replacing classes and contracts with a different fake name
Given a file named "library_spec.rb" with:
Expand Down Expand Up @@ -128,8 +128,8 @@ Feature: Replacing classes with fakes
"""

When I run `rspec book_index_spec.rb library_spec.rb`
Then all the specs should pass
Then the specs should pass

Scenario: Replacing classes with a macro
Given a file named "book_index_spec.rb" with:
"""ruby
Expand All @@ -145,4 +145,4 @@ Feature: Replacing classes with fakes
"""

When I run `rspec book_index_spec.rb`
Then all the specs should pass
Then the specs should pass
119 changes: 119 additions & 0 deletions features/minitest/minitest_support.feature
@@ -0,0 +1,119 @@
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 "foo.rb" with:
"""ruby
class Library
def self.books
end
def checkout(book)
end
def return_book(book)
end
end
class BookIndex
def self.by_author(author)
Library.books.select{|book| book[:author] == author}
end
end
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 "foo_test.rb" with the following content should fail:
"""ruby
require 'minitest/autorun'
require 'bogus/minitest'
require_relative 'foo'
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 "foo_test.rb" with the following content should pass:
"""ruby
require 'minitest/autorun'
require 'bogus/minitest'
require_relative 'foo'
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"]
refute_received @library, :return_book, ["Moby Dick"]
end
end
"""

Scenario: Spying on method calls with expectation syntax
Then minitest file "foo_spec.rb" with the following content should pass:
"""ruby
require 'minitest/autorun'
require 'bogus/minitest/spec'
require_relative 'foo'
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 "foo_spec.rb" with the following content should pass:
"""ruby
require 'minitest/autorun'
require 'bogus/minitest/spec'
require_relative 'foo'
describe BookIndex do
fake_class(Library, books: [])
it "returns books written by author" do
BookIndex.by_author("Mark Twain").must_equal []
end
end
"""
17 changes: 14 additions & 3 deletions features/step_definitions/rspec_steps.rb
Expand Up @@ -18,7 +18,7 @@
}
end

Then /^all the specs should pass$/ do
Then /^the specs should pass$/ do
steps %Q{
Then the exit status should be 0
}
Expand All @@ -45,7 +45,7 @@
"""ruby
#{string}
"""
Then all the specs should pass
Then the specs should pass
}
end

Expand All @@ -69,6 +69,17 @@
end
end
"""
Then all the specs should pass
Then the specs should pass
}
end

Then /^minitest file "([^"]*)" with the following content should (pass|fail):$/ do |file_name, pass_fail, string|
steps %Q{
Given a file named "#{file_name}" with:
"""ruby
#{string}
"""
When I run `ruby #{file_name}`
Then the specs should #{pass_fail}
}
end
9 changes: 6 additions & 3 deletions lib/bogus.rb
Expand Up @@ -8,9 +8,12 @@
require_relative 'bogus/rspec_extensions'
require_relative 'bogus/rspec_adapter'

all_files = Dir[File.expand_path('../bogus/**/*.rb', __FILE__)]
all_files = all_files.reject { |f| f.include?('bogus/rspec') }
all_files.sort.each { |f| require f }
dir = File.realpath File.expand_path('../bogus', __FILE__)
Dir["#{dir}/**/*.rb"].sort.each do |path|
next if path.include? 'bogus/minitest'
next if path.include? 'bogus/rspec'
require path
end

module Bogus
extend PublicMethods
Expand Down
46 changes: 46 additions & 0 deletions lib/bogus/minitest.rb
@@ -0,0 +1,46 @@
gem 'minitest', '>= 4.7'
require 'bogus'

module MiniTest::Assertions
def assert_received(subject, method, args, message = nil)
with_bogus_matcher_for(subject, method, args) do |matcher, result|
assert(result, message || matcher.failure_message_for_should)
end
end

def refute_received(subject, method, args, message = nil)
with_bogus_matcher_for(subject, method, args) do |matcher, result|
refute(result, message || matcher.failure_message_for_should_not)
end
end

private

def with_bogus_matcher_for(subject, method, args)
matcher = Bogus.have_received.__send__(method, *args)
result = matcher.matches?(subject)
yield matcher, result
end
end

module Bogus::Minitest
include Bogus::MockingDSL

def before_setup
super
Bogus.clear
end

def after_teardown
Bogus.ensure_all_expectations_satisfied!
super
end
end

class Minitest::Test
include Bogus::Minitest
end if defined? Minitest::Test

class MiniTest::Unit::TestCase
include Bogus::Minitest
end if defined? MiniTest::Unit::TestCase
18 changes: 18 additions & 0 deletions lib/bogus/minitest/spec.rb
@@ -0,0 +1,18 @@
require 'bogus/minitest'

module MiniTest::Expectations
infect_an_assertion :assert_received, :must_have_received, true
infect_an_assertion :refute_received, :wont_have_received, true
end

class MiniTest::Spec
module DSL
def fake(name, opts = {}, &block)
let(name) { fake(name, opts, &block) }
end

def fake_class(name, opts = {})
before { fake_class(name, opts) }
end
end
end

0 comments on commit 61a962b

Please sign in to comment.