On 5 Dec 2008, at 13:42, Daniel Lopes wrote:
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?
Hi Daniel
The error you are seeing is because the assignment to @user happens
inside #load_user: the live code never call` [EMAIL PROTECTED] = load_user`. As
far as possible, you want to avoid modifying objects under test -
private methods are only the business of instances of that class.
It's much safer in this case to do
User.should_receive(:find).and_return(@user)
That way, the filter will run as normal.
As for the association - it's actually irrelevant what that returns.
All you care is that whatever comes out of [EMAIL PROTECTED] is
assingned to the view. I tend to use symbols to represent objects
(including arrays) that get passed around. For example:
describe PropertiesController do
# Not needed
# def mock_property(stubs={})
# @mock_property ||= mock_model(Property, stubs)
# end
before do
controller.stub!(:check_administrator_role).and_return(true)
@user = mock_model(User, :id=>1, :properties => :user_properties)
User.stub!(:find).and_return(@user)
end
describe "responding to GET index" do
it "should expose all properties of given user as @properties" do
get :index
assigns[:properties].should == :user_properties
end
end
end
HTH
Ashley
--
http://www.patchspace.co.uk/
http://aviewfromafar.net/
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users