Commit ad658c4c

luuduong <luuduong@gmail.com>
2014-12-06 14:54:53
added validations to tools
1 parent 8510758
Changed files (2)
app
models
spec
app/models/tool.rb
@@ -1,3 +1,3 @@
 class Tool < ActiveRecord::Base
-
+  validates :name, presence: true, uniqueness: true
 end
\ No newline at end of file
spec/models/tool_spec.rb
@@ -33,5 +33,19 @@ describe Tool do
     expect(tool.asin).to eql("223455")
   end
 
+  context "#validation" do
+    it "has to have a name" do
+      tool = Tool.new
+      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?
+      expect(tool.errors[:name]).to_not be_empty
+    end
+  end
 end