Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jcsjcs/241313 to your computer and use it in GitHub Desktop.
Save jcsjcs/241313 to your computer and use it in GitHub Desktop.
# db/migrate/20091123163221_create_orders.rb
class CreateOrders < ActiveRecord::Migration
def self.up
create_table :orders do |t|
t.string :name
t.timestamps
end
end
def self.down
drop_table :orders
end
end
# db/migrate/20091123163241_create_items.rb
class CreateItems < ActiveRecord::Migration
def self.up
create_table :items do |t|
t.string :name
t.decimal :cost
t.belongs_to :order
t.timestamps
end
end
def self.down
drop_table :items
end
end
# app/models/order.rb
class Order < ActiveRecord::Base
has_many :items
end
# app/models/item.rb
class Item < ActiveRecord::Base
belongs_to :order
end
# config/environment.rb
config.gem "mocha" # gem 0.9.8
# test/unit/item_test.rb
require 'test_helper'
require "mocha"
class ItemTest < ActiveSupport::TestCase
# test "association proxy works with mocha" do
# widget = Widget.new
# widget.thingummies.stubs(:sum).returns(99)
# assert_equal 99, widget.thingummies.sum
# end
test "association proxy works with mocha" do
widget = Order.new
widget.items.stubs(:sum).returns(99.0)
assert_equal 99.0, widget.items.sum(:cost).to_f
end
end
#
# $ rake test:units
# (in /home/james/ra/test_mocha)
# /usr/bin/ruby1.8 -I"lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/unit/order_test.rb" "test/unit/item_test.rb" "test/unit/helpers/items_helper_test.rb" "test/unit/helpers/orders_helper_test.rb"
# Loaded suite /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader
# Started
# F.
# Finished in 0.072515 seconds.
#
# 1) Failure:
# test_association_proxy_works_with_mocha(ItemTest) [/test/unit/item_test.rb:15]:
# <99.0> expected but was
# <0.0>.
#
# 2 tests, 2 assertions, 1 failures, 0 errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment