Hello, I’m trying learn Rspec but having problems to understand when and
how
user mocks and stubs.
I have a properties_controller with to before_filter actions
(check_administrator_role e load_user)… to access index action of
properties_controller I need have a params[:user] and this parameter
will be
used to load ther user in method load_user …
Like bellow:
before_filter :check_administrator_role
before_filter :load_user
GET /properties
def index
@properties = @user.properties
end
private
def load_user
@user = User.find(params[:user_id])
if @user.nil?
flash[:notice] = “Registro não encontrado”
redirect_to root_path
end
end
But I don’t know how test index and load_user without fixtures, take a
look
on my specs:
describe PropertiesController do
def mock_property(stubs={})
@mock_property ||= mock_model(Property, stubs)
end
before do
@current_user = mock_model(User, :id => 1)
controller.stub!(:check_administrator_role).and_return(true)
@user = mock_model(User, :id=>1)
controller.stub!(:load_user).and_return(@user)
end
describe “responding to GET index” do
it "should expose all properties of given user as @properties" do
@user.should_receive(:properties).and_return([mock_property])
get :index
assigns[:properties].should == [mock_property]
end
end
But I having this error:
NoMethodError in ‘PropertiesController responding to GET index should
expose
all properties of given user as @properties’
You have a nil object when you didn’t expect it!
The error occurred while evaluating nil.properties
/Users/daniellopes/Trabalhos/luvima/luvima/app/controllers/properties_controller.rb:8:in
`index’
./spec/controllers/properties_controller_spec.rb:21:
So, how is the right way to mock associated records?
Thanks.
Atenciosamente,
Daniel L.  Area Criações
Design, Websites e Sistemas Web
Visite: http://www.areacriacoes.com.br/projects
http://blog.areacriacoes.com.br/
55 (31) 3077-4560 / 55 (31) 8808-8748 / 55 (31) 8737-7501