ruby on rails - How to write rspec test to check if post returned by index are owned by current user -
i write test index method takes account signed in user , checks if index method shows posts belonging user.
notes_controller_spec.rb
describe "get #index" "responds http 200 status code" :index expect(response).to be_success expect(response).to have_http_status(200) end "renders index template" :index expect(response).to render_template("index") end "returns posts user" end
notes_controller.rb
class notescontroller < applicationcontroller before_action :set_note, only: [:show, :edit, :update, :destroy] before_action :authenticate_user! # /notes # /notes.json def index @users = user.order(:created_at) rails.logger.info("#{current_user.id == @users[0].id}") if current_user.equal? @users[0] @notes = note.all else @notes = note.where(user: current_user) end respond_to |format| format.nil? { } format.html { } format.xml { @users = user.order(:created_at) if @users.first == current_user render xml: @notes else return head(:unauthorized) end } end end
end
scheme of this:
- you must create notes
- create user
- then authenticate user
- ater check count of user's notes count:
approximately so:
@user = user.create sign_in @user 10.times { note.create(user: @user) } 10.times { note.create } :index assigns(:notes).should have(10).notes
Comments
Post a Comment