0

The gem I'm using to integrate OpenTok in my Rails application is at: https://github.com/opentok/Opentok-Ruby-SDK. I based the core of the application on this example: http://www.tokbox.com/blog/building-a-video-party-app-with-ruby-on-rails.

In the relevant part of code, I'm creating an @opentok object in the config_opentok method:

    def config_opentok
        if @api_key.nil? or @api_secret.nil?
            if Rails.env.development?
                @api_key = API_KEY
                @api_secret = API_SECRET
            else
                @api_key = ENV['API_KEY']
                @api_secret = ENV['API_SECRET']
            end
        end
        if @opentok.nil?
            @opentok = OpenTok::OpenTokSDK.new(@api_key, @api_secret)
        end
    end

And I'm creating a session with the following code:

    config_opentok

    if Rails.env.development?
        session = @opentok.create_session('localhost')
    else
        session = @opentok.create_session(request.remote_addr)
    end

The trouble is, the create_session seems to throw an error

SocketError: getaddrinfo: nodename nor servname provided, or not known

whenever I run my Rspec tests without an internet connection. So I'd like to stub that method so that it returns just a hash {:sessionId => 1}. But I'm having trouble figuring out how to stub the method. I can't just stub the OpenTok module or the OpenTok::OpenTokSDK class. How would I go about stubbing the create_session method?

tanookiben
  • 22,575
  • 8
  • 27
  • 25

2 Answers2

0

here's what I've been doing that works:

First, what I tend to do is to initialize the OpenTok object when the app loads so I'm not creating an OpenTok object on every request. To do this, I create a ruby file (apis.rb) in my config/initializers folder.

My apis.rb looks like this:

TB_KEY = ENV['TB_KEY']
TB_SECRET = ENV['TB_SECRET']
OTSDK = OpenTok::OpenTokSDK.new TB_KEY, TB_SECRET

In my controller, to generate a session I'll simply call OTSDK.createSession, similar to what you already have.

To test with rspec, you can simply write in your test file:

OTSDK.stub(:createSession).and_return( {:sessionId => "1MX_2A3453095J0TJ30..."} )

If you run rspec with wifi turned off calling createSession should no longer throw an error.

Here's the documentation for rspec stubbing: http://rubydoc.info/gems/rspec-mocks/frames

Good Luck!

songz
  • 2,082
  • 1
  • 14
  • 18
0

The trouble is, the create_session seems to throw an error whenever I run my Rspec tests without an internet connection.

Instead of attempting to stub, why not give your tests a mock internet connection with VCR?

After initial set up, VCR lets you run all of your tests as if you were actively connected to the internet. This allows you to run tests offline, speeds up all the tests that needed an active connection, and gives you a consistent set of results.

If you have a subscription to RailsCasts, Ryan made a video about VCR in episode 291

clang1234
  • 1,704
  • 2
  • 16
  • 28