diff options
| author | luu <luuduong@gmail.com> | 2014-12-06 15:21:37 +0000 |
|---|---|---|
| committer | luu <luuduong@gmail.com> | 2014-12-06 15:21:37 +0000 |
| commit | f60ca93a327d773723f2ac7235331702256da656 (patch) | |
| tree | acff2a43acf7cc33baaaa5d2681ed3f7ca921eaf | |
| parent | ad658c4c7ba39191ed379ed73aa4a1b5a463be45 (diff) | |
add validation for asin and valid tool.
| -rw-r--r-- | app/models/tool.rb | 1 | ||||
| -rw-r--r-- | spec/factories.rb | 7 | ||||
| -rw-r--r-- | spec/models/tool_spec.rb | 22 |
3 files changed, 24 insertions, 6 deletions
diff --git a/app/models/tool.rb b/app/models/tool.rb index c675825f..43fd6e5b 100644 --- a/app/models/tool.rb +++ b/app/models/tool.rb @@ -1,3 +1,4 @@ class Tool < ActiveRecord::Base validates :name, presence: true, uniqueness: true + validates :asin, presence: true end
\ No newline at end of file diff --git a/spec/factories.rb b/spec/factories.rb index e1f30710..64309da3 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -76,4 +76,9 @@ FactoryGirl.define do city "Calgary" country "Canada" end -end + + factory :tool do + name { Faker::Name.name } + asin { SecureRandom.uuid } + end +end
\ No newline at end of file diff --git a/spec/models/tool_spec.rb b/spec/models/tool_spec.rb index caf6ba94..db08b512 100644 --- a/spec/models/tool_spec.rb +++ b/spec/models/tool_spec.rb @@ -34,18 +34,30 @@ describe Tool do end context "#validation" do + let(:tool) { build(:tool) } + it "has to have a name" do - tool = Tool.new + tool.name = nil expect(tool).to_not be_valid expect(tool.errors[:name]).to_not be_empty end it "name has to be unique" do - Tool.create(name: 'blah') - - tool = Tool.new(name: 'blah') - tool.valid? + create(:tool, name: 'blah') + tool.name = 'blah' + + expect(tool).to_not be_valid expect(tool.errors[:name]).to_not be_empty end + + it "has a ASIN number" do + tool.asin = nil + expect(tool).to_not be_valid + expect(tool.errors[:asin]).to_not be_empty + end + + it "validates" do + expect(tool).to be_valid + end end end |
