require 'rails_helper' RSpec.describe Api::PostsController, type: :controller do describe "update" do describe 'unauthorized' do it 'redirects to login page' do patch :update, params: {id: 9} response.redirect_url.should end_with('/users/sign_in') end end describe 'authorized' do before { sign_in create :user } it "includes associations if configured" do expect( subject ).to receive(:update_serialize_options).and_return(include: 'comments') post1 = create :post, title: 'post-1', body: 'post-body-1' comment = create :comment, post: post1, body: 'post-1-comment-1' patch :update, params: {id: post1.id, post: {title: 'post-1-updated', body: 'post-body-1-updated'}} assert_json( data: { id: post1.id.to_s, type: "post", attributes: {"title"=>"post-1", "body"=>"post-body-1-updated"}, # title not permitted, could be boolean admin relationships: { comments: {data: [{id: comment.id.to_s , type: "comment" }]} } }, included: [ { id: comment.id.to_s, type: "comment", attributes: {body: "post-1-comment-1"}, relationships: { post: {data: {id: post1.id.to_s, type: "posts"}} } } ] ) end end end end