ruby on rails 3 - Anyway to stub local variables in an rspec test? -


i have code looks in method trying test:

  service = if user_activator?                 someservice.new(subscription.user, cookies: cookies)              elsif sub_activator?                  someotherservice.build(subscription: subscription, cookies: cookies)             end 

i trying stub service instance of someservice , try see if receives method code looks this:

expect(some_service_instance).to receive(:activate).with(hash_including(           app_context: app_context,           cookies: cookies,           subscription: subscription         )) 

but there way stub local variable instance of useractivationservice??

here ways think may work?

first, should extract conditional local variable set method (this feels fishy):

def service     if some_service_activator?       someservice.new(subscription.user, cookies: cookies)     elsif sub_activator?       someotherservice.build(subscription: subscription, cookies: cookies)     end   end 

then can do:

let(:some_service_instance) { someservice.new(subscription.user, cookies: cookies) } allow(<something returns instance of class testing>).to receive(:service) { some_service_instance } 

then

  expect(some_service_instance).to receive(:activate).with(hash_including(           app_context: app_context,           cookies: cookies,           subscription: subscription         )) 

is kosher?

assuming taking place inside controller think want use @controller variable.

let(:user_activation_service_instance) { useractivationservice.new(subscription.user, cookies: cookies) } allow(@controller).to receive(:service) { user_activation_service_instance } 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -