提交 796c83fc 编写于 作者: R Ronak Jangir

Removed mocha from Active Record Part 1

上级 2170950b
......@@ -425,13 +425,16 @@ def test_statement_closed
configurations['arunit']['database'])
statement = ::SQLite3::Statement.new(db,
'CREATE TABLE statement_test (number integer not null)')
statement.stubs(:step).raises(::SQLite3::BusyException, 'busy')
statement.stubs(:columns).once.returns([])
statement.expects(:close).once
::SQLite3::Statement.stubs(:new).returns(statement)
assert_raises ActiveRecord::StatementInvalid do
@conn.exec_query 'select * from statement_test'
statement.stub(:step, ->{ raise ::SQLite3::BusyException.new('busy') }) do
assert_called(statement, :columns, returns: []) do
assert_called(statement, :close) do
::SQLite3::Statement.stub(:new, statement) do
assert_raises ActiveRecord::StatementInvalid do
@conn.exec_query 'select * from statement_test'
end
end
end
end
end
end
......
......@@ -108,53 +108,57 @@ def test_duplicate_middle_objects
end
def test_preloading_has_many_in_multiple_queries_with_more_ids_than_database_can_handle
Comment.connection.expects(:in_clause_length).at_least_once.returns(5)
posts = Post.all.merge!(:includes=>:comments).to_a
assert_equal 11, posts.size
assert_called(Comment.connection, :in_clause_length, returns: 5) do
posts = Post.all.merge!(:includes=>:comments).to_a
assert_equal 11, posts.size
end
end
def test_preloading_has_many_in_one_queries_when_database_has_no_limit_on_ids_it_can_handle
Comment.connection.expects(:in_clause_length).at_least_once.returns(nil)
posts = Post.all.merge!(:includes=>:comments).to_a
assert_equal 11, posts.size
assert_called(Comment.connection, :in_clause_length, returns: nil) do
posts = Post.all.merge!(:includes=>:comments).to_a
assert_equal 11, posts.size
end
end
def test_preloading_habtm_in_multiple_queries_with_more_ids_than_database_can_handle
Comment.connection.expects(:in_clause_length).at_least_once.returns(5)
posts = Post.all.merge!(:includes=>:categories).to_a
assert_equal 11, posts.size
assert_called(Comment.connection, :in_clause_length, times: 2, returns: 5) do
posts = Post.all.merge!(:includes=>:categories).to_a
assert_equal 11, posts.size
end
end
def test_preloading_habtm_in_one_queries_when_database_has_no_limit_on_ids_it_can_handle
Comment.connection.expects(:in_clause_length).at_least_once.returns(nil)
posts = Post.all.merge!(:includes=>:categories).to_a
assert_equal 11, posts.size
assert_called(Comment.connection, :in_clause_length, times: 2, returns: nil) do
posts = Post.all.merge!(:includes=>:categories).to_a
assert_equal 11, posts.size
end
end
def test_load_associated_records_in_one_query_when_adapter_has_no_limit
Comment.connection.expects(:in_clause_length).at_least_once.returns(nil)
post = posts(:welcome)
assert_queries(2) do
Post.includes(:comments).where(:id => post.id).to_a
assert_called(Comment.connection, :in_clause_length, returns: nil) do
post = posts(:welcome)
assert_queries(2) do
Post.includes(:comments).where(:id => post.id).to_a
end
end
end
def test_load_associated_records_in_several_queries_when_many_ids_passed
Comment.connection.expects(:in_clause_length).at_least_once.returns(1)
post1, post2 = posts(:welcome), posts(:thinking)
assert_queries(3) do
Post.includes(:comments).where(:id => [post1.id, post2.id]).to_a
assert_called(Comment.connection, :in_clause_length, returns: 1) do
post1, post2 = posts(:welcome), posts(:thinking)
assert_queries(3) do
Post.includes(:comments).where(:id => [post1.id, post2.id]).to_a
end
end
end
def test_load_associated_records_in_one_query_when_a_few_ids_passed
Comment.connection.expects(:in_clause_length).at_least_once.returns(3)
post = posts(:welcome)
assert_queries(2) do
Post.includes(:comments).where(:id => post.id).to_a
assert_called(Comment.connection, :in_clause_length, returns: 3) do
post = posts(:welcome)
assert_queries(2) do
Post.includes(:comments).where(:id => post.id).to_a
end
end
end
......
......@@ -796,9 +796,10 @@ def test_count
end
def test_association_proxy_transaction_method_starts_transaction_in_association_class
Post.expects(:transaction)
Category.first.posts.transaction do
# nothing
assert_called(Post, :transaction) do
Category.first.posts.transaction do
# nothing
end
end
end
......
......@@ -744,8 +744,9 @@ def test_get_ids
def test_get_ids_for_has_many_through_with_conditions_should_not_preload
Tagging.create!(:taggable_type => 'Post', :taggable_id => posts(:welcome).id, :tag => tags(:misc))
ActiveRecord::Associations::Preloader.expects(:new).never
posts(:welcome).misc_tag_ids
assert_not_called(ActiveRecord::Associations::Preloader, :new) do
posts(:welcome).misc_tag_ids
end
end
def test_get_ids_for_loaded_associations
......@@ -765,9 +766,10 @@ def test_get_ids_for_unloaded_associations_does_not_load_them
end
def test_association_proxy_transaction_method_starts_transaction_in_association_class
Tag.expects(:transaction)
Post.first.tags.transaction do
# nothing
assert_called(Tag, :transaction) do
Post.first.tags.transaction do
# nothing
end
end
end
......
......@@ -67,8 +67,9 @@ def test_attribute_present_with_booleans
def test_caching_nil_primary_key
klass = Class.new(Minimalistic)
klass.expects(:reset_primary_key).returns(nil).once
2.times { klass.primary_key }
assert_called(klass, :reset_primary_key, returns: nil) do
2.times { klass.primary_key }
end
end
def test_attribute_keys_on_new_instance
......
......@@ -69,13 +69,15 @@ def test_each_should_execute_if_id_is_in_select
end
def test_warn_if_limit_scope_is_set
ActiveRecord::Base.logger.expects(:warn)
Post.limit(1).find_each { |post| post }
assert_called(ActiveRecord::Base.logger, :warn) do
Post.limit(1).find_each { |post| post }
end
end
def test_warn_if_order_scope_is_set
ActiveRecord::Base.logger.expects(:warn)
Post.order("title").find_each { |post| post }
assert_called(ActiveRecord::Base.logger, :warn) do
Post.order("title").find_each { |post| post }
end
end
def test_logger_not_required
......@@ -137,14 +139,15 @@ def test_find_in_batches_should_quote_batch_order
def test_find_in_batches_should_not_use_records_after_yielding_them_in_case_original_array_is_modified
not_a_post = "not a post"
not_a_post.stubs(:id).raises(StandardError, "not_a_post had #id called on it")
assert_nothing_raised do
Post.find_in_batches(:batch_size => 1) do |batch|
assert_kind_of Array, batch
assert_kind_of Post, batch.first
def not_a_post.id; end
not_a_post.stub(:id, ->{ raise StandardError.new("not_a_post had #id called on it") }) do
assert_nothing_raised do
Post.find_in_batches(:batch_size => 1) do |batch|
assert_kind_of Array, batch
assert_kind_of Post, batch.first
batch.map! { not_a_post }
batch.map! { not_a_post }
end
end
end
end
......
......@@ -259,18 +259,19 @@ def test_serialized_fixtures
def test_fixtures_are_set_up_with_database_env_variable
db_url_tmp = ENV['DATABASE_URL']
ENV['DATABASE_URL'] = "sqlite3::memory:"
ActiveRecord::Base.stubs(:configurations).returns({})
test_case = Class.new(ActiveRecord::TestCase) do
fixtures :accounts
ActiveRecord::Base.stub(:configurations, {}) do
test_case = Class.new(ActiveRecord::TestCase) do
fixtures :accounts
def test_fixtures
assert accounts(:signals37)
def test_fixtures
assert accounts(:signals37)
end
end
end
result = test_case.new(:test_fixtures).run
result = test_case.new(:test_fixtures).run
assert result.passed?, "Expected #{result.name} to pass:\n#{result}"
assert result.passed?, "Expected #{result.name} to pass:\n#{result}"
end
ensure
ENV['DATABASE_URL'] = db_url_tmp
end
......
......@@ -3,6 +3,7 @@
require 'config'
require 'active_support/testing/autorun'
require 'active_support/testing/method_call_assertions'
require 'stringio'
require 'active_record'
......@@ -141,6 +142,7 @@ def disable_extension!(extension, connection)
class ActiveSupport::TestCase
include ActiveRecord::TestFixtures
include ActiveRecord::ValidationsRepairHelper
include ActiveSupport::Testing::MethodCallAssertions
self.fixture_path = FIXTURES_ROOT
self.use_instantiated_fixtures = false
......
......@@ -262,61 +262,66 @@ def test_cache_gets_cleared_after_migration
end
def test_find
Task.connection.expects(:clear_query_cache).times(1)
assert_called(Task.connection, :clear_query_cache) do
assert !Task.connection.query_cache_enabled
Task.cache do
assert Task.connection.query_cache_enabled
Task.find(1)
assert !Task.connection.query_cache_enabled
Task.cache do
assert Task.connection.query_cache_enabled
Task.find(1)
Task.uncached do
assert !Task.connection.query_cache_enabled
Task.find(1)
end
Task.uncached do
assert !Task.connection.query_cache_enabled
Task.find(1)
assert Task.connection.query_cache_enabled
end
assert Task.connection.query_cache_enabled
assert !Task.connection.query_cache_enabled
end
assert !Task.connection.query_cache_enabled
end
def test_update
Task.connection.expects(:clear_query_cache).times(2)
Task.cache do
task = Task.find(1)
task.starting = Time.now.utc
task.save!
assert_called(Task.connection, :clear_query_cache, times: 2) do
Task.cache do
task = Task.find(1)
task.starting = Time.now.utc
task.save!
end
end
end
def test_destroy
Task.connection.expects(:clear_query_cache).times(2)
Task.cache do
Task.find(1).destroy
assert_called(Task.connection, :clear_query_cache, times: 2) do
Task.cache do
Task.find(1).destroy
end
end
end
def test_insert
ActiveRecord::Base.connection.expects(:clear_query_cache).times(2)
Task.cache do
Task.create!
assert_called(ActiveRecord::Base.connection, :clear_query_cache, times: 2) do
Task.cache do
Task.create!
end
end
end
def test_cache_is_expired_by_habtm_update
ActiveRecord::Base.connection.expects(:clear_query_cache).times(2)
ActiveRecord::Base.cache do
c = Category.first
p = Post.first
p.categories << c
assert_called(ActiveRecord::Base.connection, :clear_query_cache, times: 2) do
ActiveRecord::Base.cache do
c = Category.first
p = Post.first
p.categories << c
end
end
end
def test_cache_is_expired_by_habtm_delete
ActiveRecord::Base.connection.expects(:clear_query_cache).times(2)
ActiveRecord::Base.cache do
p = Post.find(1)
assert p.categories.any?
p.categories.delete_all
assert_called(ActiveRecord::Base.connection, :clear_query_cache, times: 2) do
ActiveRecord::Base.cache do
p = Post.find(1)
assert p.categories.any?
p.categories.delete_all
end
end
end
end
......@@ -393,12 +393,14 @@ def test_join_table
product = Struct.new(:table_name, :pluralize_table_names).new('products', true)
reflection = ActiveRecord::Reflection.create(:has_many, :categories, nil, {}, product)
reflection.stubs(:klass).returns(category)
assert_equal 'categories_products', reflection.join_table
reflection.stub(:klass, category) do
assert_equal 'categories_products', reflection.join_table
end
reflection = ActiveRecord::Reflection.create(:has_many, :products, nil, {}, category)
reflection.stubs(:klass).returns(product)
assert_equal 'categories_products', reflection.join_table
reflection.stub(:klass, product) do
assert_equal 'categories_products', reflection.join_table
end
end
def test_join_table_with_common_prefix
......@@ -406,12 +408,14 @@ def test_join_table_with_common_prefix
product = Struct.new(:table_name, :pluralize_table_names).new('catalog_products', true)
reflection = ActiveRecord::Reflection.create(:has_many, :categories, nil, {}, product)
reflection.stubs(:klass).returns(category)
assert_equal 'catalog_categories_products', reflection.join_table
reflection.stub(:klass, category) do
assert_equal 'catalog_categories_products', reflection.join_table
end
reflection = ActiveRecord::Reflection.create(:has_many, :products, nil, {}, category)
reflection.stubs(:klass).returns(product)
assert_equal 'catalog_categories_products', reflection.join_table
reflection.stub(:klass, product) do
assert_equal 'catalog_categories_products', reflection.join_table
end
end
def test_join_table_with_different_prefix
......@@ -419,12 +423,14 @@ def test_join_table_with_different_prefix
page = Struct.new(:table_name, :pluralize_table_names).new('content_pages', true)
reflection = ActiveRecord::Reflection.create(:has_many, :categories, nil, {}, page)
reflection.stubs(:klass).returns(category)
assert_equal 'catalog_categories_content_pages', reflection.join_table
reflection.stub(:klass, category) do
assert_equal 'catalog_categories_content_pages', reflection.join_table
end
reflection = ActiveRecord::Reflection.create(:has_many, :pages, nil, {}, category)
reflection.stubs(:klass).returns(page)
assert_equal 'catalog_categories_content_pages', reflection.join_table
reflection.stub(:klass, page) do
assert_equal 'catalog_categories_content_pages', reflection.join_table
end
end
def test_join_table_can_be_overridden
......@@ -432,12 +438,14 @@ def test_join_table_can_be_overridden
product = Struct.new(:table_name, :pluralize_table_names).new('products', true)
reflection = ActiveRecord::Reflection.create(:has_many, :categories, nil, { :join_table => 'product_categories' }, product)
reflection.stubs(:klass).returns(category)
assert_equal 'product_categories', reflection.join_table
reflection.stub(:klass, category) do
assert_equal 'product_categories', reflection.join_table
end
reflection = ActiveRecord::Reflection.create(:has_many, :products, nil, { :join_table => 'product_categories' }, category)
reflection.stubs(:klass).returns(product)
assert_equal 'product_categories', reflection.join_table
reflection.stub(:klass, product) do
assert_equal 'product_categories', reflection.join_table
end
end
def test_includes_accepts_symbols
......
......@@ -188,8 +188,9 @@ def test_any_should_not_load_results
def test_any_should_call_proxy_found_if_using_a_block
topics = Topic.base
assert_queries(1) do
topics.expects(:empty?).never
topics.any? { true }
assert_not_called(topics, :empty?) do
topics.any? { true }
end
end
end
......@@ -217,8 +218,9 @@ def test_many_should_not_load_results
def test_many_should_call_proxy_found_if_using_a_block
topics = Topic.base
assert_queries(1) do
topics.expects(:size).never
topics.many? { true }
assert_not_called(topics, :size) do
topics.many? { true }
end
end
end
......
......@@ -53,8 +53,9 @@ def replied_topic
test "validates_uniqueness_of on generated message #{name}" do
Topic.validates_uniqueness_of :title, validation_options
@topic.title = unique_topic.title
@topic.errors.expects(:generate_message).with(:title, :taken, generate_message_options.merge(:value => 'unique!'))
@topic.valid?
assert_called_with(@topic.errors, :generate_message, [:title, :taken, generate_message_options.merge(:value => 'unique!')]) do
@topic.valid?
end
end
end
......@@ -63,8 +64,9 @@ def replied_topic
COMMON_CASES.each do |name, validation_options, generate_message_options|
test "validates_associated on generated message #{name}" do
Topic.validates_associated :replies, validation_options
replied_topic.errors.expects(:generate_message).with(:replies, :invalid, generate_message_options.merge(:value => replied_topic.replies))
replied_topic.save
assert_called_with(replied_topic.errors, :generate_message, [:replies, :invalid, generate_message_options.merge(:value => replied_topic.replies)]) do
replied_topic.save
end
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册