Writing controller spec

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

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

def create_post
args ||= valid_attributes
BbPost.create args
end
end

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” }
end

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
mes

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.
:slight_smile:

Amit,

Your controller spec that you said was working was not actually testing
your
controller since you were not calling any of your controller methods.

I’d suggest you create some code from rspec_scaffold on your rails
project
and just check out the code that’s generated for you.

ruby script/generate rspec_scaffold BpPost title:string body:text

Carlos Souza wrote:

Amit,

Your controller spec that you said was working was not actually testing
your
controller since you were not calling any of your controller methods.

Ok.In that case can you suggest how to call controller methods from the
uppermost code which is running fine and how to pass parameters.

I’d suggest you create some code from rspec_scaffold on your rails
project
and just check out the code that’s generated for you.

ruby script/generate rspec_scaffold BpPost title:string body:text

I will check this out…

Also any suggestions regarding the bottom code…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs