technoweenie / machinist forked from notahat/machinist

A Rails plugin to help populate your database with data for tests.

This URL has Read+Write access

machinist / spec / machinist_spec.rb
100644 102 lines (80 sloc) 2.085 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
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
require File.dirname(__FILE__) + '/spec_helper'
require 'machinist'
 
class Base
  include Machinist
 
  attr_accessor :invalid
 
  def save!
    raise "Invalid record" if @invalid
    save
  end
 
  def save; @saved = !@invalid; end
  def reload; @reloaded = true; self; end
  
  def saved?; @saved; end
  def reloaded?; @reloaded; end
 
  def new_record?
    !@saved
  end
end
 
class Post < Base
  attr_accessor :title
  attr_accessor :body
end
 
class Comment < Base
  attr_accessor :post
  attr_accessor :author
  attr_accessor :body
end
 
Post.blueprint do
  title "An Example Post"
  body { "The quick brown fox." }
end
 
Comment.blueprint do
  post
  author "Fred Bloggs"
  body "Just a comment."
end
 
Comment.blueprint :bob do
  post
  author "Bob"
  body "Just a comment."
end
 
describe Machinist do
  describe "calling make with no arguments" do
    before do
      @post = Post.make
    end
    
    it "should set a field from a constant in the blueprint" do
      @post.title.should == "An Example Post"
    end
  
    it "should set a field from a block in the blueprint" do
      @post.body.should == "The quick brown fox."
    end
    
    it "should save the object" do
      @post.should be_saved
    end
    
    it "should reload the object" do
      @post.should be_reloaded
    end
  end
  
  it "should override a field from the blueprint with a parameter" do
    post = Post.make(:title => "A Different Title")
    post.title.should == "A Different Title"
  end
  
  it "should override a field from the blueprint with nil" do
    post = Post.make(:title => nil)
    post.title.should be_nil
  end
 
  it "should return invalid object from #make!" do
    post = Post.make!(:invalid => true)
    post.should_not be_saved
    post.should_not be_reloaded
  end
 
  it "should create an associated object for a field with no arguments in the blueprint" do
    comment = Comment.make
    comment.post.should_not be_nil
  end
 
  it "creates an object from a non-default blueprint" do
    comment = Comment.make(:bob)
    comment.author.should == 'Bob'
  end
end