Skip to content

Commit 8a1f050

Browse files
committed
Adds Character validation specs
1 parent 8ffab9f commit 8a1f050

File tree

5 files changed

+68
-13
lines changed

5 files changed

+68
-13
lines changed

Gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ group :development, :test do
8787
gem 'faker'
8888
# gem 'jslint_on_rails'
8989
gem 'jshint_on_rails'
90-
gem 'launchy'
9190
gem 'letter_opener'
9291
gem 'meta_request'
9392
gem 'rails-erd'
@@ -97,6 +96,8 @@ end
9796

9897
group :test do
9998
gem 'capybara'
99+
gem 'faker'
100100
gem 'guard-rspec'
101+
gem 'launchy'
101102
gem 'shoulda-matchers'
102103
end

app/models/character.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Character < ActiveRecord::Base
66
presence: true
77

88
validates :name, length: { in: 1..80 }
9-
validates :name, uniqueness: { scope: :affinity_id }
9+
validates :name, uniqueness: { scope: :series_id }
1010

1111
validates :stat_hp, :stat_str, :stat_def,
1212
:stat_spd, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
@@ -89,6 +89,8 @@ def ensure_defaults
8989
self.stat_str ||= 0
9090
self.stat_def ||= 0
9191
self.stat_spd ||= 0
92+
self.stat_int ||= 0
93+
self.stat_luck ||= 0
9294
end
9395

9496
def on_before_save

app/models/user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class User < ActiveRecord::Base
77
:recoverable, :rememberable, :trackable, :validatable,
88
# :confirmable,
99
:lockable, :omniauthable
10-
#TODO uncomment confirmable before release
10+
#TODO uncomment confirmable after exiting alpha release
1111
has_many :entries
1212
has_many :contests
1313
has_many :characters

spec/factories/characters.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
FactoryGirl.define do
44
factory :character do
5-
name "MyString"
6-
desc "MyText"
7-
user_id 1
8-
affinity 1
9-
tier_id 1
10-
stat_hp 1
11-
stat_str 1
12-
stat_def 1
13-
stat_spd 1
5+
name { Faker::Name.name }
6+
desc { "This character is shrouded in mystery...\nWhat potential lies in store?" }
7+
user_id { 1 }
8+
affinity_id { [1,2,3].sample }
9+
tier_id { [1,2,3,4].sample }
10+
series_id { nil }
11+
stat_hp { (100*rand).floor }
12+
stat_str { (100*rand).floor }
13+
stat_def { (100*rand).floor }
14+
stat_spd { (100*rand).floor }
15+
stat_int { (100*rand).floor }
16+
stat_luck { (100*rand).floor }
1417
end
1518
end

spec/models/character_spec.rb

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,54 @@
11
require 'spec_helper'
22

33
describe Character do
4-
pending "add some examples to (or delete) #{__FILE__}"
4+
context 'validation' do
5+
it 'has a valid factory' do
6+
expect(FactoryGirl.create(:character)).to be_valid
7+
end
8+
9+
it 'is invalid without a name' do
10+
expect(FactoryGirl.build(:character, name: nil)).to_not be_valid
11+
end
12+
13+
it 'is invalid if name is more than 80 characters' do
14+
expect(FactoryGirl.build(:character, name: "as"*41)).to_not be_valid
15+
end
16+
17+
it 'is invalid without an affinity' do
18+
expect(FactoryGirl.build(:character, affinity_id: nil)).to_not be_valid
19+
end
20+
21+
it 'is invalid without a tier' do
22+
expect(FactoryGirl.build(:character, tier_id: nil)).to_not be_valid
23+
end
24+
25+
it 'is invalid without stats' do
26+
expect(FactoryGirl.build(:character, stat_hp: nil)).to_not be_valid
27+
expect(FactoryGirl.build(:character, stat_str: nil)).to_not be_valid
28+
expect(FactoryGirl.build(:character, stat_def: nil)).to_not be_valid
29+
expect(FactoryGirl.build(:character, stat_spd: nil)).to_not be_valid
30+
expect(FactoryGirl.build(:character, stat_int: nil)).to_not be_valid
31+
expect(FactoryGirl.build(:character, stat_luck: nil)).to_not be_valid
32+
end
33+
34+
it 'is invalid with stats than 0' do
35+
expect(FactoryGirl.build(:character, stat_str: -1)).to_not be_valid
36+
end
37+
38+
it 'is invalid with non-integer stats' do
39+
expect(FactoryGirl.build(:character, stat_str: 1.5)).to_not be_valid
40+
end
41+
42+
it 'is invalid when it shares a name with another character in the same series' do
43+
bob1 = FactoryGirl.create(:character, name: "Bob", series_id: 1)
44+
bob2 = FactoryGirl.build(:character, name: "Bob", series_id: 1)
45+
expect(bob2).to_not be_valid
46+
end
47+
48+
it 'is valid when it shares a name with another character in a different series' do
49+
bob1 = FactoryGirl.create(:character, name: "Bob", series_id: 1)
50+
bob2 = FactoryGirl.build(:character, name: "Bob", series_id: 2)
51+
expect(bob2).to be_valid
52+
end
53+
end
554
end

0 commit comments

Comments
 (0)