« Return to Thread: Can not 'assigns' value in View test

Can not 'assigns' value in View test

by Shaker :: Rate this Message:

Reply to Author | View in Thread

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!

 « Return to Thread: Can not 'assigns' value in View test