Skip to content

Commit

Permalink
added validations to micropost model
Browse files Browse the repository at this point in the history
  • Loading branch information
scarvill91 committed Mar 29, 2015
1 parent e18b91a commit 9e2d70a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
2 changes: 2 additions & 0 deletions app/models/micropost.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class Micropost < ActiveRecord::Base
belongs_to :user
validates :user_id, presence: true
validates :content, presence: true, length: { maximum: 140 }
end
28 changes: 25 additions & 3 deletions test/models/micropost_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
require 'test_helper'

class MicropostTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end

def setup
@user = users(:michael)
# This code is not idiomatically correct.
@micropost = Micropost.new(content: "Lorem ipsum", user_id: @user.id)
end

test "should be valid" do
assert @micropost.valid?
end

test "user id should be present" do
@micropost.user_id = nil
assert_not @micropost.valid?
end

test "content should be present " do
@micropost.content = " "
assert_not @micropost.valid?
end

test "content should be at most 140 characters" do
@micropost.content = "a" * 141
assert_not @micropost.valid?
end
end

0 comments on commit 9e2d70a

Please sign in to comment.