From 33816d4312dca97d0bc8a1ebd0a261a8e328bad1 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Sun, 26 Nov 2017 20:39:49 +1300 Subject: [PATCH] Use faker gem, in account types - and add controller specs --- Gemfile | 1 + Gemfile.lock | 4 ++- app/models/account_type.rb | 6 ++++ .../account_types_controller_spec.rb | 28 ++++++++++++++++--- spec/factories/account_types.rb | 8 +++++- 5 files changed, 41 insertions(+), 6 deletions(-) diff --git a/Gemfile b/Gemfile index af2f2ebc8..36a8608c0 100644 --- a/Gemfile +++ b/Gemfile @@ -134,6 +134,7 @@ group :development, :test do gem 'coveralls', require: false # coverage analysis gem 'database_cleaner' gem 'factory_bot_rails' # for creating test data + gem 'faker' gem 'haml-i18n-extractor' gem 'haml-rails' # HTML templating language gem 'haml_lint' # Checks haml files for goodness diff --git a/Gemfile.lock b/Gemfile.lock index 017c433c1..74118c7e9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -181,6 +181,8 @@ GEM factory_bot_rails (4.8.2) factory_bot (~> 4.8.2) railties (>= 3.0.0) + faker (1.8.4) + i18n (~> 0.5) faraday (0.12.2) multipart-post (>= 1.2, < 3) ffi (1.9.18) @@ -578,6 +580,7 @@ DEPENDENCIES elasticsearch-model elasticsearch-rails factory_bot_rails + faker figaro flickraw font-awesome-sass @@ -633,7 +636,6 @@ DEPENDENCIES will_paginate xmlrpc - RUBY VERSION ruby 2.4.1p111 diff --git a/app/models/account_type.rb b/app/models/account_type.rb index d6be51a94..06416751a 100644 --- a/app/models/account_type.rb +++ b/app/models/account_type.rb @@ -1,6 +1,12 @@ class AccountType < ActiveRecord::Base + # + # Relationships has_many :products + # + # Validations + validates :name, presence: true, uniqueness: true + def to_s name end diff --git a/spec/controllers/account_types_controller_spec.rb b/spec/controllers/account_types_controller_spec.rb index 3a7559f63..37dbfa706 100644 --- a/spec/controllers/account_types_controller_spec.rb +++ b/spec/controllers/account_types_controller_spec.rb @@ -1,10 +1,30 @@ require 'rails_helper' describe AccountTypesController do - # This automatically creates a "Free" account type - login_member(:admin_member) + subject { response } - def valid_attributes - { "name" => "MyString" } + context 'anon' do + describe '#index' do + before { get :index } + it { is_expected.not_to be_success } + end + end + context 'member' do + login_member(:member) + describe '#index' do + before { get :index } + it { is_expected.not_to be_success } + end + end + context 'admin' do + login_member(:admin_member) + describe '#index' do + let!(:aaa) { FactoryBot.create :account_type, name: 'aaa' } + let!(:zzz) { FactoryBot.create :account_type, name: 'zzz' } + before { get :index } + it { is_expected.to be_success } + it { expect(assigns[:account_types].first).to eql(aaa) } + it { expect(assigns[:account_types].last).to eql(zzz) } + end end end diff --git a/spec/factories/account_types.rb b/spec/factories/account_types.rb index a983e24e4..66717492f 100644 --- a/spec/factories/account_types.rb +++ b/spec/factories/account_types.rb @@ -2,10 +2,16 @@ FactoryBot.define do factory :account_type do - name "Free" + name { Faker::Name.unique.name } is_paid false is_permanent_paid false + factory :free_account_type do + name "Free" + is_paid false + is_permanent_paid false + end + factory :paid_account_type do name "Paid" is_paid true