-
Notifications
You must be signed in to change notification settings - Fork 134
/
machinable_spec.rb
101 lines (83 loc) · 2.48 KB
/
machinable_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
101
require File.dirname(__FILE__) + '/spec_helper'
module MachinableSpecs
class Post
extend Machinist::Machinable
attr_accessor :title, :body, :comments
end
class Comment
extend Machinist::Machinable
attr_accessor :post, :title
end
end
describe Machinist::Machinable do
before(:each) do
MachinableSpecs::Post.clear_blueprints!
end
it "makes an object" do
MachinableSpecs::Post.blueprint do
title { "First Post" }
end
post = MachinableSpecs::Post.make
post.should be_a(MachinableSpecs::Post)
post.title.should == "First Post"
end
it "makes an object from a named blueprint" do
MachinableSpecs::Post.blueprint do
title { "First Post" }
body { "Woot!" }
end
MachinableSpecs::Post.blueprint(:extra) do
title { "Extra!" }
end
post = MachinableSpecs::Post.make(:extra)
post.should be_a(MachinableSpecs::Post)
post.title.should == "Extra!"
post.body.should == "Woot!"
end
it "makes an array of objects" do
MachinableSpecs::Post.blueprint do
title { "First Post" }
end
posts = MachinableSpecs::Post.make(3)
posts.should be_an(Array)
posts.should have(3).elements
posts.each do |post|
post.should be_a(MachinableSpecs::Post)
post.title.should == "First Post"
end
end
it "makes array attributes from the blueprint" do
MachinableSpecs::Comment.blueprint { }
MachinableSpecs::Post.blueprint do
comments(3) { MachinableSpecs::Comment.make }
end
post = MachinableSpecs::Post.make
post.comments.should be_a(Array)
post.comments.should have(3).elements
post.comments.each do |comment|
comment.should be_a(MachinableSpecs::Comment)
end
end
it "fails without a blueprint" do
expect do
MachinableSpecs::Post.make
end.to raise_error(Machinist::NoBlueprintError) do |exception|
exception.klass.should == MachinableSpecs::Post
exception.name.should == :master
end
expect do
MachinableSpecs::Post.make(:some_name)
end.to raise_error(Machinist::NoBlueprintError) do |exception|
exception.klass.should == MachinableSpecs::Post
exception.name.should == :some_name
end
end
it "fails when calling make! on an unsavable object" do
MachinableSpecs::Post.blueprint { }
expect do
MachinableSpecs::Post.make!
end.to raise_error(Machinist::BlueprintCantSaveError) do |exception|
exception.blueprint.klass.should == MachinableSpecs::Post
end
end
end