[rspec-users] How do you mock a local variable (#2) ?

MathLef mlefrancois19 at gmail.com
Fri Feb 27 15:35:42 EST 2009


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 :

@mock_crits = mock(Hash, { :name => 'Steve' })
assigns(:crits) = @mock_crits

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
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


More information about the rspec-users mailing list