My peers and I are trying to take our testing and object design to the next level, where we’re more concerned with messages than specific objects. For the sake of this question, let’s say we have users and comments models. User has many comments.
class User < ActiveRecord::Base def update_related_statuses comments.each do |comment| comment.update_status end end end
And the corresponding test
describe User do describe '#update_related_statuses' do it 'calls update_status on all related comments' do # A number of FactoryGirl.create()s omitted for brevity expect_any_instance_of(Comment).to receive(:update_status).once user.update_related_statuses end end end
And that started a little debate among my group because there was a faction that argued for adding lines like this to the spec
describe User do describe '#update_related_statuses' do it 'calls update_status on all related posts' do comment = double 'comment' user.stub(:comments).and_return [comment] expect(comment).to receive(:update_status).once user.update_related_statuses end end end
And one of my peers expressed discomfort with that approach because he argued that the test knew too much about the implementation, which… I can’t necessarily disagree with in principle. So, putting aside the obvious rebuttals of “well, how else would you implement this method,” what do you all think about these alternative test approaches? And am I really just opening a debate about white- vs black-box testing?