Skip to content

Commit

Permalink
Fixed errors and warnings related to Rails 3
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Jun 8, 2010
1 parent 207ace9 commit b1b6ac4
Show file tree
Hide file tree
Showing 206 changed files with 10,007 additions and 122 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,7 +1,8 @@
test/rails_root/log/*.log
test/*/log/*.log
doc
coverage
.svn/
pkg
*.swp
*.swo
tags
10 changes: 7 additions & 3 deletions Rakefile
Expand Up @@ -10,7 +10,7 @@ load 'tasks/shoulda.rake'
# Test::Unit::UI::VERBOSE
test_files_pattern = 'test/{unit,functional,other,matchers}/**/*_test.rb'
Rake::TestTask.new do |t|
t.libs << 'lib'
t.libs << 'lib' << 'test'
t.pattern = test_files_pattern
t.verbose = false
end
Expand All @@ -35,8 +35,12 @@ task :sync_docs => 'rdoc' do
`rsync -ave ssh doc/ dev@dev.thoughtbot.com:/home/dev/www/dev.thoughtbot.com/shoulda`
end

desc 'Default: run tests.'
task :default => ['test']
desc 'Default: run tests for all supported versions of Rails'
task :default do
%w(2.3.8 3.0.0.beta3).each do |version|
system("RAILS_VERSION=#{version} rake -s test;")
end
end

spec = Gem::Specification.new do |s|
s.name = "shoulda"
Expand Down
6 changes: 1 addition & 5 deletions lib/shoulda/action_controller/matchers/assign_to_matcher.rb
Expand Up @@ -102,11 +102,7 @@ def equal_to_expected_value?
end

def assigned_value
assigns[@variable]
end

def assigns
@controller.response.template.assigns
@controller.instance_variable_get("@#{@variable}")
end

end
Expand Down
19 changes: 18 additions & 1 deletion lib/shoulda/action_controller/matchers/filter_param_matcher.rb
Expand Up @@ -37,19 +37,36 @@ def description
private

def filters_params?
@controller.respond_to?(:filter_parameters)
@controller.respond_to?(:filter_parameters) ||
request.respond_to?(:filtered_parameters)
end

def filters_key?
filtered_value == '[FILTERED]'
end

def filtered_value
if request.respond_to?(:filtered_parameters)
filtered_request_value
else
filtered_controller_value
end
end

def filtered_controller_value
filtered = @controller.send(:filter_parameters,
@key.to_s => @key.to_s)
filtered[@key.to_s]
end

def filtered_request_value
request.env['action_dispatch.request.parameters'] = { @key.to_s => 'value' }
request.filtered_parameters[@key.to_s]
end

def request
@request ||= ::ActionController::TestRequest.new
end
end

end
Expand Down
Expand Up @@ -14,7 +14,7 @@ def render_template(template)
class RenderTemplateMatcher # :nodoc:

def initialize(template, context)
@template = template
@template = template.to_s
@context = context
end

Expand Down
Expand Up @@ -9,14 +9,21 @@ module Matchers
# it { should render_with_layout }
# it { should render_with_layout(:special) }
# it { should_not render_with_layout }
def render_with_layout(layout = nil)
RenderWithLayout.new(layout)
def render_with_layout(expected_layout = nil)
RenderWithLayout.new(expected_layout)
end

class RenderWithLayout # :nodoc:

def initialize(layout)
@layout = layout.to_s unless layout.nil?
def initialize(expected_layout)
@expected_layout = expected_layout.to_s unless expected_layout.nil?
end

# Used to provide access to layouts recorded by
# ActionController::TemplateAssertions in Rails 3
def in_context(context)
@context = context
self
end

def matches?(controller)
Expand All @@ -34,31 +41,41 @@ def negative_failure_message

def description
description = "render with "
if @layout.nil?
if @expected_layout.nil?
description << "a layout"
else
description << "the #{@layout.inspect} layout"
description << "the #{@expected_layout.inspect} layout"
end
description
end

private

def rendered_with_layout?
!layout.blank?
!rendered_layouts.empty?
end

def rendered_with_expected_layout?
return true if @layout.nil?
layout == @layout
return true if @expected_layout.nil?
rendered_layouts.include?(@expected_layout)
end

def layout
layout = @controller.response.layout
if layout.nil?
nil
def rendered_layouts
if recorded_layouts
recorded_layouts.keys.compact.map { |layout| layout.sub(%r{^layouts/}, '') }
else
layout.split('/').last
layout = @controller.response.layout
if layout.nil?
[]
else
[layout.split('/').last]
end
end
end

def recorded_layouts
if @context
@context.instance_variable_get('@layouts')
end
end

Expand All @@ -68,7 +85,8 @@ def expectation

def result
if rendered_with_layout?
"rendered with the #{layout.inspect} layout"
"rendered with " <<
rendered_layouts.map { |layout| layout.inspect }.join(", ")
else
"rendered without a layout"
end
Expand Down
Expand Up @@ -55,7 +55,7 @@ def negative_failure_message
protected

def response_content_type
@controller.response.content_type
@controller.response.content_type.to_s
end

