[rspec-users] problem with should_receive in controller spec

David Chelimsky dchelimsky at gmail.com
Thu Oct 29 09:27:04 EDT 2009


On Oct 29, 2009, at 5:56 AM, Amit Kulkarni wrote:

> 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 )

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.

> <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
> )"

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

>



More information about the rspec-users mailing list