Question

I have the following test setup which I'm now finding necessary to use in multiple test suites.

  before do
    @guest, @host = FactoryGirl.create(:user), FactoryGirl.create(:user)

    @booking = FactoryGirl.create(
      :booking,
      user: @guest,
      listing: FactoryGirl.create(:listing, user: @host),
      state: "unaccepted_by_guest_or_host"
    )   
  end

It's obviously not ideal to have this same exact code in multiple files. What I think I would prefer is something more like this if possible:

  before do
    @guest, @host = FactoryGirl.create(:user), FactoryGirl.create(:user)

    @booking = FactoryGirl.create(
      :complete_booking,
      guest: @guest,
      host: @host,
    )   
  end

That would of course still be a little duplication, but I wouldn't feel nearly as bad about that.

Is it possible to achieve what I'm trying to achieve? Is something even better possible?

Was it helpful?

Solution

You could always define a test helper that given a @guest and a @host, it sets up the thing as you listed it in your first snippet. Same benefits, none of the duplication.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top