On 6.4.2008, at 14.20, roberto belardo wrote:

Hi all,
here i am again with this stupid problem in rspecing
this controller action:

------------------------------------
CommentsController
def create
 @comment = Comment.new(params[:comment])
 @user = User.find(params[:user_id])

[16]  @comment.author = @user

 @comment.save
end

------------------------------------
CommentsController Spec
describe CommentsController, "handling POST /comments"
do

 before(:each) do
   @user = mock_model(User, :to_param => "2")
   @comment = mock_model(Comment, :to_param => "1",
:author => @user)


User.should_receive(:find).at_least(1).times.and_return(@user)

   Comment.stub!(:new).and_return(@comment)

 end

 def post_with_successful_save
   @comment.should_receive(:save).and_return(true)

[34]post :create, :user_id => @user_id, :comment => {}

 end

 it "should create a new comment" do
   Comment.should_receive(:new).with({
}).and_return(@comment)
[47]post_with_successful_save
        @comment.author.should be(@user)
 end

end

The frustrating error Rspec gave to me is this:

Spec::Mocks::MockExpectationError in
'CommentsController handling POST /comments should
create a new comment'
Mock 'Comment_1002' received unexpected message
:author= with (#<User:0x..fdb9c1a8e
@name="User_1000">)
comments_controller.rb:16:in `create'
comments_controller_spec.rb:34:in
`post_with_successful_save'
comments_controller_spec.rb:47:


I have tried to google for something like this but i
resolved nothing. Could someone help me understanding
my fault?

Short answer: you haven't stubbed the author= method for the comment mock, only author.

A bit longer answer: Are you sure you can trust the user to only submit valid user ids? With your current code, anyone can change the user_id parameter in the call and fake any user id she wants. This might be intentional (if there are only admin users in your app), just pointing it out. You can also simplify your action by just saying @user.comments.create(params[:comment]) once you have the user fetched.

Cheers,
//jarkko


Thanks in advance,
Roberto.


     Inviato da Yahoo! Mail.
La casella di posta intelligente.
http://it.docs.yahoo.com/mail/overview/index.html
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to