[rspec-users] How do you mock a local variable (#2) ?
Scott Taylor
scott at railsnewbie.com
Fri Feb 27 16:24:08 EST 2009
On Feb 27, 2009, at 3:35 PM, MathLef wrote:
> Hi,
>
> I need help to spec a local variable in a controller. I have been
> wandering how I can do that since all I have done so far is useless.
> Note that the code is in a controller and that it is not yet finished
> since I am trying to spec it as I develop it. Here is what the code
> should look like when it is finished :
>
> ...
> crits = {}
> params.each {
> |key, val|
> crits[key.to_sym] = StringUtils.remove_chars(val).upcase if not
> (cle == "controller" || cle == "action")
> }
>
> if crits[:name].nil?
> add_where(where_clause, crits[:name])
> end
> ...
>
> I am trying to mock the crits local variable. Here is what the rspec
> looks like so far :
Don't try to spec local variables (in fact, ruby doesn't even give you
a way, AFAIK).
You are better off testing this thing in a black box fashion by
passing in different params to the action and seeing the result on the
query.
Scott
>
>
> @mock_crits = mock(Hash, { :name => 'Steve' })
> assigns(:crits) = @mock_crits
Only use assigns to see the results of setting an ivar (in rails).
setting assigns[:foo] before the action won't do you much good, I'm
afraid.
>
>
> First, I can not seem to have control on the params array. How can I
> mock this array ? Also, I also can not seem to have control on the
Don't mock it, just pass it in:
get :my_action, :some => :hash
Scott
>
> crits local variable when I try to spec the if crits[:name].nil?. Is
> this a bad way to code ? Can anyone explain this ?
>
> Many thanks in advance.
> Mathieu
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
More information about the rspec-users
mailing list