From a68c792d73b86f651430f01586222825c20f7b46 Mon Sep 17 00:00:00 2001 From: Kevin Dew Date: Tue, 12 Apr 2022 18:53:52 +0100 Subject: [PATCH] Create organisation before stubbing patch_links Creating an organisation causes a call to Whitehall::PublishingApi.patch_links due to Active Record callbacks. In Mocha 1.12.0 there was a change (presumably https://github.com/freerange/mocha/pull/474) that causes the test to start failing in it's previous incantation. My understanding is that this fail occurs because the organisation is being created after the mock (`expects`) is applied to Whitehall::PublishingApi, which leads to the test failing because it thinks the `patch_links` method has been invoked twice. As per: ``` Error: patch_links::#organisations#test_patches_links_for_organisations: DRb::DRbRemoteError: unexpected invocation: Whitehall::PublishingApi.patch_links(#, {:bulk_publishing => true}) unsatisfied expectations: - expected exactly once, invoked twice: Whitehall::PublishingApi.patch_links(#, {:bulk_publishing => true}) satisfied expectations: - allowed any number of times, invoked never: #.persisted?(any_parameters) - allowed any number of times, invoked never: #.id(any_parameters) - allowed any number of times, invoked never: Services.asset_manager(any_parameters) (Minitest::Assertion) lib/tasks/publishing_api.rake:152:in `block (4 levels) in ' lib/tasks/publishing_api.rake:149:in `each' lib/tasks/publishing_api.rake:149:in `each_with_index' lib/tasks/publishing_api.rake:149:in `block (3 levels) in ' test/unit/tasks/publishing_api_test.rb:165:in `block (3 levels) in ' ``` --- test/unit/tasks/publishing_api_test.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/unit/tasks/publishing_api_test.rb b/test/unit/tasks/publishing_api_test.rb index fca7d7b7386d..4f29b2264e8a 100644 --- a/test/unit/tasks/publishing_api_test.rb +++ b/test/unit/tasks/publishing_api_test.rb @@ -159,8 +159,11 @@ class PublishingApiRake < ActiveSupport::TestCase let(:task) { Rake::Task["publishing_api:patch_links:organisations"] } test "patches links for organisations" do + # Organisation needs to be created before the method is stubed + organisation = create(:organisation) + Whitehall::PublishingApi.expects(:patch_links).with( - create(:organisation), bulk_publishing: true + organisation, bulk_publishing: true ).once task.invoke end