/
active_model_compliance_spec.rb
104 lines (80 loc) · 2.65 KB
/
active_model_compliance_spec.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
require 'spec_helper'
begin
require 'active_model'
describe 'ActiveModel conformance of couch potato objects' do
include ActiveModel::Lint::Tests
instance_methods.sort.select{|method| method.to_s.scan(/^test_/).first}.each do |method|
it "should #{method.to_s.sub(/^test_/, '').gsub('_', ' ')}" do
send method
end
end
class ActiveComment
include CouchPotato::Persistence
property :name
property :email
validates_presence_of :name, :email
validates_format_of :email, :with => /.+@.+/
end
before(:each) do
@model = ActiveComment.new
end
describe '#to_partial_path' do
it 'returns a path based on the class name' do
@model.to_partial_path.should == 'active_comments/active_comment'
end
end
describe "#persisted?" do
it "should return false if it is a new document " do
@model.should_not be_persisted
end
it "should be true if it was saved" do
@comment = ActiveComment.new(:name => 'Thilo', :email => 'test@local.host')
CouchPotato.database.save_document! @comment
@comment.should be_persisted
end
end
describe "#to_key" do
it "should return nil if the document was not persisted" do
@model.to_key.should be_nil
end
it "should return the id of the document if it was persisted" do
@comment = ActiveComment.new(:name => 'Thilo', :email => 'test@local.host')
CouchPotato.database.save_document! @comment
@comment.to_key.should == [@comment.id]
end
end
describe "#errors" do
it "should return a single error as array" do
@model.valid?
@model.errors[:name].should be_kind_of(Array)
end
it "should return multiple errors as array" do
@model.valid?
@model.errors[:email].size.should == 2
end
it "should return no error as an empty array" do
@model.errors[:name].should == []
end
it "should be able to be Marshal.dump'ed" do
lambda { Marshal.dump(@model.errors) }.should_not raise_error
end
end
describe "#destroyed" do
it "should return destroyed if the object is deleted" do
@model._deleted = true
@model.should be_destroyed
end
it "should not return destroyed if it's not deleted" do
@model.should_not be_destroyed
end
end
def assert(boolean, message = '')
boolean || raise(message)
end
def assert_kind_of(klass, object)
object.should be_a(klass)
end
end
rescue LoadError
STDERR.puts "WARNING: active_model gem not installed. Not running ActiveModel specs."
end