[rspec-users] Can not 'assigns' value in View test

David Chelimsky dchelimsky at gmail.com
Wed Sep 12 21:14:05 EDT 2007


On 9/12/07, Shaker <flyeminent at hotmail.com> wrote:
>
> Hello, David:
>   Thank you very much for your help. Now it works!
>   I would like to recall a message posted several days ago regarding the
> "paginate". How to test "paginate" in controller spec? One simple example
> may be like this:
>   hash[:accounts_pages], hash[:accounts] = paginate :accounts(the table),
> #order and conditions follows
> How should I test this code in controller spec? Or can I mock this line,
> like should_receive(:paginate).and_return(...)? But the problem is
> 'paginate' returns two values. :(

When a method returns 2 values it is really returning an array, so ...

   and_return([val1, val2])

See if that works.

>   Thanks for investigating this question.
>
> David Chelimsky-2 wrote:
> >
> > On 9/12/07, Shaker <flyeminent at hotmail.com> wrote:
> >>
> >> I also feel puzzled. But that's what my colleagues do, and it works. The
> >> thing is they use
> >>   render :partial=>'index', :locals=>{:hash=>hash}
> >> in the controller to pass the hash data to the partial it will
> >> render(index
> >> in this case). And later they simply use <%= hash %> to get the hash data
> >> passed from the controller.
> >
> > Now THAT is a horse of a different color. OK - first:
> >
> >   render :partial=>'index', :locals=>{:hash=>hash}
> >
> > Here, the symbol :hash is just a key - it is not the object that's
> > been passed around.
> >
> > Second:
> >
> >   <%= hash %>
> >
> > I'm assuming that's in the partial named index (a partial named
> > index???????, now THAT's not at all confusing). In this case, the
> > :locals => {:hash=>hash} in the calling template assigns the hash
> > object (in the calling template's scope) as the return value of a
> > method in the partial named .... (drum roll, please) .... hash. Like
> > hash().
> >
> > So - what you want to do is to stub the hash method on the template:
> >
> >
> > template.stub!(:hash).and_return(the_hash_I_want_to_stick_in_the_template)
> >
> > That all make sense?
> >
> > Good luck!
> >
> >> Maybe the :locals does some magical work here. I
> >> am quite familiar with rspec and ruby. So I am reading more documentation
> >> now.
> >> Hopefully we could figure it out shortly.
> >>
> >> David Chelimsky-2 wrote:
> >> >
> >> > On 9/12/07, Shaker <flyeminent at hotmail.com> wrote:
> >> >>
> >> >> Unfortunately, I am not supposed to change the code inside the
> >> partial.
> >> >> That's why I posted this message. I am trying to find a way of
> >> tackling
> >> >> this
> >> >> problem. Because I am going to test tens of hundreds of partials, most
> >> of
> >> >> which are using the symbols rather than instance variable.
> >> >
> >> > Huh? Symbols in the partials that are supposed to magically transform
> >> > to some value? I don't get it. What am I missing?
> >> >
> >> >>
> >> >> David Chelimsky-2 wrote:
> >> >> >
> >> >> > On 9/12/07, Evan David Light <evan at tiggerpalace.com> wrote:
> >> >> >>         Granted that I'm new too; however, assigns[:hash] creates a
> >> >> >> member
> >> >> >> variable @hash that is made available to the view.  Maybe that's
> >> the
> >> >> >> problem?  Modify your view to use @hash instead.
> >> >> >
> >> >> > http://rspec.rubyforge.org/documentation/rails/writing/views.html
> >> >> >
> >> >> >>
> >> >> >> On Sep 12, 2007, at 5:20 AM, Shaker wrote:
> >> >> >>
> >> >> >> >
> >> >> >> > Hello everyone:
> >> >> >> >   I am quit new to View test using rspec. I want to write spec
> >> for a
> >> >> >> > partial, which is rendered by a controller. However, the
> >> controller
> >> >> >> > does not
> >> >> >> > pass data to the partial using instance variables, it uses symbol
> >> >> >> > variable
> >> >> >> > instead. I read the example of view spec provided in the website.
> >> >> >> > But the
> >> >> >> > example is using instance variable. So I am looking for a way of
> >> >> >> > assigning
> >> >> >> > my test data to the partial to be tested. I would like to show an
> >> >> >> > my code
> >> >> >> > below:
> >> >> >> > class MyController
> >> >> >> >   def index
> >> >> >> >     hash = {...#data provided here}
> >> >> >> >     render :partial=>'index', :layout=>false,
> >> :locals=>{:hash=>hash}
> >> >> >> >   end
> >> >> >> > end
> >> >> >> >
> >> >> >> > _index.rhtml
> >> >> >> >   ......#unrelated code omitted
> >> >> >> >   <script>
> >> >> >> >     var data = <%= hash %>;
> >> >> >> >     <!-- process data here -->
> >> >> >> >   </script>
> >> >> >> >
> >> >> >> > In my spec, I used assigns[:hash] = my_own_data, but an error
> >> >> >> > occurred,
> >> >> >> > saying that "undefined local variable or method 'hash' ".
> >> >> >> > How can I assign my own data to hash in my view spec?
> >> >> >> > Thank you!
> >> >> >> >
> >> >> >> > --
> >> >> >> > View this message in context: http://www.nabble.com/Can-not-%
> >> >> >> > 27assigns%27-value-in-View-test-tf4428018.html#a12631788
> >> >> >> > Sent from the rspec-users mailing list archive at Nabble.com.
> >> >> >> >
> >> >> >> > _______________________________________________
> >> >> >> > rspec-users mailing list
> >> >> >> > rspec-users at rubyforge.org
> >> >> >> > http://rubyforge.org/mailman/listinfo/rspec-users
> >> >> >>
> >> >> >> _______________________________________________
> >> >> >> rspec-users mailing list
> >> >> >> rspec-users at rubyforge.org
> >> >> >> http://rubyforge.org/mailman/listinfo/rspec-users
> >> >> >>
> >> >> > _______________________________________________
> >> >> > rspec-users mailing list
> >> >> > rspec-users at rubyforge.org
> >> >> > http://rubyforge.org/mailman/listinfo/rspec-users
> >> >> >
> >> >> >
> >> >>
> >> >> --
> >> >> View this message in context:
> >> >>
> >> http://www.nabble.com/Can-not-%27assigns%27-value-in-View-test-tf4428018.html#a12646459
> >> >> Sent from the rspec-users mailing list archive at Nabble.com.
> >> >>
> >> >> _______________________________________________
> >> >> rspec-users mailing list
> >> >> rspec-users at rubyforge.org
> >> >> http://rubyforge.org/mailman/listinfo/rspec-users
> >> >>
> >> > _______________________________________________
> >> > rspec-users mailing list
> >> > rspec-users at rubyforge.org
> >> > http://rubyforge.org/mailman/listinfo/rspec-users
> >> >
> >> >
> >>
> >> --
> >> View this message in context:
> >> http://www.nabble.com/Can-not-%27assigns%27-value-in-View-test-tf4428018.html#a12646774
> >> Sent from the rspec-users mailing list archive at Nabble.com.
> >>
> >> _______________________________________________
> >> rspec-users mailing list
> >> rspec-users at rubyforge.org
> >> http://rubyforge.org/mailman/listinfo/rspec-users
> >>
> > _______________________________________________
> > rspec-users mailing list
> > rspec-users at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/rspec-users
> >
> >
>
> --
> View this message in context: http://www.nabble.com/Can-not-%27assigns%27-value-in-View-test-tf4428018.html#a12646978
> Sent from the rspec-users mailing list archive at Nabble.com.
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list