mardi 18 juin 2019

Testing an abstracted if conditional

I'm trying to figure out the best way to test find_communities here without resorting to using polymorphism here to defeat the if statement staring at me.

class CommunityFinder
  def initialize(filters={})
    @filters = filters
  end

  def find_communities
     return my_communities if @filters[:my_communities]
     visible_communities
  end

  def my_communities
     # [...]
  end

  def visibile_communities
     # [...]
  end
end

I have both my_communities and visible_communities well tested, but I have concerns about testing find_communities.

  1. I don't want to duplicate the test setup for both my_communities and visible_communities, because there's likely going to be
  2. I would prefer for the class API to contain all 3 public methods because the conditions for find_communities won't ever change.
  3. I'm writing this with the expectation that the class is going to change by someone other than me in the near future, and that there's going to be more methods

Should I:

  1. make find_communities live in the caller
  2. make find_communities be it's own strategy
  3. duplicate the tests into find_communities
  4. pick your own 4th option.

Aucun commentaire:

Enregistrer un commentaire