/
blueprint_spec.rb
76 lines (66 loc) · 2.22 KB
/
blueprint_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
require File.dirname(__FILE__) + '/spec_helper'
require 'ostruct'
describe Machinist::Blueprint do
it "makes an object of the given class" do
blueprint = Machinist::Blueprint.new(OpenStruct) { }
blueprint.make.should be_an(OpenStruct)
end
it "constructs an attribute from the blueprint" do
blueprint = Machinist::Blueprint.new(OpenStruct) do
name { "Fred" }
end
blueprint.make.name.should == "Fred"
end
it "constructs an array for an attribute in the blueprint" do
blueprint = Machinist::Blueprint.new(OpenStruct) do
things(3) { Object.new }
end
things = blueprint.make.things
things.should be_an(Array)
things.should have(3).elements
things.each {|thing| thing.should be_an(Object) }
things.uniq.should == things
end
it "allows passing in attributes to override the blueprint" do
block_called = false
blueprint = Machinist::Blueprint.new(OpenStruct) do
name { block_called = true; "Fred" }
end
blueprint.make(:name => "Bill").name.should == "Bill"
block_called.should be_false
end
it "provides a serial number within the blueprint" do
blueprint = Machinist::Blueprint.new(OpenStruct) do
name { "Fred #{sn}" }
end
blueprint.make.name.should == "Fred 0001"
blueprint.make.name.should == "Fred 0002"
end
it "provides access to the object being constructed within the blueprint" do
blueprint = Machinist::Blueprint.new(OpenStruct) do
title { "Test" }
body { object.title }
end
blueprint.make.body.should == "Test"
end
it "allows attribute names to be strings" do
blueprint = Machinist::Blueprint.new(OpenStruct) do
name { "Fred" }
end
blueprint.make("name" => "Bill").name.should == "Bill"
end
# These are normally a problem because of name clashes with the standard (but
# deprecated) Ruby methods. This test makes sure we work around this.
it "works with type and id attributes" do
klass = Class.new do
attr_accessor :id, :type
end
blueprint = Machinist::Blueprint.new(klass) do
id { "custom id" }
type { "custom type" }
end
object = blueprint.make
object.id.should == "custom id"
object.type.should == "custom type"
end
end