Stubject (Stubbing the Subject)

You may occasionally see a test case that uses allow or expect on the same object it’s testing. Sam Phippen refers to this antipattern as the stubject code smell, since you’re stubbing methods on the test subject.[102]

For example, consider the following code for a discussion forum that sends users a summary of what’s happened over the past day:

 class​ DailySummary
 def​ send_daily_summary(user_email, todays_messages)
  message_count = todays_messages.count
  thread_count = todays_messages.map { |m| m[​:thread_id​] }.uniq.count
  subject = ​'Your daily message summary'
  body = ​"You missed ​​#{​message_count​}​​ messages "​ \

Get Effective Testing with RSpec 3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.