Skip to content

Commit

Permalink
Removed all ThoughtBot (and Thoughtbot) namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
webmat committed Jan 5, 2009
1 parent 5ede009 commit 5c1329f
Show file tree
Hide file tree
Showing 22 changed files with 1,963 additions and 1,990 deletions.
8 changes: 4 additions & 4 deletions README.rdoc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Assertions:: Many common rails testing idioms have been distilled into a set of


= Usage = Usage


=== Context Helpers (ThoughtBot::Shoulda::Context) === Context Helpers (Shoulda::Context)


Stop killing your fingers with all of those underscores... Name your tests with plain sentences! Stop killing your fingers with all of those underscores... Name your tests with plain sentences!


Expand Down Expand Up @@ -43,7 +43,7 @@ Produces the following test methods:


So readable! So readable!


=== ActiveRecord Tests (ThoughtBot::Shoulda::ActiveRecord::Macros) === ActiveRecord Tests (Shoulda::ActiveRecord::Macros)


Quick macro tests for your ActiveRecord associations and validations: Quick macro tests for your ActiveRecord associations and validations:


Expand Down Expand Up @@ -71,7 +71,7 @@ Quick macro tests for your ActiveRecord associations and validations:


Makes TDD so much easier. Makes TDD so much easier.


=== Controller Tests (ThoughtBot::Shoulda::Controller::Macros) === Controller Tests (Shoulda::Controller::Macros)


Macros to test the most common controller patterns... Macros to test the most common controller patterns...


Expand All @@ -90,7 +90,7 @@ Macros to test the most common controller patterns...
end end
end end


=== Helpful Assertions (ThoughtBot::Shoulda::Assertions) === Helpful Assertions (Shoulda::Assertions)


More to come here, but have fun with what's there. More to come here, but have fun with what's there.


Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ task :default => ['test']


spec = Gem::Specification.new do |s| spec = Gem::Specification.new do |s|
s.name = "shoulda" s.name = "shoulda"
s.version = Thoughtbot::Shoulda::VERSION s.version = Shoulda::VERSION
s.summary = "Making tests easy on the fingers and eyes" s.summary = "Making tests easy on the fingers and eyes"
s.homepage = "http://thoughtbot.com/projects/shoulda" s.homepage = "http://thoughtbot.com/projects/shoulda"
s.rubyforge_project = "shoulda" s.rubyforge_project = "shoulda"
Expand Down
8 changes: 4 additions & 4 deletions lib/shoulda.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
module Test # :nodoc: all module Test # :nodoc: all
module Unit module Unit
class TestCase class TestCase
extend Thoughtbot::Shoulda extend Shoulda
include ThoughtBot::Shoulda::Assertions include Shoulda::Assertions
extend ThoughtBot::Shoulda::Macros extend Shoulda::Macros
include ThoughtBot::Shoulda::Helpers include Shoulda::Helpers
end end
end end
end end
2 changes: 1 addition & 1 deletion lib/shoulda/action_mailer.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module Test # :nodoc: all module Test # :nodoc: all
module Unit module Unit
class TestCase class TestCase
include ThoughtBot::Shoulda::ActionMailer::Assertions include Shoulda::ActionMailer::Assertions
end end
end end
end end
65 changes: 32 additions & 33 deletions lib/shoulda/action_mailer/assertions.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,39 +1,38 @@
module ThoughtBot # :nodoc: module Shoulda # :nodoc:
module Shoulda # :nodoc: module ActionMailer # :nodoc:
module ActionMailer # :nodoc: module Assertions
module Assertions # Asserts that an email was delivered. Can take a block that can further
# Asserts that an email was delivered. Can take a block that can further # narrow down the types of emails you're expecting.
# narrow down the types of emails you're expecting. #
# # assert_sent_email
# assert_sent_email #
# # Passes if ActionMailer::Base.deliveries has an email
# Passes if ActionMailer::Base.deliveries has an email #
# # assert_sent_email do |email|
# assert_sent_email do |email| # email.subject =~ /hi there/ && email.to.include?('none@none.com')
# email.subject =~ /hi there/ && email.to.include?('none@none.com') # end
# end #
# # Passes if there is an email with subject containing 'hi there' and
# Passes if there is an email with subject containing 'hi there' and # 'none@none.com' as one of the recipients.
# 'none@none.com' as one of the recipients. #
# def assert_sent_email
def assert_sent_email emails = ::ActionMailer::Base.deliveries
emails = ::ActionMailer::Base.deliveries assert !emails.empty?, "No emails were sent"
assert !emails.empty?, "No emails were sent" if block_given?
if block_given? matching_emails = emails.select {|email| yield email }
matching_emails = emails.select {|email| yield email } assert !matching_emails.empty?, "None of the emails matched."
assert !matching_emails.empty?, "None of the emails matched."
end
end end
end


