1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #21566 from ronakjangir47/active_record2

Removed mocha from Active Record Part 2
This commit is contained in:
Kasper Timm Hansen 2015-09-16 21:34:17 +02:00
commit 79b201e4e3
8 changed files with 85 additions and 58 deletions

View file

@ -1299,11 +1299,12 @@ class BasicsTest < ActiveRecord::TestCase
end
def test_compute_type_no_method_error
ActiveSupport::Dependencies.stubs(:safe_constantize).raises(NoMethodError)
ActiveSupport::Dependencies.stub(:safe_constantize, proc{ raise NoMethodError }) do
assert_raises NoMethodError do
ActiveRecord::Base.send :compute_type, 'InvalidModel'
end
end
end
def test_compute_type_on_undefined_method
error = nil
@ -1315,20 +1316,22 @@ class BasicsTest < ActiveRecord::TestCase
error = e
end
ActiveSupport::Dependencies.stubs(:safe_constantize).raises(e)
ActiveSupport::Dependencies.stub(:safe_constantize, proc{ raise e }) do
exception = assert_raises NameError do
ActiveRecord::Base.send :compute_type, 'InvalidModel'
end
assert_equal error.message, exception.message
end
end
def test_compute_type_argument_error
ActiveSupport::Dependencies.stubs(:safe_constantize).raises(ArgumentError)
ActiveSupport::Dependencies.stub(:safe_constantize, proc{ raise ArgumentError }) do
assert_raises ArgumentError do
ActiveRecord::Base.send :compute_type, 'InvalidModel'
end
end
end
def test_clear_cache!
# preheat cache

View file

@ -39,21 +39,21 @@ if ActiveRecord::Base.connection.supports_explain?
binds = [[], []]
queries = sqls.zip(binds)
connection.stubs(:explain).returns('query plan foo', 'query plan bar')
stub_explain_for_query_plans do
expected = sqls.map {|sql| "EXPLAIN for: #{sql}\nquery plan #{sql}"}.join("\n")
assert_equal expected, base.exec_explain(queries)
end
end
def test_exec_explain_with_binds
cols = [Object.new, Object.new]
cols[0].expects(:name).returns('wadus')
cols[1].expects(:name).returns('chaflan')
object = Struct.new(:name)
cols = [object.new('wadus'), object.new('chaflan')]
sqls = %w(foo bar)
binds = [[[cols[0], 1]], [[cols[1], 2]]]
queries = sqls.zip(binds)
connection.stubs(:explain).returns("query plan foo\n", "query plan bar\n")
stub_explain_for_query_plans(["query plan foo\n", "query plan bar\n"]) do
expected = <<-SQL.strip_heredoc
EXPLAIN for: #{sqls[0]} [["wadus", 1]]
query plan foo
@ -63,14 +63,25 @@ if ActiveRecord::Base.connection.supports_explain?
SQL
assert_equal expected, base.exec_explain(queries)
end
end
def test_unsupported_connection_adapter
connection.stubs(:supports_explain?).returns(false)
base.logger.expects(:warn).never
connection.stub(:supports_explain?, false) do
assert_not_called(base.logger, :warn) do
Car.where(:name => 'honda').to_a
end
end
end
private
def stub_explain_for_query_plans(query_plans = ['query plan foo', 'query plan bar'])
explain_called = 0
connection.stub(:explain, proc{ explain_called += 1; query_plans[explain_called - 1] }) do
yield
end
end
end
end

View file

@ -178,9 +178,10 @@ class FinderTest < ActiveRecord::TestCase
end
def test_exists_does_not_instantiate_records
Developer.expects(:instantiate).never
assert_not_called(Developer, :instantiate) do
Developer.exists?
end
end
def test_find_by_array_of_one_id
assert_kind_of(Array, Topic.find([ 1 ]))

View file

