Skip to content

Commit

Permalink
Replace permissive Mocha expectations
Browse files Browse the repository at this point in the history
Step 6 in rails#33162

When using Mocha like this:

`ActiveRecord::Base.expects(:establish_connection).with(some_args)`,

the expectations created look something like this:

```
@EXPECTATIONS=
          [#<Expectation:0x561350d968e0 expected exactly once, not yet invoked: ActiveRecord::Base.establish_connection("adapter" => "mysql2", "database" => nil) >,
           #<Expectation:0x561350dab8f8 allowed any number of times, not yet invoked: ActiveRecord::Base.establish_connection(any_parameters) >,
           #<Expectation:0x561350dc30c0 allowed any number of times, not yet invoked: ActiveRecord::Base.connection(any_parameters) >]
```

Minitest mocking (and the way we use it in `MethodCallAssertions`)
expressly refuses to facilitate such permissiive expectations, insisting
that all calls be specified in the actual expected order.

This patch replaces such calls to `Mocha#expects` with
`ActiveSupport::Testing::MethodCallAssertions` and specifies all
expected calls in the epxected order.
  • Loading branch information
utilum committed Jul 22, 2018
1 parent d899375 commit 82e42c1
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 32 deletions.
29 changes: 20 additions & 9 deletions activerecord/test/cases/tasks/mysql_rake_test.rb
Expand Up @@ -21,11 +21,17 @@ def teardown
end

def test_establishes_connection_without_database
ActiveRecord::Base.stubs(:establish_connection)
ActiveRecord::Base.stub(:connection, @connection) do
ActiveRecord::Base.expects(:establish_connection).
with("adapter" => "mysql2", "database" => nil)
ActiveRecord::Tasks::DatabaseTasks.create @configuration
assert_called_with(
ActiveRecord::Base,
:establish_connection,
[
[ "adapter" => "mysql2", "database" => nil ],
[ "adapter" => "mysql2", "database" => "my-app-db" ],
]
) do
ActiveRecord::Tasks::DatabaseTasks.create @configuration
end
end
end

Expand Down Expand Up @@ -58,12 +64,17 @@ def test_creates_database_with_given_collation
end

def test_establishes_connection_to_database
ActiveRecord::Base.stubs(:establish_connection)

ActiveRecord::Base.stub(:connection, @connection) do
ActiveRecord::Base.expects(:establish_connection).with(@configuration)

ActiveRecord::Tasks::DatabaseTasks.create @configuration
assert_called_with(
ActiveRecord::Base,
:establish_connection,
[
["adapter" => "mysql2", "database" => nil],
[@configuration]
]
) do
ActiveRecord::Tasks::DatabaseTasks.create @configuration
end
end
end

Expand Down
89 changes: 66 additions & 23 deletions activerecord/test/cases/tasks/postgresql_rake_test.rb
Expand Up @@ -21,14 +21,24 @@ def teardown
end

def test_establishes_connection_to_postgresql_database
ActiveRecord::Base.stubs(:establish_connection)
ActiveRecord::Base.stub(:connection, @connection) do
ActiveRecord::Base.expects(:establish_connection).with(
"adapter" => "postgresql",
"database" => "postgres",
"schema_search_path" => "public"
)
ActiveRecord::Tasks::DatabaseTasks.create @configuration
assert_called_with(
ActiveRecord::Base,
:establish_connection,
[
[
"adapter" => "postgresql",
"database" => "postgres",
"schema_search_path" => "public"
],
[
"adapter" => "postgresql",
"database" => "my-app-db"
]
]
) do
ActiveRecord::Tasks::DatabaseTasks.create @configuration
end
end
end

Expand Down Expand Up @@ -78,11 +88,23 @@ def test_creates_database_with_given_collation_and_ctype
end

def test_establishes_connection_to_new_database
ActiveRecord::Base.stubs(:establish_connection)
ActiveRecord::Base.stub(:connection, @connection) do
ActiveRecord::Base.expects(:establish_connection).with(@configuration)

ActiveRecord::Tasks::DatabaseTasks.create @configuration
assert_called_with(
ActiveRecord::Base,
:establish_connection,
[
[
"adapter" => "postgresql",
"database" => "postgres",
"schema_search_path" => "public"
],
[
@configuration
]
]
) do
ActiveRecord::Tasks::DatabaseTasks.create @configuration
end
end
end

Expand Down Expand Up @@ -207,15 +229,24 @@ def test_clears_active_connections
end

def test_establishes_connection_to_postgresql_database
ActiveRecord::Base.stubs(:establish_connection)
with_stubbed_connection do
ActiveRecord::Base.expects(:establish_connection).with(
"adapter" => "postgresql",
"database" => "postgres",
"schema_search_path" => "public"
)

ActiveRecord::Tasks::DatabaseTasks.purge @configuration
assert_called_with(
ActiveRecord::Base,
:establish_connection,
[
[
"adapter" => "postgresql",
"database" => "postgres",
"schema_search_path" => "public"
],
[
"adapter" => "postgresql",
"database" => "my-app-db"
]
]
) do
ActiveRecord::Tasks::DatabaseTasks.purge @configuration
end
end
end

Expand Down Expand Up @@ -244,11 +275,23 @@ def test_creates_database
end

def test_establishes_connection
ActiveRecord::Base.stubs(:establish_connection)
with_stubbed_connection do
ActiveRecord::Base.expects(:establish_connection).with(@configuration)

ActiveRecord::Tasks::DatabaseTasks.purge @configuration
assert_called_with(
ActiveRecord::Base,
:establish_connection,
[
[
"adapter" => "postgresql",
"database" => "postgres",
"schema_search_path" => "public"
],
[
@configuration
]
]
) do
ActiveRecord::Tasks::DatabaseTasks.purge @configuration
end
end
end

Expand Down

0 comments on commit 82e42c1

Please sign in to comment.