How to test instance variable is assigned in Rspec?

200 views
Skip to first unread message

Larry Zhao

unread,
Oct 3, 2014, 12:12:08 AM10/3/14
to ruby-...@googlegroups.com
Hi, Guys,

When I test Grape API with Rspec in Rails project, how should I test instance variable is assigned? like @current_user ?

Thank you very much.

Best Regards.
Larry

Daniel Doubrovkine

unread,
Oct 3, 2014, 7:21:38 AM10/3/14
to ruby-...@googlegroups.com
I think you cannot reach into the endpoint at execution time. Curious what your scenario is, I can't think of a time when I'd want something like this?

--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

dB. | Moscow - Geneva - Seattle - New York
code.dblock.org - @dblockdotorg - artsy.net - github/dblock

Larry Zhao

unread,
Oct 3, 2014, 12:04:44 PM10/3/14
to ruby-...@googlegroups.com
​The scenario is something like @current_user in Rails when a user browse the website after he logged in. 

It authenticates the user and set @current_user in a `before` method, and @current_user could be used in the following execution of the API, so that the process knows this API call is on behalf of a certain user.

Is it a wrong approach to do this? What's the recommended way?

Best Regards.
Larry

Daniel Doubrovkine

unread,
Oct 3, 2014, 4:18:31 PM10/3/14
to ruby-...@googlegroups.com
IMHO I think @variable is an unfriendly pattern because it's unclear what its scope is. I always found that really confusing in Rails controllers.

I generally write helpers like this:

module WardenHelper
  def authenticated?(*args)
    warden.authenticated?(*args)
  end

  def current_user(*args)
    warden.user(*args)
  end

  def current_user=(user)
    warden.set_user user
  end

  def warden
    request.env['warden']
  end
end

So you can see how this is delegated to Warden, and warden can be stubbed easily in a test.


--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Larry Zhao

unread,
Oct 12, 2014, 11:49:07 PM10/12/14
to ruby-...@googlegroups.com
Hi, Daniel,

That is great. I will also try to go this way. Thanks a lot.

Actually I not using warden / Devise to authenticate in our API. We use our own approach to do the authentication and set @current_user in a helper method executed before the actual API execution. 

Beside the user authentication, there's other places that I use an instance variable throughout the API process, like we use jbuilder to render json output and use instance variable to pass the model objects to jbuilder templates. I still would like to know if there's a way to test instance variable assignment in RSpec for Grape?

Best Regards.
Larry


--

Cheers ~

Larry

--
You received this message because you are subscribed to a topic in the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ruby-grape/T566JthhrZM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ruby-grape+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages