Hi, I want to create a similar test as described in the following tutorial:
The relevant code for me is the put request, but in my code I can’t use let since I’ve some validation that will fail (my form has a file upload field and I don’t want to test it since it will slow the tests). How can I skip the validation for the creation of the object and still receive an object id of the fake created object.
Here is the relevant code from the tutorial.
I would like this line to create the object event if the validation fails: let!(:bookmark) { Bookmark.create(url: ‘https://rubyyagi.com’, title: ‘Ruby Yagi’) }
require 'rails_helper'
describe 'PUT /bookmarks' do
# this will create a 'bookmark' method, which return the created bookmark object,
# before each scenario is ran
let!(:bookmark) { Bookmark.create(url: 'https://rubyyagi.com', title: 'Ruby Yagi') }
scenario 'valid bookmark attributes' do
# send put request to /bookmarks/:id
put "/bookmarks/#{bookmark.id}", params: {
bookmark: {
url: 'https://fluffy.es',
title: 'Fluffy'
}
}
# response should have HTTP Status 200 OK
expect(response.status).to eq(200)
# response should contain JSON of the updated object
json = JSON.parse(response.body).deep_symbolize_keys
expect(json[:url]).to eq('https://fluffy.es')
expect(json[:title]).to eq('Fluffy')
# The bookmark title and url should be updated
expect(bookmark.reload.title).to eq('Fluffy')
expect(bookmark.reload.url).to eq('https://fluffy.es')
end
scenario 'invalid bookmark attributes' do
# send put request to /bookmarks/:id
put "/bookmarks/#{bookmark.id}", params: {
bookmark: {
url: '',
title: 'Fluffy'
}
}
# response should have HTTP Status 422 Unprocessable entity
expect(response.status).to eq(422)
# response should contain error message
json = JSON.parse(response.body).deep_symbolize_keys
expect(json[:url]).to eq(["can't be blank"])
# The bookmark title and url remain unchanged
expect(bookmark.reload.title).to eq('Ruby Yagi')
expect(bookmark.reload.url).to eq('https://rubyyagi.com')
end
end
Many thanks!