add zone test-rspec

This commit is contained in:
Min Zeya Phyo
2017-03-24 21:28:57 +06:30
parent 3ec71e85fe
commit a247cd44eb
19 changed files with 108 additions and 4 deletions

View File

@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

View File

@@ -0,0 +1,3 @@
// Place all the styles related to the api/restaurant/zone controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View File

@@ -0,0 +1,4 @@
class Api::Restaurant::ZoneController < ApplicationController
def index
end
end

View File

@@ -0,0 +1,2 @@
module Api::Restaurant::ZoneHelper
end

View File

@@ -1,3 +1,6 @@
class SeatTable < ApplicationRecord
belongs_to :zone
# validations
validates_presence_of :name,:order_by, :created_by
end

View File

@@ -1,2 +1,7 @@
class Zone < ApplicationRecord
# model association
has_many :seat_tables, dependent: :destroy
# validations
validates_presence_of :name, :created_by
end

View File

@@ -0,0 +1,2 @@
<h1>Api::Restaurant::Zone#index</h1>
<p>Find me in app/views/api/restaurant/zone/index.html.erb</p>

View File

@@ -1,3 +1,9 @@
Rails.application.routes.draw do
namespace :api do
namespace :restaurant do
get 'zone/index'
end
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

View File

@@ -3,6 +3,7 @@ class CreateZones < ActiveRecord::Migration[5.0]
create_table :zones do |t|
t.string :name, :null => false
t.boolean :is_active, :default => true
t.string :created_by
t.timestamps
end
end

View File

@@ -8,7 +8,7 @@ class CreateSeatTables < ActiveRecord::Migration[5.0]
t.string :table_type, :null => false, :default => "square"
t.float :position_x, :null => false, :default => 0.0
t.float :position_y, :null => false, :default => 0.0
t.string :created_by
t.timestamps
end
end

View File

@@ -0,0 +1,12 @@
require 'rails_helper'
RSpec.describe Api::Restaurant::ZoneController, type: :controller do
describe "GET #index" do
it "returns http success" do
get :index
expect(response).to have_http_status(:success)
end
end
end

View File

@@ -0,0 +1,8 @@
FactoryGirl.define do
factory :seat_tables do
name { Faker::StarWars.character }
order_by { Faker::Number.number(5)}
no_of_seater {Faker::Number.number(5)}
zone_id nil
end
end

6
spec/factories/zones.rb Normal file
View File

@@ -0,0 +1,6 @@
FactoryGirl.define do
factory :zone do
name { Faker::Lorem.word }
created_by { Faker::StarWars.character }
end
end

View File

@@ -1,5 +1,13 @@
require 'rails_helper'
RSpec.describe SeatTable, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
# Association test
# ensure an item record belongs to a single todo record
it { should belong_to(:zone) }
# Validation test
# ensure column name is present before saving
it { should validate_presence_of(:name) }
it { should validate_presence_of(:order_by) }
it { should validate_presence_of(:created_by) }
end

View File

@@ -1,5 +1,11 @@
require 'rails_helper'
RSpec.describe Zone, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
# Association test
# ensure Todo model has a 1:m relationship with the Item model
it { should have_many(:seat_tables).dependent(:destroy) }
# Validation tests
# ensure columns title and created_by are present before saving
it { should validate_presence_of(:name) }
it { should validate_presence_of(:created_by) }
end

View File

@@ -22,7 +22,7 @@ require 'database_cleaner'
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
@@ -43,6 +43,9 @@ RSpec.configure do |config|
# add `FactoryGirl` methods
config.include FactoryGirl::Syntax::Methods
config.include RequestSpecHelper, type: :request
# start by truncating all the tables but then use the faster transaction strategy the rest of the time.
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)

View File

@@ -0,0 +1,25 @@
require 'rails_helper'
RSpec.describe 'Zone API', type: :request do
# initialize test data
let!(:zone) { create_list(:zone, 10) }
let(:zone_id) { zones.first.id }
# Test suite for GET /todos
describe 'GET /api/restaurant/zones' do
# make HTTP get request before each example
before { get '/api/restaurant/zones' }
it 'returns zones' do
# Note `json` is a custom helper to parse JSON responses
expect(json).not_to be_empty
expect(json.size).to eq(10)
end
it 'returns status code 200' do
expect(response).to have_http_status(200)
end
end
end

View File

@@ -0,0 +1,7 @@
# spec/support/request_spec_helper
module RequestSpecHelper
# Parse JSON response to ruby hash
def json
JSON.parse(response.body)
end
end