Grape Put endpoint not properly getting the id from Rspec test

16 views Asked by At

I'm trying to write a put endpoint in grape for my api, I wrote a test for it and when I run the test, I get a 400 as a response that says

ActiveRecord::RecordNotFound: Couldn't find Book with 'id'=:id

My endpoint:

desc 'update a book'
    params do
      requires :id, type: Integer, desc: 'book id'
      optional :isbn, type: Integer, desc: 'International Standard Book Number'
      optional :title, type: String, desc: 'title of the book'
      optional :stock, type: Integer, desc: 'how many books are in stock'
    end
    put ':id' do
      Book.find(params[:id]).update({
          isbn: params[:book][:isbn],
          title: params[:book][:title],
          stock: params[:book][:stock]
      })
    end

My spec file:

context 'put' do
   let(:book_endpoint) { '/api/v1/books/:id' }
   let!(:book) { create(:book) }
   let(:book_id) { book.id }
   let(:new_isbn) { book.isbn+2 }
   let(:new_title) { book.title+'ahhh' }
   let(:new_stock) { book.stock+2 }

   it 'updates the isbn, title and stock of a specified book' do
      put book_endpoint, :params => { id: book_id, book: {isbn: new_isbn, title: new_title, stock: new_stock }}

      expect(Book.all.count).to be 1
      expect(Book.first.id).to eq book_id
      expect(Book.first.title).not_to eq book.title
      expect(Book.first.isbn).not_to eq book.isbn
      expect(Book.first.stock).not_to eq book.stock
   end
end

When I stopped the debugger in the endpoint file, I took a look at what params was, and it said it's a HashWithIndifferentAccess, I can see and access everything inside params[:book], but for id it just says "id" => ":id". I thought maybe I wasn't sending anything from my test file, but in the debugger I can see the id just fine

I'm pretty stumped

0

There are 0 answers