[rspec-users] problem with should_receive in controller spec

Amit Kulkarni lists at ruby-forum.com
Thu Oct 29 06:56:45 EDT 2009

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 }
        format.html { render :action => "new" }
  format.xml  { render :xml => @bb_post.errors, :status =>
:unprocessable_entity }

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

      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
        @post.should redirect_to( blog_bb_posts_url )
        post :create

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,
ture_id: integer, bb_post_category_id: integer, created_at: datetime,
: datetime, abstract: text, view_count: integer, user_id: integer,
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' =>

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/.

More information about the rspec-users mailing list