Search code examples
ruby-on-railstestingcucumberkoalabefore-filter

running code once before cucumber runs its tests


I'm testing facebook integration for my rails app. I want to create a test user once using Koala's API, and then run my integration tests.

I wrote this method (in koala.rb in features/support):

def facebook_user
    test_user ||= Koala::Facebook::TestUsers.new(app_id: ENV['FACEBOOK_APP_ID'], secret: ENV['FACEBOOK_SECRET'])
@facebook_user ||= test_user.create(true, 'email')
end

And I want it to run only once. Right now, everytime I call the facebook_user method anywhere in my tests, it runs the code again, instead of keeping the value it retrieved before.

How do I run this code once and retain the values for all of my scenarios?

Thanks


Solution

  • Used a global variable: on any file in the support folder:

    Before do
        if !$dunit
            $facebook_user = Koala::Facebook::TestUsers.new(app_id: ENV['FACEBOOK_APP_ID'], secret: ENV['FACEBOOK_SECRET']).create(true, 'email')
        $dunit = true  
    end
    

    end

    and then call $facebook_user at will