Forum: Ruby on Rails How can I test my controller via rspec for something other than response.should be_success?

4da8f502012ef8003af59bf5f4ddad0c?d=identicon&s=25 Chris P. (chris_p)
on 2014-06-09 20:58
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)
7f959297e473789a12397cc20cd3996c?d=identicon&s=25 dasibre (Guest)
on 2014-06-10 18:56
(Received via mailing list)
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.