Friday, 15 July 2011

ruby on rails - Stubbing instance method for OpenTok object -



ruby on rails - Stubbing instance method for OpenTok object -

the gem i'm using integrate opentok in rails application at: https://github.com/opentok/opentok-ruby-sdk. based core of application on example: http://www.tokbox.com/blog/building-a-video-party-app-with-ruby-on-rails.

in relevant part of code, i'm creating @opentok object in 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 session next code:

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

the problem is, create_session seems throw error

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

whenever run rspec tests without net connection. i'd stub method returns hash {:sessionid => 1}. i'm having problem figuring out how stub method. can't stub opentok module or opentok::opentoksdk class. how go stubbing create_session method?

here's i've been doing works:

first, tend initialize opentok object when app loads i'm not creating opentok object on every request. this, create ruby file (apis.rb) in config/initializers folder.

my apis.rb looks this:

tb_key = env['tb_key'] tb_secret = env['tb_secret'] otsdk = opentok::opentoksdk.new tb_key, tb_secret

in controller, generate session i'll phone call otsdk.createsession, similar have.

to test rspec, can write in test file:

otsdk.stub(:createsession).and_return( {:sessionid => "1mx_2a3453095j0tj30..."} )

if run rspec wifi turned off calling createsession should no longer throw error.

here's documentation rspec stubbing: http://rubydoc.info/gems/rspec-mocks/frames

good luck!

ruby-on-rails rspec stub

No comments:

Post a Comment