comment_test.rb
1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require File.dirname(__FILE__) + '/../test_helper'
class CommentTest < Test::Unit::TestCase
should 'have a name and require it' do
assert_mandatory(Comment.new, :title)
end
should 'have a body and require it' do
assert_mandatory(Comment.new, :body)
end
should 'belong to an article' do
c = Comment.new
assert_raise ActiveRecord::AssociationTypeMismatch do
c.article = 1
end
assert_nothing_raised do
c.article = Article.new
end
end
should 'record authenticated author' do
c = Comment.new
assert_raise ActiveRecord::AssociationTypeMismatch do
c.author = 1
end
assert_raise ActiveRecord::AssociationTypeMismatch do
c.author = Profile
end
assert_nothing_raised do
c.author = Person.new
end
end
should 'record unauthenticated author' do
assert_optional Comment.new, :name
assert_optional Comment.new, :email
# if given name, require email
c1 = Comment.new
c1.name = 'My Name'
assert_mandatory c1, :email
# if given email, require name
c2 = Comment.new
c2.email = 'my@email.com'
assert_mandatory c2, :name
end
should 'accept either an authenticated or unauthenticated author' do
assert_mandatory Comment.new, :author_id
c1 = Comment.new
c1.author = create_user('someperson').person
c1.name = 'my name'
c1.valid?
assert c1.errors.invalid?(:name)
end
end