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.