RSpec.describe 'Widget management', type::requestdo it creates a Widget and redirects to the Widget's page do get '/widgets/new' expect(response).to render_template(:new) post '/widgets', widget: { name: 'My Widget'} expect(response).to redirect_to(assigns(:widget)) follow_redirect! expect(response).to render_template(:show) expect(response.body).to include('Widget was successfully created.') end end
🚜 JSONのレスポンスのテスト
APIにアクセスして、JSONのレスポンスを受け取るテストです。
require'rails_helper'
RSpec.describe 'Widget management', type::requestdo it 'creates a Widget'do headers = { 'ACCEPT' => 'application/json', # This is what Rails 4 accepts 'HTTP_ACCEPT' => 'application/json'# This is what Rails 3 accepts } post '/widgets', { widget: { name:'My Widget'} }, headers
expect(response.content_type).to eq('application/json') expect(response).to have_http_status(:created) end end