Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RoR and RSpec: How to access controller instance variables without defining accessors?

Tags:

I'm writing a rspec tests for my controller and i cannot find solution following problem. For one of the edge case tests I need to verify the value of one instance variable. How can i access it without having to define the accessor? By default the usual:

controller.variable.should == '3.15' 

doesn't work.

Defining

attr_reader :variable

just to make the tests pass would be silly and i'm sure that there is a more inteligent way.

like image 210
Jakub Troszok Avatar asked Jul 16 '09 13:07

Jakub Troszok


1 Answers

controller.instance_variable_get(:var)

if you find yourself doing this, you might want to rethink your approach to information hiding so that you are testing essential behavior rather than incidental implementation details. Your tests should ensure that the "thing" functions as it should without being too tightly bound to the particular implementation.

EDIT: Isn't assigns(:var) the rails testing magic for doing the same thing with controllers?

like image 168
Ben Hughes Avatar answered Oct 11 '22 18:10

Ben Hughes