# Asserts that no ActionMailer mails were delivered # Asserts that no ActionMailer mails were delivered
# #
# assert_did_not_send_email # assert_did_not_send_email
def assert_did_not_send_email def assert_did_not_send_email
msg = "Sent #{::ActionMailer::Base.deliveries.size} emails.\n" msg = "Sent #{::ActionMailer::Base.deliveries.size} emails.\n"
::ActionMailer::Base.deliveries.each { |m| msg << " '#{m.subject}' sent to #{m.to.to_sentence}\n" } ::ActionMailer::Base.deliveries.each { |m| msg << " '#{m.subject}' sent to #{m.to.to_sentence}\n" }
assert ::ActionMailer::Base.deliveries.empty?, msg assert ::ActionMailer::Base.deliveries.empty?, msg
end
end end
end end
end end
end end

4 changes: 2 additions & 2 deletions lib/shoulda/active_record.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
module Test # :nodoc: all module Test # :nodoc: all
module Unit module Unit
class TestCase class TestCase
include ThoughtBot::Shoulda::ActiveRecord::Assertions include Shoulda::ActiveRecord::Assertions
extend ThoughtBot::Shoulda::ActiveRecord::Macros extend Shoulda::ActiveRecord::Macros
end end
end end
end end
150 changes: 74 additions & 76 deletions lib/shoulda/active_record/assertions.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,86 +1,84 @@
module ThoughtBot # :nodoc: module Shoulda # :nodoc:
module Shoulda # :nodoc: module ActiveRecord # :nodoc:
module ActiveRecord # :nodoc: module Assertions
module Assertions # Asserts that the given object can be saved
# Asserts that the given object can be saved #
# # assert_save User.new(params)
# assert_save User.new(params) def assert_save(obj)
def assert_save(obj) assert obj.save, "Errors: #{pretty_error_messages obj}"
assert obj.save, "Errors: #{pretty_error_messages obj}" obj.reload
obj.reload end
end


# Asserts that the given object is valid # Asserts that the given object is valid
# #
# assert_valid User.new(params) # assert_valid User.new(params)
def assert_valid(obj) def assert_valid(obj)
assert obj.valid?, "Errors: #{pretty_error_messages obj}" assert obj.valid?, "Errors: #{pretty_error_messages obj}"
end end