@ -408,10 +408,12 @@ class FixturesWithoutInstantiationTest < ActiveRecord::TestCase
end
def test_reloading_fixtures_through_accessor_methods
topic = Struct.new(:title)
assert_equal "The First Topic", topics(:first).title
@loaded_fixtures['topics']['first'].expects(:find).returns(stub(:title => "Fresh Topic!"))
assert_called(@loaded_fixtures['topics']['first'], :find, returns: topic.new("Fresh Topic!")) do
assert_equal "Fresh Topic!", topics(:first, true).title
end
end
end
class FixturesWithoutInstanceInstantiationTest < ActiveRecord::TestCase

View file

@ -31,7 +31,8 @@ module ActiveRecord
end
def test_unknown_commands_delegate
recorder = CommandRecorder.new(stub(:foo => 'bar'))
recorder = Struct.new(:foo)
recorder = CommandRecorder.new(recorder.new('bar'))
assert_equal 'bar', recorder.foo
end

View file

@ -273,11 +273,12 @@ class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase
end
def test_should_modify_an_existing_record_if_there_is_a_matching_composite_id
@ship.stubs(:id).returns('ABC1X')
@ship.stub(:id, 'ABC1X') do
@pirate.ship_attributes = { :id => @ship.id, :name => 'Davy Jones Gold Dagger' }
assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
end
end
def test_should_destroy_an_existing_record_if_there_is_a_matching_id_and_destroy_is_truthy
@pirate.ship.destroy
@ -457,11 +458,12 @@ class TestNestedAttributesOnABelongsToAssociation < ActiveRecord::TestCase
end
def test_should_modify_an_existing_record_if_there_is_a_matching_composite_id
@pirate.stubs(:id).returns('ABC1X')
@pirate.stub(:id, 'ABC1X') do
@ship.pirate_attributes = { :id => @pirate.id, :catchphrase => 'Arr' }
assert_equal 'Arr', @ship.pirate.catchphrase
end
end
def test_should_destroy_an_existing_record_if_there_is_a_matching_id_and_destroy_is_truthy
@ship.pirate.destroy
@ -638,8 +640,8 @@ module NestedAttributesOnACollectionAssociationTests
end
def test_should_take_a_hash_with_composite_id_keys_and_assign_the_attributes_to_the_associated_models
@child_1.stubs(:id).returns('ABC1X')
@child_2.stubs(:id).returns('ABC2X')
@child_1.stub(:id, 'ABC1X') do
@child_2.stub(:id, 'ABC2X') do
@pirate.attributes = {
association_getter => [
@ -650,6 +652,8 @@ module NestedAttributesOnACollectionAssociationTests
assert_equal ['Grace OMalley', 'Privateers Greed'], [@child_1.name, @child_2.name]
end
end
end
def test_should_raise_RecordNotFound_if_an_id_is_given_but_doesnt_return_a_record
exception = assert_raise ActiveRecord::RecordNotFound do

View file

@ -55,10 +55,11 @@ module ActiveRecord
test '#order! on non-string does not attempt regexp match for references' do
obj = Object.new
obj.expects(:=~).never
assert_not_called(obj, :=~) do
assert relation.order!(obj)
assert_equal [obj], relation.order_values
end
end
test '#references!' do
assert relation.references!(:foo).equal?(relation)

View file

@ -487,15 +487,19 @@ class TransactionTest < ActiveRecord::TestCase
end
def test_rollback_when_commit_raises
Topic.connection.expects(:begin_db_transaction)
Topic.connection.expects(:commit_db_transaction).raises('OH NOES')
Topic.connection.expects(:rollback_db_transaction)
assert_called(Topic.connection, :begin_db_transaction) do
Topic.connection.stub(:commit_db_transaction, ->{ raise('OH NOES') }) do
assert_called(Topic.connection, :rollback_db_transaction) do
assert_raise RuntimeError do
e = assert_raise RuntimeError do
Topic.transaction do
# do nothing
end
end
assert_equal 'OH NOES', e.message
end
end
end
end
def test_rollback_when_saving_a_frozen_record