Skip to content

Commit

Permalink
Create organisation before stubbing patch_links
Browse files Browse the repository at this point in the history
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
freerange/mocha#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(#<Organisation:0x3e620>, {:bulk_publishing => true})
unsatisfied expectations:
- expected exactly once, invoked twice: Whitehall::PublishingApi.patch_links(#<Organisation:0x3e530>, {:bulk_publishing => true})
satisfied expectations:
- allowed any number of times, invoked never: #<User:0x3e490>.persisted?(any_parameters)
- allowed any number of times, invoked never: #<User:0x3e490>.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 <top (required)>'
    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 <top (required)>'
    test/unit/tasks/publishing_api_test.rb:165:in `block (3 levels) in <class:PublishingApiRake>'
```
  • Loading branch information
kevindew committed Apr 19, 2022
1 parent ca9ecae commit a68c792
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion test/unit/tasks/publishing_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a68c792

Please sign in to comment.