abstract_notifier icon indicating copy to clipboard operation
abstract_notifier copied to clipboard

have_enqueued_notification fails when 2 notifications with same message are enqueued

Open kleber-gueriero opened this issue 6 years ago • 1 comments

When testing a method that calls 2 different notification methods which sends exactly same message, the expect with have_enqueued_notification fails, saying that it wasn't enqueued not even once:

expected to enqueue notification: {:body=>"Here we go again"} exactly once, but haven't enqueued anything

I didn't test similar cases, like enqueue twice w/ same method, but at least the problem exists for this specific case.

kleber-gueriero avatar Jun 05 '19 01:06 kleber-gueriero

Hey @kleber-gueriero!

Could you please provide a code example and a test example for this exception?'

P.S. Sorry for such a late response.

palkan avatar Aug 24 '19 01:08 palkan

Edit

nvm, I didn't realize that you can add .twice 🙏

What I originally wrote I can see a similar issue with `have_sent_notification` matcher as well.

I have two callbacks where each of them create notifications.

Basically, when I have a test like

the test

RSpec.describe Post, type: :model do
  describe 'notifications' do
      context 'when new post is published' do
        it { expect { create(:post) }.to have_sent_notification }
      end
  end
end

example model

class Post < ActiveRecord

  after_create :notify_can_see_posts
  after_create :notify_new_post

  def notify_can_see_posts
    FromAdminNotifier.send_can_see_posts.notify_now 
  end

  def notify_new_post
    NewPostNotifier.created(self).notify_now
  end
end

example notifiers

# frozen_string_literal: true

class NewPostNotifier < ApplicationNotifier
  def created(post)
    notification(
      title: 'New post!',
      body: "#{post.title}",
    )
  end
end

class FromAdminNotifier < ApplicationNotifier
  def send_can_see_posts()
    notification(
      title: 'Thanks for your post!',
      body: 'Thank you for your submission. Now you can see what others have posted on the platform!',
    )
  end
end

So creating a post sends two notifications. One to the user who have submitted to tell them that they can now see others post (we have some exclusive thread page like that), and another to rest of the users who have already submitted a post to tell them that there's a new post in the app.

When one action sends two or more notifications, the matcher fails.

SuzukiRyuichiro avatar Jul 10 '23 18:07 SuzukiRyuichiro