Skip to content

Commit

Permalink
add test case for message model
Browse files Browse the repository at this point in the history
  • Loading branch information
brickgao committed Jul 25, 2016
1 parent 85d6722 commit 9b98c38
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/models/message.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Message < ActiveRecord::Base
belongs_to :from, :class_name => "User"
belongs_to :to, :class_name => "User"
validates :from, presence: true
validates :to, presence: true
validates :body, presence: true, length: { minimum: 5, maximum: 200 }
validates :is_read, presence: true
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class User < ActiveRecord::Base
has_many :posts
has_many :comments
has_many :message_from, :class_name => "Message", :foreign_key => "from_id"
has_many :message_to, :class_name => "Message", :foreign_key => "to_id"
before_save { self.email.downcase! }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ alice:
name: Alice
email: alice@test.com
password_digest: <%= User.digest('badpassword') %>

bob:
name: Bob
email: bob@test.com
password_digest: <%= User.digest('anotherbadpassword') %>
2 changes: 1 addition & 1 deletion test/models/comment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def setup
assert_not @comment.valid?
end

test "body should be present" do
test "comment should not be too short" do
@comment.body = " " * 8
assert_not @comment.valid?
end
Expand Down
39 changes: 39 additions & 0 deletions test/models/message_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,43 @@ class MessageTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
def setup
from, to = users("alice"), users("bob")
@message = Message.new(from: from, to: to,
body: "samplemessage", is_read: 0)
end

test "should be vaild" do
assert @message.valid?
end

test "from should be present" do
@message.from = nil
assert_not @message.valid?
end

test "to should be present" do
@message.to = nil
assert_not @message.valid?
end

test "body should be present" do
@message.body = nil
assert_not @message.valid?
end

test "is_read should be present" do
@message.is_read = nil
assert_not @message.valid?
end

test "body should not be too short" do
@message.body = " " * 3
assert_not @message.valid?
end

test "body should not be too long" do
@message.body = "a" * 300
assert_not @message.valid?
end
end

0 comments on commit 9b98c38

Please sign in to comment.