Skip to content

Commit

Permalink
Updated to work with NestedAttributes instead of NestedParams.
Browse files Browse the repository at this point in the history
Made tests green with new ActiveSupport::TestCase.
  • Loading branch information
alloy committed Nov 29, 2008
1 parent 889d140 commit f620ba2
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 38 deletions.
6 changes: 3 additions & 3 deletions app/models/project.rb
@@ -1,8 +1,8 @@
class Project < ActiveRecord::Base
# Automatically turns on autosave and thus also validates
has_one :author, :nested_params => true
has_many :tasks, :dependent => :destroy, :nested_params => true, :destroy_missing => true
has_and_belongs_to_many :tags, :nested_params => true, :destroy_missing => true
has_one :author, :accept_nested_attributes => true
has_many :tasks, :dependent => :destroy, :accept_nested_attributes => true, :destroy_missing => true
has_and_belongs_to_many :tags, :accept_nested_attributes => true, :destroy_missing => true

validates_presence_of :name
end
12 changes: 0 additions & 12 deletions test/functional/projects_controller_test.rb
Expand Up @@ -74,16 +74,4 @@
project.reload
project.name.should == 'NestedParams'
end

it "should rollback any changes to the project and tasks if an exception occurs in one of the tasks" do
Task.any_instance.stubs(:save).raises

lambda {
put :update, :id => @project.id, :project => @valid_update_params
@project.reload
}.should.raise

@project.name.should == 'NestedParams'
@project.tasks.map(&:name).sort.should == ['Check other implementations', 'Try with our plugin']
end
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Expand Up @@ -5,7 +5,7 @@
require 'test/spec'
require 'test/spec/rails'

class Test::Unit::TestCase
class ActiveSupport::TestCase
# Transactional fixtures accelerate your tests by wrapping each test method
# in a transaction that's rolled back on completion. This ensures that the
# test database remains unchanged so your fixtures don't have to be reloaded
Expand Down
12 changes: 0 additions & 12 deletions test/unit/project_test.rb
Expand Up @@ -54,16 +54,4 @@
@project.name.should == 'NestedParams and AutosaveAssociation'
@project.tasks.first.name.should == 'Just start!'
end

it "should rollback any changes to the project and tasks if an exception occurs in one of the tasks" do
Task.any_instance.stubs(:save).raises

@project.attributes = @valid_update_params
lambda { @project.save }.should.raise

@project.reload

@project.name.should == 'NestedParams'
@project.tasks.map(&:name).sort.should == ['Check other implementations', 'Try with our plugin']
end
end
53 changes: 53 additions & 0 deletions vendor/plugins/on_test_spec/lib/test/spec/add_allow_switch.rb
@@ -0,0 +1,53 @@
require 'active_support'

class Class
def add_allow_switch(method, options={})
default = options[:default] || false

class_eval do
cattr_accessor "allow_#{method}"
self.send("allow_#{method}=", default)

alias_method "original_#{method}", method

eval %{
def #{method}(*args)
if allow_#{method}
original_#{method}(*args)
else
raise RuntimeError, "You're trying to call `#{method}' on `#{self}', which you probably don't want in a test."
end
end
}, binding, __FILE__, __LINE__
end
end
end

class Module
def add_allow_switch(method, options={})
default = options[:default] || false

mattr_accessor "allow_#{method}"
send("allow_#{method}=", default)

unless respond_to?(:__metaclass___)
def __metaclass__
class << self; self; end
end
end

__metaclass__.class_eval do
alias_method "original_#{method}", method

eval %{
def #{method}(*args)
if allow_#{method}
original_#{method}(*args)
else
raise RuntimeError, "You're trying to call `#{method}' on `#{self}', which you probably don't want in a test."
end
end
}, binding, __FILE__, __LINE__
end
end
end
34 changes: 28 additions & 6 deletions vendor/plugins/on_test_spec/lib/test/spec/rails.rb
Expand Up @@ -2,18 +2,40 @@

module Test
module Spec
class Should
include ActionController::Assertions
module Rails
module Assertions
include ActiveSupport::Testing::Assertions
include ActionController::TestCase::Assertions
end
end

class ShouldNot
include ActionController::Assertions
class Should
include Rails::Assertions
end

module Rails
class ShouldNot
include Rails::Assertions
end
end
end

%w(test_spec_ext spec_responder expectations).each { |lib| require "test/spec/rails/#{lib}" }
Dir[File.dirname(__FILE__) + '/rails/**/*_helpers.rb'].each { |lib| require lib }
Dir[File.dirname(__FILE__) + '/rails/**/*_helpers.rb'].each { |lib| require lib }

module Kernel
alias :context_before_on_test_spec :context
alias :xcontext_before_on_test_spec :xcontext

def context(name, superclass=ActiveSupport::TestCase, klass=Test::Spec::TestCase, &block)
context_before_on_test_spec(name, superclass, klass, &block)
end