def lookup_by_extension(extension)
Expand Down
Expand Up @@ -64,7 +64,11 @@ def symbol_to_status_code(potential_symbol)
when :missing then 404
when :error then 500..599
when Symbol
::ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE[potential_symbol]
if defined?(::Rack::Utils::SYMBOL_TO_STATUS_CODE)
::Rack::Utils::SYMBOL_TO_STATUS_CODE[potential_symbol]
else
::ActionController::Base::SYMBOL_TO_STATUS_CODE[potential_symbol]
end
else
potential_symbol
end
Expand Down
2 changes: 1 addition & 1 deletion lib/shoulda/action_controller/matchers/route_matcher.rb
Expand Up @@ -36,6 +36,7 @@ def initialize(method, path, context)

def to(params)
@params = params
stringify_params!
self
end

Expand All @@ -47,7 +48,6 @@ def in_context(context)
def matches?(controller)
@controller = controller
guess_controller!
stringify_params!
route_recognized?
end

Expand Down
Expand Up @@ -70,7 +70,11 @@ def assigned_value
end

def session
@controller.response.session.data
if @controller.request.respond_to?(:session)
@controller.request.session.to_hash
else
@controller.response.session.data
end
end

def expectation
Expand Down
Expand Up @@ -60,7 +60,7 @@ def regexp_value_matches?
end

def flash
@controller.response.session['flash']
@controller.send(:flash)
end

def expectation
Expand Down
7 changes: 6 additions & 1 deletion lib/shoulda/active_record/helpers.rb
Expand Up @@ -17,7 +17,12 @@ def pretty_error_messages(obj) # :nodoc:
# default_error_message(:too_long, :count => 60)
def default_error_message(key, values = {})
if Object.const_defined?(:I18n) # Rails >= 2.2
I18n.translate("activerecord.errors.messages.#{key}", values)
result = I18n.translate("activerecord.errors.messages.#{key}", values)
if result =~ /^translation missing/
I18n.translate("errors.messages.#{key}", values)
else
result
end
else # Rails <= 2.1.x
::ActiveRecord::Errors.default_error_messages[key] % values[:count]
end
Expand Down
12 changes: 10 additions & 2 deletions lib/shoulda/active_record/matchers/allow_value_matcher.rb
Expand Up @@ -59,9 +59,17 @@ def description

def errors_match?
@instance.valid?
@errors = @instance.errors.on(@attribute)
@errors = errors_for_attribute(@instance, @attribute)
@errors = [@errors] unless @errors.is_a?(Array)
@expected_message ? (errors_match_regexp? || errors_match_string?) : (@errors != [nil])
@expected_message ? (errors_match_regexp? || errors_match_string?) : (@errors.compact.any?)
end

def errors_for_attribute(instance, attribute)
if instance.errors.respond_to?(:[])
instance.errors[attribute]
else
instance.errors.on(attribute)
end
end

def errors_match_regexp?
Expand Down
4 changes: 0 additions & 4 deletions lib/shoulda/rails.rb
Expand Up @@ -6,7 +6,3 @@
require 'shoulda/action_controller' if defined? ActionController::Base
require 'shoulda/action_mailer' if defined? ActionMailer::Base

if defined?(RAILS_ROOT)
# load in the 3rd party macros from vendorized plugins and gems
Shoulda.autoload_macros RAILS_ROOT, File.join("vendor", "{plugins,gems}", "*")
end
1 change: 1 addition & 0 deletions rails/init.rb
Expand Up @@ -3,5 +3,6 @@
require 'shoulda/rspec'
else
require 'shoulda/rails'
Shoulda.autoload_macros RAILS_ROOT, File.join("vendor", "{plugins,gems}", "*")
end
end
2 changes: 1 addition & 1 deletion test/functional/posts_controller_test.rb
@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/../test_helper'
require 'test_helper'
require 'posts_controller'

# Re-raise errors caught by the controller.
Expand Down
2 changes: 1 addition & 1 deletion test/functional/users_controller_test.rb
@@ -1,4 +1,4 @@
require File.dirname(__FILE__) + '/../test_helper'
require 'test_helper'
require 'users_controller'

# Re-raise errors caught by the controller.
Expand Down
21 changes: 16 additions & 5 deletions test/matchers/action_mailer/have_sent_email_test.rb
Expand Up @@ -5,13 +5,24 @@ class HaveSentEmailTest < ActiveSupport::TestCase # :nodoc:
setup do
define_mailer :mailer, [:the_email] do
def the_email
from "do-not-reply@example.com"
recipients "myself@me.com"
subject "This is spam"
body :body => "Every email is spam."
if defined?(AbstractController::Rendering)
mail :from => "do-not-reply@example.com",
:to => "myself@me.com",
:subject => "This is spam",
:body => "Every email is spam."
else
from "do-not-reply@example.com"
recipients "myself@me.com"
subject "This is spam"
body "Every email is spam."
end
end
end
@mail = Mailer.create_the_email
if defined?(AbstractController::Rendering)
@mail = Mailer.the_email
else
@mail = Mailer.create_the_email
end
end

should "accept based on the subject" do
Expand Down
@@ -1,4 +1,4 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
require 'test_helper'

class AllowMassAssignmentOfMatcherTest < ActiveSupport::TestCase # :nodoc:

Expand Down
2 changes: 1 addition & 1 deletion test/matchers/active_record/allow_value_matcher_test.rb
@@ -1,4 +1,4 @@
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
require 'test_helper'

class AllowValueMatcherTest < ActiveSupport::TestCase # :nodoc:

Expand Down

0 comments on commit b1b6ac4

Please sign in to comment.