[rspec-users] problem to mock association proxy

Daniel Area Criações danielvlopes at areacriacoes.com.br
Fri Dec 5 15:26:44 EST 2008


Thanks Ashley, now the tests passed... but I want understand one thing:
This line is only saying that @properties (assigned[:properties]) have some
value ... is that?
assigns[:properties].should == :user_properties

I can change above to assigns[:properties].should_not be_nill and the
semantic value will be the same, right?

Thanks...

Atenciosamente,

Daniel Lopes    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


On Fri, Dec 5, 2008 at 6:16 PM, Ashley Moran
<ashley.moran at patchspace.co.uk>wrote:

>
> 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` `@user = 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 `@user.properties` 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 at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20081205/14f64e67/attachment.html>


More information about the rspec-users mailing list