Skip to content

Instantly share code, notes, and snippets.

@JunichiIto

JunichiIto/api_products_spec.rb Secret

Last active Nov 30, 2016
Embed
What would you like to do?
require 'rails_helper'
describe 'Api::Products' do
let(:product) {create :product, name: 'AB-123CD'}
let!(:detail) {product.create_detail(style: '屋外用')}
let(:sub_product) {create :sub_product, name: 'AB-456UV'}
let!(:sub_detail) {sub_product.create_sub_detail(style: '据置型')}
describe 'index' do
example do
get products_path(q: 'ab', role: 'admin')
expect(response).to have_http_status :ok
result = JSON.parse(response.body)
data = Hashie::Mash.new(result)
expect(data.products.size).to eq 2
product_1 = data.products[0]
expect(product_1.type).to eq 'Product'
expect(product_1.name).to eq 'AB-123CD'
expect(product_1.style).to eq '屋外用'
product_2 = data.products[1]
expect(product_2.type).to eq 'SubProduct'
expect(product_2.name).to eq 'AB-456UV'
expect(product_2.style).to eq '据置型'
# detailがないとき
detail.destroy
sub_detail.destroy
get products_path(q: 'ab', role: 'admin')
expect(response).to have_http_status :ok
result = JSON.parse(response.body)
data = Hashie::Mash.new(result)
expect(data.products.size).to eq 2
product_1 = data.products[0]
expect(product_1.type).to eq 'Product'
expect(product_1.name).to eq 'AB-123CD'
expect(product_1.style).to be_nil
product_2 = data.products[1]
expect(product_2.type).to eq 'SubProduct'
expect(product_2.name).to eq 'AB-456UV'
expect(product_2.style).to be_nil
end
end
describe 'show' do
example do
get product_path(product, type: 'Product', role: 'admin')
expect(response).to have_http_status :ok
result = JSON.parse(response.body)
product_1 = Hashie::Mash.new(result).product
expect(product_1.type).to eq 'Product'
expect(product_1.name).to eq 'AB-123CD'
expect(product_1.style).to eq '屋外用'
get product_path(sub_product, type: 'SubProduct', role: 'admin')
expect(response).to have_http_status :ok
result = JSON.parse(response.body)
product_2 = Hashie::Mash.new(result).product
expect(product_2.type).to eq 'SubProduct'
expect(product_2.name).to eq 'AB-456UV'
expect(product_2.style).to eq '据置型'
# detailがないとき
detail.destroy
sub_detail.destroy
get product_path(product, type: 'Product', role: 'admin')
expect(response).to have_http_status :ok
result = JSON.parse(response.body)
product_1 = Hashie::Mash.new(result).product
expect(product_1.type).to eq 'Product'
expect(product_1.name).to eq 'AB-123CD'
expect(product_1.style).to be_nil
get product_path(sub_product, type: 'SubProduct', role: 'admin')
expect(response).to have_http_status :ok
result = JSON.parse(response.body)
product_2 = Hashie::Mash.new(result).product
expect(product_2.type).to eq 'SubProduct'
expect(product_2.name).to eq 'AB-456UV'
expect(product_2.style).to be_nil
end
end
describe 'エラー処理' do
context 'レコードが見つからない場合' do
example do
get product_path(-1, type: 'Product', role: 'admin')
expect(response).to have_http_status :not_found
result = JSON.parse(response.body)
expect(result['message']).to eq 'Not Found'
end
end
context 'typeの指定がおかしい場合' do
example do
get product_path(product, type: 'FooBar', role: 'admin')
expect(response).to have_http_status :not_found
result = JSON.parse(response.body)
expect(result['message']).to eq 'Not Found'
end
end
context '権限がない場合' do
example do
get product_path(product, type: 'Product')
expect(response).to have_http_status :forbidden
result = JSON.parse(response.body)
expect(result['message']).to eq 'Forbidden'
end
end
context '実行時エラーが発生した場合' do
example do
allow(UserContext).to receive(:new).and_raise('TEST.')
get product_path(product, type: 'Product', role: 'admin')
expect(response).to have_http_status :internal_server_error
result = JSON.parse(response.body)
expect(result['message']).to eq 'TEST.'
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.