[rspec-users] Writing controller spec

Amit Kulkarni lists at ruby-forum.com
Tue Oct 27 09:10:20 EDT 2009

Hello everybody,
I had written same scenario using normal code which is as follows :
def valid_attributes
    :body => "test_description",
    :title => "test123",

describe BbPostsController, "POST Create" do
  before(:each) do
    @post = BbPost.new
  context "Admin" do
    fixtures :users, :bb_posts
    @user = User.find_by_login("kiran")
    if @user
     it "should create post" do
      @post = create_post
      @post.should be_valid

 def create_post
  args ||= valid_attributes
  BbPost.create args

The above code is working.Now i want to try my hand on using mocks so i
write some code using the mocks which is as follows:

it "should create post" do
        BbPost.should_receive(:new).with("title" => "Test123" ,"body" =>
"test_description", "abstract" => "test_abstract")
        post :create, :bb_post => { "title" => "Test123" ,"body" =>
"test_description", "abstract" => "test_abstract" }

it is showing me error which is as follows :

Spec::Mocks::MockExpecttionError in 'BbPostsController POST Create admin
should create post'
<BbPost(id: integer, body: text, title: string,(clss)> expected :new
with ({"body"=>"test_description", "title"=>"Test123"}) once, but
received it 0 ti

Please suggest if something is wrong.
I am referring to Rspec book.Also in that they had given seperate method
of save.But like the upper one i want to do in one scenario.Is it
possible?Please clarify

Also i want to know the whether above code which is running fine
actually uses create method of model.I think it does not uses the
"create method of controller".Please suggest what to do in this case.
Posted via http://www.ruby-forum.com/.

More information about the rspec-users mailing list