-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmodel_spec.rb
More file actions
55 lines (48 loc) · 1.54 KB
/
model_spec.rb
File metadata and controls
55 lines (48 loc) · 1.54 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
class MyCollection
include Hook::Model
field :name
field :score
validates_presence_of :score
end
class CustomHighscore
include Hook::Model
field :name
field :score
end
describe Hook::Model do
it "should respond to activemodel dirty methods" do
instance = MyCollection.new
expect(instance.name).to be_nil
expect(instance.name_changed?).to be == false
instance.name = 'Endel'
expect(instance.name).to be == 'Endel'
expect(instance.name_changed?).to be == true
expect(instance.save).to be == false, "shouldn't save when model has validation errors"
expect(instance.errors.messages.length).to be == 1
end
it "should set default attributes" do
instance = MyCollection.new(:name => "Endel", :score => 100)
expect(instance.name).to be == "Endel"
expect(instance.score).to be == 100
expect(instance.changed?).to be == true
instance.save
expect(instance.changed?).to be == false
end
it "general methods" do
instance = MyCollection.create(:name => "Endel", :score => 100)
expect(instance.name).to be == "Endel"
expect(instance.score).to be == 100
expect(instance.changed?).to be == false
expect(instance.save).to be == false
end
it "should create multiple" do
rows = CustomHighscore.create([
{:name => "Somebody", :score => 50},
{:name => "Anybody", :score => 10},
])
expect(rows.length).to be == 2
expect(rows[0].name).to be == "Somebody"
expect(rows[1].name).to be == "Anybody"
expect(CustomHighscore.delete_all).to be == 2
end
end