문제

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

도움이 되었습니까?

해결책 2

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

다른 팁

test_user is a local variable and is never cached. The use of ||= there does nothing.

You need to cache the result in another instance variable, @test_user ||= ..., or do the following:

def facebook_user
  @facebook_user ||= begin
    test_user = Koala::Facebook::TestUsers.new(app_id: ENV['FACEBOOK_APP_ID'], secret: ENV['FACEBOOK_SECRET'])
    test_user.create(true, 'email')
  end
end
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top