[rspec-users] problem with should_receive in controller spec

15 views
Skip to first unread message

Amit Kulkarni

unread,
Oct 29, 2009, 6:56:45 AM10/29/09
to rspec...@rubyforge.org
I am trying to write controller spec but i am getting some problem.
Following is the controller code:

def create
@bb_post = @feature.posts.new( params[ :bb_post ] )
if @bb_post.save
flash[ :notice ] = 'Blog post was successfully created.'
format.html { redirect_to( blog_bb_posts_url ) }
format.xml { render :xml => @bb_post, :status => :created,
:location => @bb_post }
else
format.html { render :action => "new" }
format.xml { render :xml => @bb_post.errors, :status =>
:unprocessable_entity }
end
end
end

My controller spec is :
describe BbPostsController, "POST Create" do
context "Admin" do
fixtures :users, :bb_posts

it "should create post" do
@post = mock_model(BbPost, :save => nil)
BbPost.should_receive(:new).with('title' => 'Test123' ,'body' =>
'test_description', 'abstract' => 'test_abstract').and_return(@post)
post :create
end

it "should save post" do
@post = mock_model( BbPost, :body => "test_description", :title
=> "test123", :abstract => "test_abstract" )
BbPost.should_receive( :new ).and_return @post
#~ @post.should_receive( :redirect_to
).with(blog_bb_posts_url).and_return(true)
@post.should redirect_to( blog_bb_posts_url )
post :create
end
end
end

Now it gives me error

Spec::Mocks::MockExpectationError in 'BbPostsController POST Create
Admin should
not create post without login'
<BbPost(id: integer, body: text, title: string, comment_count: integer,
tags: st
ring, published: boolean, inappropriate: boolean, permalink: string,
channel_fea
ture_id: integer, bb_post_category_id: integer, created_at: datetime,
updated_at
: datetime, abstract: text, view_count: integer, user_id: integer,
unsoliciteds_
count: integer, textilized: text, delta: boolean) (class)> expected :new
with ({
"abstract"=>"test_abstract", "body"=>"test_description",
"title"=>"Test123"}) on
ce, but received it 0 times

And it is pointing error at "BbPost.should_receive(:new).with('title' =>
'Test123' ,'body' => 'test_description', 'abstract' =>
'test_abstract').and_return(@post)"


and for second it gives error as
NoMethodError in 'BbPostsController POST Create Admin should save post'
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.rewrite
(eval):15:in `edit_profile_url'
And it is pointing error at "@post.should redirect_to( edit_profile_url
)"


Please suggest something on this.I am not able to figure it out.
--
Posted via http://www.ruby-forum.com/.
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

David Chelimsky

unread,
Oct 29, 2009, 9:27:04 AM10/29/09
to rspec...@rubyforge.org

This should be:

response.should redirect_to( blog_bb_posts_url )

> post :create
> end
> end
> end
>
> Now it gives me error
>
> Spec::Mocks::MockExpectationError in 'BbPostsController POST Create
> Admin should
> not create post without login'

This name, "should not create post without login," is not in the code
above, so we have no way to diagnose this error. Please be sure to
post matching code and failure message.

This is the one I explained above - the spec says "@post.should ..."
instead of "response.should ..."

> Please suggest something on this.I am not able to figure it out.

It's pretty hard to read the code in an email without any syntax
highlighting and the email formatting things in unpleasant ways. Even
if you include the code in email for posterity, please also post it to http://gist.github.com
or http://pastie.org/ or some such in future posts.

Thanks,
David

Reply all
Reply to author
Forward
0 new messages