Some of the hypocrites in the time of the Messenger of Allah ﷺ would stay behind whenever the Prophet ﷺ set out on a military campaign, and they rejoiced at remaining seated at home in opposition to the Messenger of Allah ﷺ. Then, when the Prophet ﷺ returned, they would offer him excuses and swear oaths, wanting to be praised for what they had not done. So this verse was revealed, "Do not think that those who rejoice in what they have done and love to be praised for what they have not done, do not think them safe from the punishment" [Al Imran 3:188].
أن رجالا من المنافقين في عهد رسول الله ﷺ كانوا إذا خرج النبي ﷺ إلى الغزو تخلفوا عنه، وفرحوا بمقعدهم خلاف رسول الله ﷺ، فإذا قدم النبي ﷺ اعتذروا إليه، وحلفوا، وأحبوا أن يحمدوا بما لم يفعلوا، فنزلت: {لا تحسبن الذين يفرحون بما أتوا ويحبون أن يحمدوا بما لم يفعلوا فلا تحسبنهم بمفازة من العذاب}.
Bukhari related it in the tafsir (4567), and Muslim in the qualities of the hypocrites (2777), both of them through the route of Saeed bin Abu Maryam, who said, Muhammad bin Jafar informed us, who said, Zayd bin Aslam narrated to me, from Ata bin Yasar, from Abu Saeed Khudri, who mentioned it.