rspec-sidekiq icon indicating copy to clipboard operation
rspec-sidekiq copied to clipboard

has_enqueued_sidekiq_job is not matching even though they are identical

Open theSociableme opened this issue 5 years ago • 5 comments

  expected to have an enqueued ApexChatEnrichmentJob job
         arguments: [[{"data"=>{"chatId"=>"3078079", "chatAccountId"=>"3269891", "chatSummary"=>"Hello\\nBye", "chatTextFull"=>"Visitor: Bye\\nSystem: The chat session has ended.\\n", "provider"=>"Apex", "visitorId"=>"baca09be-79d7-46c2-81d3-0e12732f0244", "country"=>"USA"}, "eventTime"=>"2019-03-08 18:29:09", "eventType"=>"Chat", "platform"=>"USA", "scope"=>"CHAT", "scopeId"=>"172716", "subType"=>"Lead"}]]
       found
         arguments: [[{"data"=>{"chatId"=>"3078079", "chatAccountId"=>"3269891", "chatSummary"=>"Hello\\nBye", "chatTextFull"=>"Visitor: Bye\\nSystem: The chat session has ended.\\n", "provider"=>"Apex", "visitorId"=>"baca09be-79d7-46c2-81d3-0e12732f0244", "country"=>"USA"}, "eventTime"=>"2019-03-08 18:29:09", "eventType"=>"Chat", "platform"=>"USA", "scope"=>"CHAT", "scopeId"=>"172716", "subType"=>"Lead"}]]

Here is my spec

 context 'when chat event is from Apex' do
      let(:apex_chat_params) do
        {
          'country' => 'USA',
          'data': {
            'chatId' => '3078079',
            'chatAccountId' => '3269891',
            'chatSummary' => 'Hello\nBye',
            'chatTextFull' => 'Visitor: Bye\nSystem: The chat session has ended.\n',
            'provider' => 'Apex',
            'visitorId' => 'baca09be-79d7-46c2-81d3-0e12732f0244'
          },
          'eventTime' => '2019-03-08 18:29:09',
          'eventType' => 'Chat',
          'scope' => 'CHAT',
          'scopeId' => '172716',
          'subType' => 'Lead'
        }
      end

      it 'enqueues an ApexChatEnrichmentJob' do
        post :create, params: apex_chat_params
        expect(ApexChatEnrichmentJob).to have_enqueued_sidekiq_job([
              data: {
                chatId: '3078079',
                chatAccountId: '3269891',
                chatSummary: 'Hello\nBye',
                chatTextFull: 'Visitor: Bye\nSystem: The chat session has ended.\n',
                provider: 'Apex',
                visitorId: 'baca09be-79d7-46c2-81d3-0e12732f0244',
                country: 'USA'
              },
              eventTime: '2019-03-08 18:29:09',
              eventType: 'Chat',
              platform: 'USA',
              scope: 'CHAT',
              scopeId: '172716',
              subType: 'Lead'
          ])
      end

Any help would be appreciated

theSociableme avatar Jul 30 '20 19:07 theSociableme

Having same issue in rspec-sidekiq 3.1.0. Tried both with have_enqueued_sidekiq_job([...]) and have_enqueued_sidekiq_job(...)

ts-mustaghees avatar May 06 '21 13:05 ts-mustaghees

Same problem using 3.1.0.

expected to have an enqueued ProductWorker::CreateBatch job
         arguments: [[{"tenant_id"=>1, "balance_id"=>1, "user_id"=>1, "readed_codes"=>["23131:1", "21312312:29"]}]]
       found
         arguments: [[{"tenant_id"=>1, "balance_id"=>1, "user_id"=>1, "readed_codes"=>["23131:1", "21312312:29"]}]]

joseneto avatar Nov 03 '21 13:11 joseneto

This could be #162 where (at least) arrays are flattened in the "found arguments" output message, making you believe that the expectation is met, when in reality it isn't.

michaelfranzl avatar Feb 03 '22 07:02 michaelfranzl

I ran into this too. The issue turned out to be that we sneakily had Sidekiq::Testing.inline! enabled in our tests. Disabling that fixed the issue.

danny-pflughoeft avatar Apr 27 '22 20:04 danny-pflughoeft

I ran into the same issue: this is caused only when we use arrays on arguments.

Things we can do to avoid this issue is

  1. expect exactly what u need. don't add [] in the expectation. for example expect(User::RemoveAccess).to have_enqueued_sidekiq_job([user.id,user_account_ids]) here don't use [user.id, user_account_ids] use expect(User::RemoveAccess).to have_enqueued_sidekiq_job(user.id,user_account_ids) it will show the error as the same expectation when u use [ ] as @michaelfranzl mentioned.

as for me, this was the error, and I fixed using the same.

jana-rently avatar Sep 11 '22 10:09 jana-rently

Another interesting issue I ran into, I had some Ruby code adding a Sidekiq job and two of the parameters were empty arrays.

image

Following this to the error message, you can see the use of the splat operator gets rid of them.
image

Now we don't have these job arguments structured well, which would def. save us here, but I wanted to point out something that made me scratch my head until I dived into the gem. If you do have empty arrays going into your parameters somehow, they will get excluded from the Failure message at the end.

I should probably obfuscate that URL, but it's non-production, so whatever, now you know my client...

rob-lane avatar Jun 17 '23 17:06 rob-lane

The failure description definitely needs some work. Under the hood, rspec-sidekiq uses a ContainsExactly so the following are not the same expectations:

SomeJob.perform_async "some_arg", "some_other_arg"

# expect a job with two specific args
expect(SomeJob).to have_enqueued_sidekiq_job("some_arg", "some_other_arg")
# passes

# expect a job with _one_ array arg with two elements
expect(SomeJob).to have_enqueued_sidekiq_job(["some_arg", "some_other_arg"])
# fails

But the description, as pointed out, makes it a little hard to see that fact. I'll see if in V4 we can clean that up to make it more clear that a single array arg was expected and that was our failure

wspurgin avatar Jul 26 '23 13:07 wspurgin