How can I test my controller via rspec for something other than response.should be_success?

20 views
Skip to first unread message

Chris P.

unread,
Jun 9, 2014, 2:59:21 PM6/9/14
to rubyonra...@googlegroups.com
1) Right now this is just testing a successful response. However, since
I've gone and created a Post via FactoryGirl... how can I test the
@post.id equals one of the id's being returned by the get: index json?


describe Api::V1::PostsController do

context 'Post' do
before(:each) do
@post = FactoryGirl.create(:post)
end


context '#index' do
it "should have a successful response on get index" do
get :index, format: :json
response.should be_success
end
end

EDIT:

2) How can I use "post :create" when my route is nested.

For example... this works great as an rspec controller post :create with
a top level resource, such as post

it "should get a success response on post create" do
post :create, params
response.should be_success
end

however, for a nested resource like 'comment' which is nested under
post... the above would throw the following...

Failure/Error: post :create, params
ActionController::UrlGenerationError:
No route matches

and if I tried

it "should get a success response on post create" do
post :create, post_id: @post.id, params #@post is defined and
created above
response.should be_success
end

I get this odd error...

syntax error, unexpected '\n', expecting => (SyntaxError)

--
Posted via http://www.ruby-forum.com/.

dasibre

unread,
Jun 10, 2014, 12:54:56 PM6/10/14
to rubyonra...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages