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

Jonathan Linowes jonathan at parkerhill.com
Fri Feb 27 16:13:29 EST 2009


if you moved the code that defines crits into a method then you could  
stub the method

crits = define_crits

then
controller.stub!(:define_crits).and_return( { :name => 'Steve' })


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 :
>
> @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
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

____________________________________________________________
Click here for great quotes from top international movers!
http://thirdpartyoffers.netzero.net/TGL2241/fc/BLSrjpYZJC3f6oCp59jmvk73w3RQPFmDYOsliJp8Zb9RMPdh9WHBfJtdXTC/


More information about the rspec-users mailing list