I''m having troubles to test methods on controllers, specially this
method:
def update
@property = @user.properties.find(params[:id])
if params[:property][:owner_id].blank?
@owner = Owner.create_from_user(@user)
@property.owner = @owner
end
if @property.update_attributes(params[:property])
flash[:notice] = ''Imóvel atualizado com
sucesso.''
redirect_to user_property_path(@user)
else
render :action => "edit"
end
end
This method is pretty standard, except for the line ==> if
params[:property][:owner_id].blank?
In my spec file I try this:
it "should expose the requested property as @property" do
@property = mock_model(Property, :owner=>:owner, :owner= =>:owner,
:update_attributes => true)
Property.stub!(:find).and_return(@property)
Owner.stub!(:create_from_user)
put :update, :id => "1"
assigns(:property).should equal(@property)
end
But get this error:
The error occurred while evaluating nil.[]
The problem is params and I don''t know how is the best way to simulete
params hash ...
Anyone can help?
Thanks.
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://rubyforge.org/pipermail/rspec-users/attachments/20081212/85aabff9/attachment.html>
"Daniel Lopes" <danielvlopes at gmail.com> writes:> I''m having troubles to test methods on controllers, specially this method: > > def update > @property = @user.properties.find(params[:id]) > > if params[:property][:owner_id].blank? > @owner = Owner.create_from_user(@user) > @property.owner = @owner > end > > if @property.update_attributes(params[:property]) > flash[:notice] = ''Imóvel atualizado com sucesso.'' > redirect_to user_property_path(@user) > else > render :action => "edit" > end > end > > This method is pretty standard, except for the line ==> if params[:property][:owner_id].blank? > > In my spec file I try this: > > it "should expose the requested property as @property" do > @property = mock_model(Property, :owner=>:owner, :owner= =>:owner, :update_attributes => true) > Property.stub!(:find).and_return(@property) > Owner.stub!(:create_from_user) > > put :update, :id => "1" > assigns(:property).should equal(@property) > end > > But get this error: > The error occurred while evaluating nil.[] > > The problem is params and I don''t know how is the best way to simulete params hash ...Hi, you need to pass the param in the put: put :update, :id => "11", :property => {:owner_id => "123"} and when you want to check for the blank one, you just have to pass in an empty property hash: put :update, :id => "11", :property => {} This way params[:property][:owner_id] will return nil. Pat
Thanks, now it''s looks obivious. :D
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 12, 2008 at 6:16 PM, Pat Maddox <pergesu at gmail.com> wrote:
> "Daniel Lopes" <danielvlopes at gmail.com> writes:
>
> > I''m having troubles to test methods on controllers, specially
this
> method:
> >
> > def update
> > @property = @user.properties.find(params[:id])
> >
> > if params[:property][:owner_id].blank?
> > @owner = Owner.create_from_user(@user)
> > @property.owner = @owner
> > end
> >
> > if @property.update_attributes(params[:property])
> > flash[:notice] = ''Imóvel atualizado com
sucesso.''
> > redirect_to user_property_path(@user)
> > else
> > render :action => "edit"
> > end
> > end
> >
> > This method is pretty standard, except for the line ==> if
> params[:property][:owner_id].blank?
> >
> > In my spec file I try this:
> >
> > it "should expose the requested property as @property"
do
> > @property = mock_model(Property, :owner=>:owner, :owner>
=>:owner, :update_attributes => true)
> > Property.stub!(:find).and_return(@property)
> > Owner.stub!(:create_from_user)
> >
> > put :update, :id => "1"
> > assigns(:property).should equal(@property)
> > end
> >
> > But get this error:
> > The error occurred while evaluating nil.[]
> >
> > The problem is params and I don''t know how is the best way to
simulete
> params hash ...
>
> Hi, you need to pass the param in the put:
>
> put :update, :id => "11", :property => {:owner_id =>
"123"}
>
> and when you want to check for the blank one, you just have to pass in
> an empty property hash:
>
> put :update, :id => "11", :property => {}
>
> This way params[:property][:owner_id] will return nil.
>
> Pat
> _______________________________________________
> 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/20081212/1c596200/attachment.html>