# Asserts that an Active Record model validates with the passed # Asserts that an Active Record model validates with the passed
# <tt>value</tt> by making sure the <tt>error_message_to_avoid</tt> is not # <tt>value</tt> by making sure the <tt>error_message_to_avoid</tt> is not
# contained within the list of errors for that attribute. # contained within the list of errors for that attribute.
# #
# assert_good_value(User.new, :email, "user@example.com") # assert_good_value(User.new, :email, "user@example.com")
# assert_good_value(User.new, :ssn, "123456789", /length/) # assert_good_value(User.new, :ssn, "123456789", /length/)
# #
# If a class is passed as the first argument, a new object will be # If a class is passed as the first argument, a new object will be
# instantiated before the assertion. If an instance variable exists with # instantiated before the assertion. If an instance variable exists with
# the same name as the class (underscored), that object will be used # the same name as the class (underscored), that object will be used
# instead. # instead.
# #
# assert_good_value(User, :email, "user@example.com") # assert_good_value(User, :email, "user@example.com")
# #
# @product = Product.new(:tangible => false) # @product = Product.new(:tangible => false)
# assert_good_value(Product, :price, "0") # assert_good_value(Product, :price, "0")
def assert_good_value(object_or_klass, attribute, value, error_message_to_avoid = //) def assert_good_value(object_or_klass, attribute, value, error_message_to_avoid = //)
object = get_instance_of(object_or_klass) object = get_instance_of(object_or_klass)
object.send("#{attribute}=", value) object.send("#{attribute}=", value)
object.valid? object.valid?
assert_does_not_contain(object.errors.on(attribute), error_message_to_avoid, "when set to #{value.inspect}") assert_does_not_contain(object.errors.on(attribute), error_message_to_avoid, "when set to #{value.inspect}")
end end


# Asserts that an Active Record model invalidates the passed # Asserts that an Active Record model invalidates the passed
# <tt>value</tt> by making sure the <tt>error_message_to_expect</tt> is # <tt>value</tt> by making sure the <tt>error_message_to_expect</tt> is
# contained within the list of errors for that attribute. # contained within the list of errors for that attribute.
# #
# assert_bad_value(User.new, :email, "invalid") # assert_bad_value(User.new, :email, "invalid")
# assert_bad_value(User.new, :ssn, "123", /length/) # assert_bad_value(User.new, :ssn, "123", /length/)
# #
# If a class is passed as the first argument, a new object will be # If a class is passed as the first argument, a new object will be
# instantiated before the assertion. If an instance variable exists with # instantiated before the assertion. If an instance variable exists with
# the same name as the class (underscored), that object will be used # the same name as the class (underscored), that object will be used
# instead. # instead.
# #
# assert_bad_value(User, :email, "invalid") # assert_bad_value(User, :email, "invalid")
# #
# @product = Product.new(:tangible => true) # @product = Product.new(:tangible => true)
# assert_bad_value(Product, :price, "0") # assert_bad_value(Product, :price, "0")
def assert_bad_value(object_or_klass, attribute, value, def assert_bad_value(object_or_klass, attribute, value,
error_message_to_expect = self.class.default_error_message(:invalid)) error_message_to_expect = self.class.default_error_message(:invalid))
object = get_instance_of(object_or_klass) object = get_instance_of(object_or_klass)
object.send("#{attribute}=", value) object.send("#{attribute}=", value)
assert !object.valid?, "#{object.class} allowed #{value.inspect} as a value for #{attribute}" assert !object.valid?, "#{object.class} allowed #{value.inspect} as a value for #{attribute}"
assert object.errors.on(attribute), "There are no errors on #{attribute} after being set to #{value.inspect}" assert object.errors.on(attribute), "There are no errors on #{attribute} after being set to #{value.inspect}"
assert_contains(object.errors.on(attribute), error_message_to_expect, "when set to #{value.inspect}") assert_contains(object.errors.on(attribute), error_message_to_expect, "when set to #{value.inspect}")
end end


def pretty_error_messages(obj) def pretty_error_messages(obj)
obj.errors.map do |a, m| obj.errors.map do |a, m|
msg = "#{a} #{m}" msg = "#{a} #{m}"
msg << " (#{obj.send(a).inspect})" unless a.to_sym == :base msg << " (#{obj.send(a).inspect})" unless a.to_sym == :base
end
end end
end


private private


def get_instance_of(object_or_klass) def get_instance_of(object_or_klass)
if object_or_klass.is_a?(Class) if object_or_klass.is_a?(Class)
klass = object_or_klass klass = object_or_klass
instance_variable_get("@#{klass.to_s.underscore}") || klass.new instance_variable_get("@#{klass.to_s.underscore}") || klass.new
else else
object_or_klass object_or_klass
end
end end
end end
end end
Expand Down
Loading

0 comments on commit 5c1329f

Please sign in to comment.