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

assert_called

This commit is contained in:
utilum 2018-04-22 18:33:40 +02:00
parent e4e25cc8dc
commit 94ceda00b9
12 changed files with 102 additions and 66 deletions

View file

@ -1,6 +1,7 @@
# frozen_string_literal: true
require "test_helper"
require "active_support/testing/method_call_assertions"
require "stubs/test_connection"
require "stubs/room"
@ -143,6 +144,8 @@ module ActionCable::StreamTests
end
class StreamFromTest < ActionCable::TestCase
include ActiveSupport::Testing::MethodCallAssertions
setup do
@server = TestServer.new(subscription_adapter: ActionCable::SubscriptionAdapter::Async)
@server.config.allowed_request_origins = %w( http://rubyonrails.com )
@ -153,10 +156,11 @@ module ActionCable::StreamTests
connection = open_connection
subscribe_to connection, identifiers: { id: 1 }
connection.websocket.expects(:transmit)
@server.broadcast "test_room_1", { foo: "bar" }, { coder: DummyEncoder }
wait_for_async
wait_for_executor connection.server.worker_pool.executor
assert_called(connection.websocket, :transmit) do
@server.broadcast "test_room_1", { foo: "bar" }, { coder: DummyEncoder }
wait_for_async
wait_for_executor connection.server.worker_pool.executor
end
end
end
@ -175,10 +179,10 @@ module ActionCable::StreamTests
run_in_eventmachine do
connection = open_connection
expected = { "identifier" => { "channel" => MultiChatChannel.name }.to_json, "type" => "confirm_subscription" }
connection.websocket.expects(:transmit).with(expected.to_json)
receive(connection, command: "subscribe", channel: MultiChatChannel.name, identifiers: {})
wait_for_async
assert_called(connection.websocket, :transmit, [expected.to_json]) do
receive(connection, command: "subscribe", channel: MultiChatChannel.name, identifiers: {})
wait_for_async
end
end
end

View file

@ -7,6 +7,7 @@ require "websocket-client-simple"
require "json"
require "active_support/hash_with_indifferent_access"
require "active_support/testing/method_call_assertions"
####
# 😷 Warning suppression 😷
@ -27,6 +28,8 @@ WebSocket::Frame::Data.prepend Module.new {
####
class ClientTest < ActionCable::TestCase
include ActiveSupport::Testing::MethodCallAssertions
WAIT_WHEN_EXPECTING_EVENT = 2
WAIT_WHEN_NOT_EXPECTING_EVENT = 0.5
@ -289,9 +292,10 @@ class ClientTest < ActionCable::TestCase
subscriptions = app.connections.first.subscriptions.send(:subscriptions)
assert_not_equal 0, subscriptions.size, "Missing EchoChannel subscription"
channel = subscriptions.first[1]
channel.expects(:unsubscribed)
c.close
sleep 0.1 # Data takes a moment to process
assert_called(channel, :unsubscribed) do
c.close
sleep 0.1 # Data takes a moment to process
end
# All data is removed: No more connection or subscription information!
assert_equal(0, app.connections.count)

View file

@ -1,9 +1,12 @@
# frozen_string_literal: true
require "test_helper"
require "active_support/testing/method_call_assertions"
require "stubs/test_server"
class ActionCable::Connection::AuthorizationTest < ActionCable::TestCase
include ActiveSupport::Testing::MethodCallAssertions
class Connection < ActionCable::Connection::Base
attr_reader :websocket
@ -25,9 +28,9 @@ class ActionCable::Connection::AuthorizationTest < ActionCable::TestCase
"HTTP_HOST" => "localhost", "HTTP_ORIGIN" => "http://rubyonrails.com"
connection = Connection.new(server, env)
connection.websocket.expects(:close)
connection.process
assert_called(connection.websocket, :close) do
connection.process
end
end
end
end

View file

@ -3,8 +3,11 @@
require "test_helper"
require "stubs/test_server"
require "active_support/core_ext/object/json"
require "active_support/testing/method_call_assertions"
class ActionCable::Connection::BaseTest < ActionCable::TestCase
include ActiveSupport::Testing::MethodCallAssertions
class Connection < ActionCable::Connection::Base
attr_reader :websocket, :subscriptions, :message_buffer, :connected
@ -60,10 +63,10 @@ class ActionCable::Connection::BaseTest < ActionCable::TestCase
connection = open_connection
connection.websocket.expects(:transmit).with({ type: "welcome" }.to_json)
connection.message_buffer.expects(:process!)
connection.process
wait_for_async
assert_called(connection.message_buffer, :process!) do
connection.process
wait_for_async
end
assert_equal [ connection ], @server.connections
assert connection.connected
@ -80,8 +83,9 @@ class ActionCable::Connection::BaseTest < ActionCable::TestCase
connection.send :handle_open
assert connection.connected
connection.subscriptions.expects(:unsubscribe_from_all)
connection.send :handle_close
assert_called(connection.subscriptions, :unsubscribe_from_all) do
connection.send :handle_close
end
assert_not connection.connected
assert_equal [], @server.connections
@ -106,8 +110,9 @@ class ActionCable::Connection::BaseTest < ActionCable::TestCase
connection = open_connection
connection.process
connection.websocket.expects(:close)
connection.close
assert_called(connection.websocket, :close) do
connection.close
end
end
end

View file

@ -44,8 +44,9 @@ class ActionCable::Connection::IdentifierTest < ActionCable::TestCase
run_in_eventmachine do
open_connection_with_stubbed_pubsub
@connection.websocket.expects(:close)
@connection.process_internal_message "type" => "disconnect"
assert_called(@connection.websocket, :close) do
@connection.process_internal_message "type" => "disconnect"
end
end
end

View file

@ -1,9 +1,12 @@
# frozen_string_literal: true
require "test_helper"
require "active_support/testing/method_call_assertions"
require "stubs/test_server"
class ActionCable::Connection::StreamTest < ActionCable::TestCase
include ActiveSupport::Testing::MethodCallAssertions
class Connection < ActionCable::Connection::Base
attr_reader :connected, :websocket, :errors
@ -42,9 +45,10 @@ class ActionCable::Connection::StreamTest < ActionCable::TestCase
# Internal hax = :(
client = connection.websocket.send(:websocket)
client.instance_variable_get("@stream").instance_variable_get("@rack_hijack_io").expects(:write).raises(closed_exception, "foo")
client.expects(:client_gone)
client.write("boo")
assert_called(client, :client_gone) do
client.write("boo")
end
assert_equal [], connection.errors
end
end

View file

@ -1,8 +1,11 @@
# frozen_string_literal: true
require "test_helper"
require "active_support/testing/method_call_assertions"
class ActionCable::Connection::SubscriptionsTest < ActionCable::TestCase
include ActiveSupport::Testing::MethodCallAssertions
class Connection < ActionCable::Connection::Base
attr_reader :websocket
@ -55,9 +58,11 @@ class ActionCable::Connection::SubscriptionsTest < ActionCable::TestCase
subscribe_to_chat_channel
channel = subscribe_to_chat_channel
channel.expects(:unsubscribe_from_channel)
@subscriptions.execute_command "command" => "unsubscribe", "identifier" => @chat_identifier
assert_called(channel, :unsubscribe_from_channel) do
@subscriptions.execute_command "command" => "unsubscribe", "identifier" => @chat_identifier
end
assert_empty @subscriptions.identifiers
end
end
@ -92,10 +97,11 @@ class ActionCable::Connection::SubscriptionsTest < ActionCable::TestCase
channel2_id = ActiveSupport::JSON.encode(id: 2, channel: "ActionCable::Connection::SubscriptionsTest::ChatChannel")
channel2 = subscribe_to_chat_channel(channel2_id)
channel1.expects(:unsubscribe_from_channel)
channel2.expects(:unsubscribe_from_channel)
@subscriptions.unsubscribe_from_all
assert_called(channel1, :unsubscribe_from_channel) do
assert_called(channel2, :unsubscribe_from_channel) do
@subscriptions.unsubscribe_from_all
end
end
end
end

View file

@ -3,8 +3,11 @@
require "test_helper"
require "stubs/test_server"
require "active_support/core_ext/hash/indifferent_access"
require "active_support/testing/method_call_assertions"
class BaseTest < ActiveSupport::TestCase
include ActiveSupport::Testing::MethodCallAssertions
def setup
@server = ActionCable::Server::Base.new
@server.config.cable = { adapter: "async" }.with_indifferent_access
@ -19,17 +22,20 @@ class BaseTest < ActiveSupport::TestCase
conn = FakeConnection.new
@server.add_connection(conn)
conn.expects(:close)
@server.restart
assert_called(conn, :close) do
@server.restart
end
end
test "#restart shuts down worker pool" do
@server.worker_pool.expects(:halt)
@server.restart
assert_called(@server.worker_pool, :halt) do
@server.restart
end
end
test "#restart shuts down pub/sub adapter" do
@server.pubsub.expects(:shutdown)
@server.restart
assert_called(@server.pubsub, :shutdown) do
@server.restart
end
end
end

View file

@ -2105,9 +2105,10 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
end
def test_association_proxy_transaction_method_starts_transaction_in_association_class
Comment.expects(:transaction)
Post.first.comments.transaction do
# nothing
assert_called(Comment, :transaction) do
Post.first.comments.transaction do
# nothing
end
end
end

View file

@ -488,8 +488,9 @@ class NamedScopingTest < ActiveRecord::TestCase
[:public_method, :protected_method, :private_method].each do |reserved_method|
assert Topic.respond_to?(reserved_method, true)
ActiveRecord::Base.logger.expects(:warn)
silence_warnings { Topic.scope(reserved_method, -> {}) }
assert_called(ActiveRecord::Base.logger, :warn) do
silence_warnings { Topic.scope(reserved_method, -> {}) }
end
end
end

View file

@ -152,25 +152,25 @@ module ActiveRecord
def test_creates_configurations_with_local_ip
@configurations["development"].merge!("host" => "127.0.0.1")
ActiveRecord::Tasks::DatabaseTasks.expects(:create)
ActiveRecord::Tasks::DatabaseTasks.create_all
assert_called(ActiveRecord::Tasks::DatabaseTasks, :create) do
ActiveRecord::Tasks::DatabaseTasks.create_all
end
end
def test_creates_configurations_with_local_host
@configurations["development"].merge!("host" => "localhost")
ActiveRecord::Tasks::DatabaseTasks.expects(:create)
ActiveRecord::Tasks::DatabaseTasks.create_all
assert_called(ActiveRecord::Tasks::DatabaseTasks, :create) do
ActiveRecord::Tasks::DatabaseTasks.create_all
end
end
def test_creates_configurations_with_blank_hosts
@configurations["development"].merge!("host" => nil)
ActiveRecord::Tasks::DatabaseTasks.expects(:create)
ActiveRecord::Tasks::DatabaseTasks.create_all
assert_called(ActiveRecord::Tasks::DatabaseTasks, :create) do
ActiveRecord::Tasks::DatabaseTasks.create_all
end
end
end
@ -368,25 +368,25 @@ module ActiveRecord
def test_drops_configurations_with_local_ip
@configurations[:development].merge!("host" => "127.0.0.1")
ActiveRecord::Tasks::DatabaseTasks.expects(:drop)
ActiveRecord::Tasks::DatabaseTasks.drop_all
assert_called(ActiveRecord::Tasks::DatabaseTasks, :drop) do
ActiveRecord::Tasks::DatabaseTasks.drop_all
end
end
def test_drops_configurations_with_local_host
@configurations[:development].merge!("host" => "localhost")
ActiveRecord::Tasks::DatabaseTasks.expects(:drop)
ActiveRecord::Tasks::DatabaseTasks.drop_all
assert_called(ActiveRecord::Tasks::DatabaseTasks, :drop) do
ActiveRecord::Tasks::DatabaseTasks.drop_all
end
end
def test_drops_configurations_with_blank_hosts
@configurations[:development].merge!("host" => nil)
ActiveRecord::Tasks::DatabaseTasks.expects(:drop)
ActiveRecord::Tasks::DatabaseTasks.drop_all
assert_called(ActiveRecord::Tasks::DatabaseTasks, :drop) do
ActiveRecord::Tasks::DatabaseTasks.drop_all
end
end
end
@ -645,8 +645,9 @@ module ActiveRecord
end
def test_migrate_clears_schema_cache_afterward
ActiveRecord::Base.expects(:clear_cache!)
ActiveRecord::Tasks::DatabaseTasks.migrate
assert_called(ActiveRecord::Base, :clear_cache!) do
ActiveRecord::Tasks::DatabaseTasks.migrate
end
end
end

View file

@ -204,9 +204,9 @@ if current_adapter?(:SQLite3Adapter)
def test_structure_dump_with_ignore_tables
dbfile = @database
filename = "awesome-file.sql"
ActiveRecord::SchemaDumper.expects(:ignore_tables).returns(["foo"])
ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, filename, "/rails/root")
assert_called(ActiveRecord::SchemaDumper, :ignore_tables, returns: ["foo"]) do
ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, filename, "/rails/root")
end
assert File.exist?(dbfile)
assert File.exist?(filename)
assert_match(/bar/, File.read(filename))