Revert bunch of bad commits:

2442d1f608
35213b5a1b
804f859435
fc938bb185

Getting the following error when running tests in a real project:

  ...activesupport/lib/active_support/dependencies.rb:276:in `load_missing_constant': uninitialized constant Shoulda::ActiveRecord::Base (NameError)

Think this is because of the namespace changes, but not sure.  Will investigate later.
This commit is contained in:
Tammer Saleh 2008-06-23 12:36:31 -04:00
parent 2442d1f608
commit b415bff884
18 changed files with 1701 additions and 1931 deletions

View File

@ -10,7 +10,7 @@ Assertions:: Many common rails testing idioms have been distilled into a set of
= Usage
=== Context Helpers (Shoulda::Context)
=== Context Helpers (ThoughtBot::Shoulda::Context)
Stop killing your fingers with all of those underscores... Name your tests with plain sentences!
@ -43,7 +43,7 @@ Produces the following test methods:
So readable!
=== ActiveRecord Tests (Shoulda::ActiveRecord)
=== ActiveRecord Tests (ThoughtBot::Shoulda::ActiveRecord)
Quick macro tests for your ActiveRecord associations and validations:
@ -73,7 +73,7 @@ Quick macro tests for your ActiveRecord associations and validations:
Makes TDD so much easier.
=== Controller Tests (Shoulda::Controller::ClassMethods)
=== Controller Tests (ThoughtBot::Shoulda::Controller::ClassMethods)
Macros to test the most common controller patterns...
@ -105,7 +105,7 @@ Test entire controllers in a few lines...
should_be_restful generates 40 tests on the fly, for both html and xml requests.
=== Helpful Assertions (Shoulda::General)
=== Helpful Assertions (ThoughtBot::Shoulda::General)
More to come here, but have fun with what's there.

View File

@ -6,7 +6,7 @@ require 'rake/rdoctask'
Rake::TestTask.new do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.pattern = 'test/{unit,functional,other}/**/*_test.rb'
t.verbose = false
end

View File

@ -1,144 +1,43 @@
require 'shoulda/context'
require 'shoulda/gem/shoulda'
require 'shoulda/private_helpers'
require 'shoulda/general'
require 'shoulda/active_record_helpers'
require 'shoulda/controller_tests/controller_tests.rb'
require 'yaml'
require 'shoulda/active_record_helpers' if defined?(ActiveRecord)
require 'shoulda/controller_tests/controller_tests.rb' if defined?(ActionController)
config_files = []
config_files << "shoulda.conf"
config_files << File.join("test", "shoulda.conf")
config_files << File.join(RAILS_ROOT, "test", "shoulda.conf") if defined?(RAILS_ROOT)
config_files << File.join(ENV["HOME"], ".shoulda.conf") if ENV["HOME"]
shoulda_options = {}
config_files.each do |file|
shoulda_options.merge!(YAML.load_file(file).symbolize_keys) if File.exists? file
possible_config_paths = []
possible_config_paths << File.join(ENV["HOME"], ".shoulda.conf") if ENV["HOME"]
possible_config_paths << "shoulda.conf"
possible_config_paths << File.join("test", "shoulda.conf")
possible_config_paths << File.join(RAILS_ROOT, "test", "shoulda.conf") if defined?(RAILS_ROOT)
possible_config_paths.each do |config_file|
if File.exists? config_file
shoulda_options = YAML.load_file(config_file).symbolize_keys
break
end
end
require 'shoulda/color' if shoulda_options[:color]
module Shoulda
class << self
attr_accessor :current_context
end
# Should statements are just syntactic sugar over normal Test::Unit test methods. A should block
# contains all the normal code and assertions you're used to seeing, with the added benefit that
# they can be wrapped inside context blocks (see below).
#
# == Example:
#
# class UserTest << Test::Unit::TestCase
#
# def setup
# @user = User.new("John", "Doe")
# end
#
# should "return its full name"
# assert_equal 'John Doe', @user.full_name
# end
#
# end
#
# ...will produce the following test:
# * <tt>"test: User should return its full name. "</tt>
#
# Note: The part before <tt>should</tt> in the test name is gleamed from the name of the Test::Unit class.
def should(name, &blk)
should_eventually(name) && return unless block_given?
if Shoulda.current_context
Shoulda.current_context.should(name, &blk)
else
context_name = self.name.gsub(/Test/, "")
context = Shoulda::Context.new(context_name, self) do
should(name, &blk)
end
context.build
end
end
# Just like should, but never runs, and instead prints a differed message in the Test::Unit output.
def should_eventually(name, &blk)
context_name = self.name.gsub(/Test/, "")
context = Shoulda::Context.new(context_name, self) do
should_eventually(name, &blk)
end
context.build
end
# A context block groups should statements under a common set of setup/teardown methods.
# Context blocks can be arbitrarily nested, and can do wonders for improving the maintainability
# and readability of your test code.
#
# A context block can contain setup, should, should_eventually, and teardown blocks.
#
# class UserTest << Test::Unit::TestCase
# context "A User instance" do
# setup do
# @user = User.find(:first)
# end
#
# should "return its full name"
# assert_equal 'John Doe', @user.full_name
# end
# end
# end
#
# This code will produce the method <tt>"test: A User instance should return its full name. "</tt>.
#
# Contexts may be nested. Nested contexts run their setup blocks from out to in before each
# should statement. They then run their teardown blocks from in to out after each should statement.
#
# class UserTest << Test::Unit::TestCase
# context "A User instance" do
# setup do
# @user = User.find(:first)
# end
#
# should "return its full name"
# assert_equal 'John Doe', @user.full_name
# end
#
# context "with a profile" do
# setup do
# @user.profile = Profile.find(:first)
# end
#
# should "return true when sent :has_profile?"
# assert @user.has_profile?
# end
# end
# end
# end
#
# This code will produce the following methods
# * <tt>"test: A User instance should return its full name. "</tt>
# * <tt>"test: A User instance with a profile should return true when sent :has_profile?. "</tt>
#
# <b>Just like should statements, a context block can exist next to normal <tt>def test_the_old_way; end</tt>
# tests</b>. This means you do not have to fully commit to the context/should syntax in a test file.
def context(name, &blk)
if Shoulda.current_context
Shoulda.current_context.context(name, &blk)
else
context = Shoulda::Context.new(name, self, &blk)
context.build
end
end
end
module Test # :nodoc: all
module Unit
class TestCase
include Shoulda::General
include Shoulda::Controller
extend Shoulda::ActiveRecord
include ThoughtBot::Shoulda::General
include ThoughtBot::Shoulda::Controller
extend ThoughtBot::Shoulda::ActiveRecord
end
end
end
module ActionController #:nodoc: all
module Integration
class Session
include ThoughtBot::Shoulda::General
end
end
end

View File

@ -1,4 +1,5 @@
module Shoulda
module ThoughtBot # :nodoc:
module Shoulda # :nodoc:
# = Macro test helpers for your active record models
#
# These helpers will test most of the validations and associations for your ActiveRecord models.
@ -597,6 +598,7 @@ module Shoulda
private
include Shoulda::Private
include ThoughtBot::Shoulda::Private
end
end
end

View File

@ -1,8 +1,6 @@
require 'test/unit/ui/console/testrunner'
# Completely stolen from redgreen gem (thanks Pat Eyler and Chris Wanstrath).
#
# NOTE: Is this now in autotest? Can I remove this, then?
# Completely stolen from redgreen gem
#
# Adds colored output to your tests. Specify <tt>color: true</tt> in
# your <tt>~/.shoulda.conf</tt> file to enable.
@ -11,7 +9,7 @@ require 'test/unit/ui/console/testrunner'
# every rake task, as though there was another (empty) set of tests.
# A fix would be most welcome.
#
module Shoulda::Color
module ThoughtBot::Shoulda::Color
COLORS = { :clear => 0, :red => 31, :green => 32, :yellow => 33 } # :nodoc:
def self.method_missing(color_name, *args) # :nodoc:
color(color_name) + args.first + color(:clear)
@ -27,7 +25,7 @@ module Test # :nodoc:
alias :old_to_s :to_s
def to_s
if old_to_s =~ /\d+ tests, \d+ assertions, (\d+) failures, (\d+) errors/
Shoulda::Color.send($1.to_i != 0 || $2.to_i != 0 ? :red : :green, $&)
ThoughtBot::Shoulda::Color.send($1.to_i != 0 || $2.to_i != 0 ? :red : :green, $&)
end
end
end
@ -45,16 +43,16 @@ module Test # :nodoc:
class Failure # :nodoc:
alias :old_long_display :long_display
def long_display
# old_long_display.sub('Failure', Shoulda::Color.red('Failure'))
Shoulda::Color.red(old_long_display)
# old_long_display.sub('Failure', ThoughtBot::Shoulda::Color.red('Failure'))
ThoughtBot::Shoulda::Color.red(old_long_display)
end
end
class Error # :nodoc:
alias :old_long_display :long_display
def long_display
# old_long_display.sub('Error', Shoulda::Color.yellow('Error'))
Shoulda::Color.yellow(old_long_display)
# old_long_display.sub('Error', ThoughtBot::Shoulda::Color.yellow('Error'))
ThoughtBot::Shoulda::Color.yellow(old_long_display)
end
end
@ -64,9 +62,9 @@ module Test # :nodoc:
def output_single(something, level=NORMAL)
return unless (output?(level))
something = case something
when '.' then Shoulda::Color.green('.')
when 'F' then Shoulda::Color.red("F")
when 'E' then Shoulda::Color.yellow("E")
when '.' then ThoughtBot::Shoulda::Color.green('.')
when 'F' then ThoughtBot::Shoulda::Color.red("F")
when 'E' then ThoughtBot::Shoulda::Color.yellow("E")
else something
end
@io.write(something)

View File

@ -1,140 +0,0 @@
require File.join(File.dirname(__FILE__), 'extensions', 'proc')
module Shoulda
class Context
attr_accessor :name # my name
attr_accessor :parent # may be another context, or the original test::unit class.
attr_accessor :subcontexts # array of contexts nested under myself
attr_accessor :setup_block # block given via a setup method
attr_accessor :teardown_block # block given via a teardown method
attr_accessor :shoulds # array of hashes representing the should statements
attr_accessor :should_eventuallys # array of hashes representing the should eventually statements
def initialize(name, parent, &blk)
Shoulda.current_context = self
self.name = name
self.parent = parent
self.setup_block = nil
self.teardown_block = nil
self.shoulds = []
self.should_eventuallys = []
self.subcontexts = []
blk.bind(self).call
Shoulda.current_context = nil
end
# Creates a context. See Shoulda#context.
def context(name, &blk)
subcontexts << Context.new(name, self, &blk)
Shoulda.current_context = self
end
# Creates a should statement. See Shoulda#should.
def should(name, &blk)
self.shoulds << { :name => name, :block => blk }
end
# Creates a should_eventually statement. See Shoulda#should_eventually.
def should_eventually(name, &blk)
self.should_eventuallys << { :name => name, :block => blk }
end
# Any code in a setup block will be run before the should statements in a
# context. Nested contexts will have their setup blocks run in order.
def setup(&blk)
self.setup_block = blk
end
# Any code in a teardown block will be run after the should statements in a
# context. Nested contexts will have their teardown blocks run in reverse
# order.
def teardown(&blk)
self.teardown_block = blk
end
# The full name of this context, including parents.
def full_name
parent_name = parent.full_name if subcontext?
return [parent_name, name].join(" ").strip
end
# Returns true if this context is nested
def subcontext?
parent.is_a?(self.class) # my parent is the same class as myself.
end
# Returns the root class that decends from Test::Unit.
def test_unit_class
subcontext? ? parent.test_unit_class : parent
end
# Creates a single test from a should hash
def create_test_from_should_hash(should)
test_name = ["test:", full_name, "should", "#{should[:name]}. "].flatten.join(' ').to_sym
if test_unit_class.instance_methods.include?(test_name.to_s)
warn " * WARNING: '#{test_name}' is already defined"
end
context = self
test_unit_class.send(:define_method, test_name) do |*args|
begin
context.run_all_setup_blocks(self)
should[:block].bind(self).call
ensure
context.run_all_teardown_blocks(self)
end
end
end
# Runs all the setup blocks in order
def run_all_setup_blocks(binding)
self.parent.run_all_setup_blocks(binding) if subcontext?
setup_block.bind(binding).call if setup_block
end
# Runs all the teardown blocks in reverse order
def run_all_teardown_blocks(binding)
teardown_block.bind(binding).call if teardown_block
self.parent.run_all_teardown_blocks(binding) if subcontext?
end
# Prints the should_eventually names to stdout
def print_should_eventuallys
should_eventuallys.each do |should|
test_name = [full_name, "should", "#{should[:name]}. "].flatten.join(' ')
puts " * DEFERRED: " + test_name
end
subcontexts.each { |context| context.print_should_eventuallys }
end
# Triggers the test method creation process, and prints the unimplemented tests.
def build
shoulds.each do |should|
create_test_from_should_hash(should)
end
subcontexts.each { |context| context.build }
print_should_eventuallys
end
# This delegates all method calls inside a context to the surrounding
# Test::Unit class. This allows us to call Test::Unit macros inside a
# context.
def method_missing(method, *args, &blk)
test_unit_class.send(method, *args, &blk)
end
end
end
module Test # :nodoc: all
module Unit
class TestCase
extend Shoulda
end
end
end

View File

@ -1,11 +1,12 @@
module Shoulda
module ThoughtBot # :nodoc:
module Shoulda # :nodoc:
module Controller
def self.included(other) # :nodoc:
other.class_eval do
extend Shoulda::Controller::ClassMethods
include Shoulda::Controller::InstanceMethods
Shoulda::Controller::ClassMethods::VALID_FORMATS.each do |format|
include "Shoulda::Controller::#{format.to_s.upcase}".constantize
extend ThoughtBot::Shoulda::Controller::ClassMethods
include ThoughtBot::Shoulda::Controller::InstanceMethods
ThoughtBot::Shoulda::Controller::ClassMethods::VALID_FORMATS.each do |format|
include "ThoughtBot::Shoulda::Controller::#{format.to_s.upcase}".constantize
end
end
end
@ -462,11 +463,5 @@ module Shoulda
end
end
end
end
module ActionController #:nodoc: all
module Integration
class Session
include Shoulda::General
end
end
end

View File

@ -1,9 +1,10 @@
module Shoulda
module ThoughtBot # :nodoc:
module Shoulda # :nodoc:
module Controller # :nodoc:
module HTML # :nodoc: all
def self.included(other)
other.class_eval do
extend Shoulda::Controller::HTML::ClassMethods
extend ThoughtBot::Shoulda::Controller::HTML::ClassMethods
end
end
@ -197,3 +198,4 @@ module Shoulda
end
end
end
end

View File

@ -1,9 +1,10 @@
module Shoulda
module ThoughtBot # :nodoc:
module Shoulda # :nodoc:
module Controller # :nodoc:
module XML
def self.included(other) #:nodoc:
other.class_eval do
extend Shoulda::Controller::XML::ClassMethods
extend ThoughtBot::Shoulda::Controller::XML::ClassMethods
end
end
@ -166,3 +167,4 @@ module Shoulda
end
end
end
end

View File

@ -1,17 +0,0 @@
begin
require 'active_support'
rescue LoadError
# Stolen straight from ActiveSupport
class Proc #:nodoc:
def bind(object)
block, time = self, Time.now
(class << object; self end).class_eval do
method_name = "__bind_#{time.to_i}_#{time.usec}"
define_method(method_name, &block)
method = instance_method(method_name)
remove_method(method_name)
method
end.bind(object)
end
end
end

View File

@ -0,0 +1,14 @@
# Stolen straight from ActiveSupport
class Proc #:nodoc:
def bind(object)
block, time = self, Time.now
(class << object; self end).class_eval do
method_name = "__bind_#{time.to_i}_#{time.usec}"
define_method(method_name, &block)
method = instance_method(method_name)
remove_method(method_name)
method
end.bind(object)
end
end

241
lib/shoulda/gem/shoulda.rb Normal file
View File

@ -0,0 +1,241 @@
require File.join(File.dirname(__FILE__), 'proc_extensions')
module Thoughtbot
module Shoulda
class << self
attr_accessor :current_context
end
VERSION = '1.1.1'
# = Should statements
#
# Should statements are just syntactic sugar over normal Test::Unit test methods. A should block
# contains all the normal code and assertions you're used to seeing, with the added benefit that
# they can be wrapped inside context blocks (see below).
#
# == Example:
#
# class UserTest << Test::Unit::TestCase
#
# def setup
# @user = User.new("John", "Doe")
# end
#
# should "return its full name"
# assert_equal 'John Doe', @user.full_name
# end
#
# end
#
# ...will produce the following test:
# * <tt>"test: User should return its full name. "</tt>
#
# Note: The part before <tt>should</tt> in the test name is gleamed from the name of the Test::Unit class.
def should(name, &blk)
should_eventually(name) && return unless block_given?
if Shoulda.current_context
Shoulda.current_context.should(name, &blk)
else
context_name = self.name.gsub(/Test/, "")
context = Thoughtbot::Shoulda::Context.new(context_name, self) do
should(name, &blk)
end
context.build
end
end
# Just like should, but never runs, and instead prints an 'X' in the Test::Unit output.
def should_eventually(name, &blk)
context_name = self.name.gsub(/Test/, "")
context = Thoughtbot::Shoulda::Context.new(context_name, self) do
should_eventually(name, &blk)
end
context.build
end
# = Contexts
#
# A context block groups should statements under a common set of setup/teardown methods.
# Context blocks can be arbitrarily nested, and can do wonders for improving the maintainability
# and readability of your test code.
#
# A context block can contain setup, should, should_eventually, and teardown blocks.
#
# class UserTest << Test::Unit::TestCase
# context "A User instance" do
# setup do
# @user = User.find(:first)
# end
#
# should "return its full name"
# assert_equal 'John Doe', @user.full_name
# end
# end
# end
#
# This code will produce the method <tt>"test: A User instance should return its full name. "</tt>.
#
# Contexts may be nested. Nested contexts run their setup blocks from out to in before each
# should statement. They then run their teardown blocks from in to out after each should statement.
#
# class UserTest << Test::Unit::TestCase
# context "A User instance" do
# setup do
# @user = User.find(:first)
# end
#
# should "return its full name"
# assert_equal 'John Doe', @user.full_name
# end
#
# context "with a profile" do
# setup do
# @user.profile = Profile.find(:first)
# end
#
# should "return true when sent :has_profile?"
# assert @user.has_profile?
# end
# end
# end
# end
#
# This code will produce the following methods
# * <tt>"test: A User instance should return its full name. "</tt>
# * <tt>"test: A User instance with a profile should return true when sent :has_profile?. "</tt>
#
# <b>Just like should statements, a context block can exist next to normal <tt>def test_the_old_way; end</tt>
# tests</b>. This means you do not have to fully commit to the context/should syntax in a test file.
def context(name, &blk)
if Shoulda.current_context
Shoulda.current_context.context(name, &blk)
else
context = Thoughtbot::Shoulda::Context.new(name, self, &blk)
context.build
end
end
class Context # :nodoc:
attr_accessor :name # my name
attr_accessor :parent # may be another context, or the original test::unit class.
attr_accessor :subcontexts # array of contexts nested under myself
attr_accessor :setup_block # block given via a setup method
attr_accessor :teardown_block # block given via a teardown method
attr_accessor :shoulds # array of hashes representing the should statements
attr_accessor :should_eventuallys # array of hashes representing the should eventually statements
def initialize(name, parent, &blk)
Shoulda.current_context = self
self.name = name
self.parent = parent
self.setup_block = nil
self.teardown_block = nil
self.shoulds = []
self.should_eventuallys = []
self.subcontexts = []
blk.bind(self).call
Shoulda.current_context = nil
end
def context(name, &blk)
subcontexts << Context.new(name, self, &blk)
Shoulda.current_context = self
end
def setup(&blk)
self.setup_block = blk
end
def teardown(&blk)
self.teardown_block = blk
end
def should(name, &blk)
self.shoulds << { :name => name, :block => blk }
end
def should_eventually(name, &blk)
self.should_eventuallys << { :name => name, :block => blk }
end
def full_name
parent_name = parent.full_name if am_subcontext?
return [parent_name, name].join(" ").strip
end
def am_subcontext?
parent.is_a?(self.class) # my parent is the same class as myself.
end
def test_unit_class
am_subcontext? ? parent.test_unit_class : parent
end
def create_test_from_should_hash(should)
test_name = ["test:", full_name, "should", "#{should[:name]}. "].flatten.join(' ').to_sym
if test_unit_class.instance_methods.include?(test_name.to_s)
warn " * WARNING: '#{test_name}' is already defined"
end
context = self
test_unit_class.send(:define_method, test_name) do |*args|
begin
context.run_all_setup_blocks(self)
should[:block].bind(self).call
ensure
context.run_all_teardown_blocks(self)
end
end
end
def run_all_setup_blocks(binding)
self.parent.run_all_setup_blocks(binding) if am_subcontext?
setup_block.bind(binding).call if setup_block
end
def run_all_teardown_blocks(binding)
teardown_block.bind(binding).call if teardown_block
self.parent.run_all_teardown_blocks(binding) if am_subcontext?
end
def print_should_eventuallys
should_eventuallys.each do |should|
test_name = [full_name, "should", "#{should[:name]}. "].flatten.join(' ')
puts " * DEFERRED: " + test_name
end
subcontexts.each { |context| context.print_should_eventuallys }
end
def build
shoulds.each do |should|
create_test_from_should_hash(should)
end
subcontexts.each { |context| context.build }
print_should_eventuallys
end
def method_missing(method, *args, &blk)
test_unit_class.send(method, *args, &blk)
end
end
end
end
module Test # :nodoc: all
module Unit
class TestCase
extend Thoughtbot::Shoulda
end
end
end

View File

@ -1,8 +1,9 @@
module Shoulda
module ThoughtBot # :nodoc:
module Shoulda # :nodoc:
module General
def self.included(other) # :nodoc:
other.class_eval do
extend Shoulda::General::ClassMethods
extend ThoughtBot::Shoulda::General::ClassMethods
end
end
@ -114,3 +115,4 @@ module Shoulda
end
end
end

View File

@ -1,4 +1,5 @@
module Shoulda
module ThoughtBot # :nodoc:
module Shoulda # :nodoc:
module Private # :nodoc:
# Returns the values for the entries in the args hash who's keys are listed in the wanted array.
# Will raise if there are keys in the args hash that aren't listed.
@ -18,3 +19,4 @@ module Shoulda
end
end
end
end

View File

@ -64,4 +64,10 @@ class ContextTest < Test::Unit::TestCase # :nodoc:
end
end
should_eventually "should pass, since it's unimplemented" do
flunk "what?"
end
should_eventually "should not require a block when using should_eventually"
should "should pass without a block, as that causes it to piggyback to should_eventually"
end

View File

@ -1,7 +1,7 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
class PrivateHelpersTest < Test::Unit::TestCase # :nodoc:
include Shoulda::ActiveRecord
include ThoughtBot::Shoulda::ActiveRecord
context "get_options!" do
should "remove opts from args" do
args = [:a, :b, {}]

View File

@ -1,236 +0,0 @@
require 'test/unit'
require 'rubygems'
require 'mocha'
class ShouldaTest < Test::Unit::TestCase # :nodoc:
should "be able to define a should statement outside of a context" do
assert true
end
should "see the name of my class as ShouldaTest" do
assert_equal "ShouldaTest", self.class.name
end
def self.should_see_class_methods
should "be able to see class methods" do
assert true
end
end
def self.should_see_a_context_block_like_a_Test_Unit_class
should "see a context block as a Test::Unit class" do
assert_equal "ShouldaTest", self.class.name
end
end
def self.should_see_blah
should "see @blah through a macro" do
assert @blah
end
end
def self.should_not_see_blah
should "not see @blah through a macro" do
assert_nil @blah
end
end
def self.should_be_able_to_make_context_macros(prefix = nil)
context "a macro" do
should "have the tests named correctly" do
assert_match(/^test: #{prefix}a macro should have the tests named correctly/, self.to_s)
end
end
end
context "Context" do
should_see_class_methods
should_see_a_context_block_like_a_Test_Unit_class
should_be_able_to_make_context_macros("Context ")
should "not define @blah" do
assert ! self.instance_variables.include?("@blah")
end
should_not_see_blah
should "be able to define a should statement" do
assert true
end
should "see the name of my class as ShouldaTest" do
assert_equal "ShouldaTest", self.class.name
end
context "with a subcontext" do
should_be_able_to_make_context_macros("Context with a subcontext ")
end
end
context "Context with setup block" do
setup do
@blah = "blah"
end
should "have @blah == 'blah'" do
assert_equal "blah", @blah
end
should_see_blah
should "have name set right" do
assert_match(/^test: Context with setup block/, self.to_s)
end
context "and a subcontext" do
setup do
@blah = "#{@blah} twice"
end
should "be named correctly" do
assert_match(/^test: Context with setup block and a subcontext should be named correctly/, self.to_s)
end
should "run the setup methods in order" do
assert_equal @blah, "blah twice"
end
should_see_blah
end
end
context "Another context with setup block" do
setup do
@blah = "foo"
end
should "have @blah == 'foo'" do
assert_equal "foo", @blah
end
should "have name set right" do
assert_match(/^test: Another context with setup block/, self.to_s)
end
should_see_blah
end
should_eventually "pass, since it's a should_eventually" do
flunk "what?"
end
should_eventually "should not require a block when using should_eventually"
should "should pass without a block, as that causes it to piggyback to should_eventually"
# Context creation and naming
def test_should_create_a_new_context
assert_nothing_raised do
Shoulda::Context.new("context name", self) do; end
end
end
def test_should_create_a_nested_context
assert_nothing_raised do
parent = Shoulda::Context.new("Parent", self) do; end
child = Shoulda::Context.new("Child", parent) do; end
end
end
def test_should_name_a_contexts_correctly
parent = Shoulda::Context.new("Parent", self) do; end
child = Shoulda::Context.new("Child", parent) do; end
grandchild = Shoulda::Context.new("GrandChild", child) do; end
assert_equal "Parent", parent.full_name
assert_equal "Parent Child", child.full_name
assert_equal "Parent Child GrandChild", grandchild.full_name
end
# Should statements
def test_should_have_should_hashes_when_given_should_statements
context = Shoulda::Context.new("name", self) do
should "be good" do; end
should "another" do; end
end
names = context.shoulds.map {|s| s[:name]}
assert_equal ["another", "be good"], names.sort
end
# setup and teardown
def test_should_capture_setup_and_teardown_blocks
context = Shoulda::Context.new("name", self) do
setup do; "setup"; end
teardown do; "teardown"; end
end
assert_equal "setup", context.setup_block.call
assert_equal "teardown", context.teardown_block.call
end
# building
def test_should_create_shoulda_test_for_each_should_on_build
context = Shoulda::Context.new("name", self) do
should "one" do; end
should "two" do; end
end
context.expects(:create_test_from_should_hash).with(has_entry(:name => "one"))
context.expects(:create_test_from_should_hash).with(has_entry(:name => "two"))
context.build
end
def test_should_create_test_methods_on_build
tu_class = Test::Unit::TestCase
context = Shoulda::Context.new("A Context", tu_class) do
should "define the test" do; end
end
tu_class.expects(:define_method).with(:"test: A Context should define the test. ")
context.build
end
def test_should_create_test_methods_on_build_when_subcontext
tu_class = Test::Unit::TestCase
context = Shoulda::Context.new("A Context", tu_class) do
context "with a child" do
should "define the test" do; end
end
end
tu_class.expects(:define_method).with(:"test: A Context with a child should define the test. ")
context.build
end
# Test::Unit integration
def test_should_create_a_new_context_and_build_it_on_Test_Unit_context
c = mock("context")
c.expects(:build)
Shoulda::Context.expects(:new).with("foo", kind_of(Class)).returns(c)
self.class.context "foo" do; end
end
def test_should_create_a_one_off_context_and_build_it_on_Test_Unit_should
s = mock("test")
Shoulda::Context.any_instance.expects(:should).with("rock").returns(s)
Shoulda::Context.any_instance.expects(:build)
self.class.should "rock" do; end
end
def test_should_define_a_test_on_should
s = mock("test")
Shoulda::Context.any_instance.expects(:should).with("rock").returns(s)
Shoulda::Context.any_instance.expects(:build)
self.class.should "rock" do; end
end
def test_should_create_a_one_off_context_and_build_it_on_Test_Unit_should_eventually
s = mock("test")
Shoulda::Context.any_instance.expects(:should_eventually).with("rock").returns(s)
Shoulda::Context.any_instance.expects(:build)
self.class.should_eventually "rock" do; end
end
end