def xcontext(name, superclass=ActiveSupport::TestCase, &block)
xcontext_before_on_test_spec(name, superclass, &block)
end

private :context, :xcontext

alias :describe :context
alias :xdescribe :xcontext
end
Expand Up @@ -9,6 +9,8 @@ def tests(controller_class)
end
end
module InstanceMethods
include Assertions

attr_reader :controller

# Sets up the test environment for functional tests
Expand All @@ -21,12 +23,15 @@ def rescue_action(e)
@controller = controller_class.new
@controller.request = @request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new

@controller.params = {}
@controller.send(:initialize_current_url)
end
end
end
end
end
end

Test::Spec::TestCase::ClassMethods.send(:include, Test::Spec::Rails::Controller::ClassMethods)
Test::Unit::TestCase.send(:include, Test::Spec::Rails::Controller::InstanceMethods)
ActiveSupport::TestCase.send(:extend, Test::Spec::Rails::Controller::ClassMethods)
ActiveSupport::TestCase.send(:include, Test::Spec::Rails::Controller::InstanceMethods)
13 changes: 13 additions & 0 deletions vendor/plugins/on_test_spec/lib/test/spec/rails/expectations.rb
Expand Up @@ -2,6 +2,8 @@ module Test
module Spec
module Rails
module ShouldExpectations
include Assertions

# Test that we were redirected somewhere:
# should.redirect
#
Expand Down Expand Up @@ -62,8 +64,14 @@ def cache_pages(*pages, &block)
end
assert pages.all? { |page| files.include?(page) }
end

# Test two HTML strings for equivalency (e.g., identical up to reordering of attributes)
def dom_equal(expected)
assert_dom_equal expected, @object
end
end
module ShouldNotExpectations
include Assertions

# Test that an object is not valid
def validate
Expand All @@ -77,6 +85,11 @@ def differ(*args)
assert_no_difference(*args, &@object)
end
alias change differ

# Test that two HTML strings are not equivalent
def dom_equal(expected)
assert_dom_not_equal expected, @object
end
end
end
end
Expand Down
Expand Up @@ -8,4 +8,4 @@ module RequestHelpers
end
end

Test::Unit::TestCase.send(:include, Test::Spec::Rails::RequestHelpers)
ActiveSupport::TestCase.send(:include, Test::Spec::Rails::RequestHelpers)
Expand Up @@ -39,4 +39,4 @@ def layout
end
end

Test::Unit::TestCase.send(:include, Test::Spec::Rails::ResponseHelpers)
ActiveSupport::TestCase.send(:include, Test::Spec::Rails::ResponseHelpers)
104 changes: 104 additions & 0 deletions vendor/plugins/on_test_spec/test/add_allow_switch_test.rb
@@ -0,0 +1,104 @@
require 'rubygems' rescue LoadError

require 'test/spec'
require 'mocha'

require File.expand_path('../../lib/test/spec/add_allow_switch', __FILE__)

module Factory
def self.run
true
end
end
Factory.add_allow_switch :run, :default => true

describe "Factory with an allow switch on run" do
it "should alias the original method" do
Factory.respond_to?(:original_run, include_private=true).should == true
end

it "should define a getter and setter" do
Factory.should.respond_to(:allow_run)
Factory.should.respond_to(:allow_run=)
end

it "should switch off" do
Factory.allow_run = false
lambda {
Factory.run
}.should.raise(RuntimeError)
end

it "should switch on" do
Factory.allow_run = true
lambda {
Factory.run.should == true
}.should.not.raise
end
end

class Bunny
def hop
'Hop hop!'
end
end
Bunny.add_allow_switch :hop

describe "Bunny with an allow switch on hop" do
before do
@bunny = Bunny.new
end

it "should alias the original method" do
@bunny.respond_to?(:original_hop).should == true
end

it "should define a getter and setter" do
Bunny.should.respond_to(:allow_hop)
Bunny.should.respond_to(:allow_hop=)
end

it "should switch off" do
Bunny.allow_hop = false
lambda {
@bunny.hop
}.should.raise(RuntimeError)
end

it "should switch on" do
Bunny.allow_hop = true
lambda {
@bunny.hop.should == 'Hop hop!'
}.should.not.raise
end
end


Kernel.add_allow_switch :system

describe "Kernel with an allow switch on system" do
SILENT_COMMANT = 'ls > /dev/null'

it "should alias the original method" do
Kernel.respond_to?(:original_system, include_private=true).should == true
end

it "should define a getter and setter" do
Factory.should.respond_to(:allow_system)
Factory.should.respond_to(:allow_system=)
end

it "should switch off" do
Kernel.allow_system = false
lambda {
Kernel.system(SILENT_COMMANT)
}.should.raise(RuntimeError)
end

it "should switch on" do
Kernel.allow_system = true
lambda {
Kernel.system(SILENT_COMMANT)
}.should.not.raise
end
end

0 comments on commit f620ba2

Please sign in to comment.