forked from AdaGold/media-ranker
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathvote_test.rb
103 lines (81 loc) · 2.5 KB
/
vote_test.rb
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
require "test_helper"
describe Vote do
# it "does a thing" do
# value(1+1).must_equal 2
# end
#
it "can be instantiated" do
# Assert
work = works(:gods)
user = users(:ron)
vote1 = Vote.create(user_id: user.id, work_id: work.id)
expect(vote1.valid?).must_equal true
vote2 = votes(:first)
expect(vote2.valid?).must_equal true
end
it "will have the required fields" do
# Arrange
vote = votes(:third)
[:work_id, :user_id].each do |field|
# Assert
expect(vote).must_respond_to field
end
end
describe "validations" do
it "will not let user vote for work more than once" do
# Arrange
duplicate_vote = Vote.create(work_id: 1, user_id: 1)
# Assert
expect(duplicate_vote.valid?).must_equal false
expect(duplicate_vote.errors.messages).must_include :work_id
end
end
describe 'relations' do
it 'can set the user through user' do
# Create two models
user = User.create!(name: "test user")
vote = Vote.new(work_id: 1, user_id: 4)
# Make the models relate to one another
vote.user = user
# author_id should have changed accordingly
expect(vote.user).must_equal user
end
it 'can set the user through "user_id"' do
# Create two models
user = User.create!(name: "test user")
vote = Vote.new(work_id: 1, user_id: 4)
# Make the models relate to one another
vote.user_id = user.id
# author_id should have changed accordingly
expect(vote.user_id).must_equal user.id
end
it 'can set the work through work' do
# Create two models
work = works(:world)
vote = Vote.new(work_id: 1, user_id: 4)
# Make the models relate to one another
vote.work = work
# author_id should have changed accordingly
expect(vote.work).must_equal work
end
it 'can set the work through "work_id"' do
# Create two models
work = works(:world)
vote = Vote.new(work_id: 1, user_id: 4)
# Make the models relate to one another
vote.work_id = work.id
# author_id should have changed accordingly
expect(vote.work_id).must_equal work.id
end
it "belongs to one user" do
vote = votes(:first)
expect(vote.user).must_be_instance_of User
expect(vote.user).must_equal users(:harry)
end
it "belongs to one work" do
vote = votes(:first)
expect(vote.work).must_be_instance_of Work
expect(vote.work).must_equal works(:gods)
end
end
end