Guest User

Untitled

a guest
Feb 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. require File.join(File.dirname(__FILE__), "spec_helper")
  2.  
  3. describe SimpleFixtures do
  4.  
  5. before(:each) do
  6. @model = Class.new do
  7. attr_accessor :attrs, :blk
  8. def initialize(attrs, &blk)
  9. @attrs = attrs
  10. @blk = blk
  11. end
  12. def save
  13. @saved = true
  14. end
  15. def saved?
  16. @saved
  17. end
  18. end
  19.  
  20. @model.extend(SimpleFixtures)
  21. end
  22.  
  23. describe "regular named fixtures" do
  24.  
  25. before(:each) do
  26. model = @model
  27. @model.define_fixture( :name => "Person", :friend => proc { model.fixture!(:with_block) } )
  28. @model.define_fixture(:oleg, :name => "Oleg", :friend => proc { model.fixture!(:antares) } )
  29. @model.define_fixture(:antares, proc {{:name => "MK"}})
  30. @model.define_fixture(:with_block) do |overrides| # with block, but without a friend
  31. overrides.merge(:with_block => :i_am_over_it, :friend => nil)
  32. end
  33. end
  34.  
  35. it "should have named fixtures" do
  36. @model.fixtures[:__default__].should_not be_empty
  37. @model.fixtures[:oleg].should_not be_empty
  38. @model.fixtures[:antares].should respond_to(:call)
  39. @model.fixtures[:with_block].should respond_to(:call)
  40. end
  41.  
  42. it "should generate a named and default fixtures using Model#fixture and #fixture!" do
  43. f = @model.fixture(&@some_block)
  44.  
  45. f.should_not be_saved
  46. f.attrs[:friend].should be_saved
  47.  
  48. f.attrs[:name].should == "Person"
  49. f.attrs[:friend].should be_kind_of(@model)
  50. f.attrs[:friend].attrs[:name].should == "Person"
  51. f.attrs[:friend].attrs[:with_block].should == :i_am_over_it
  52. f.attrs[:friend].attrs[:friend].should == nil
  53. end
  54.  
  55. describe "unique_fixture!(:name, overrides = {})" do
  56. before(:each) do
  57. @uf1 = @model.unique_fixture!(:oleg, {:xyz => 123})
  58. @uf2 = @model.unique_fixture!(:oleg, {:qwe => 345})
  59. end
  60. it "should return equal unique fixtures" do
  61. @uf1.object_id.should == @uf2.object_id
  62. end
  63. it "should set attributes provided first time" do
  64. @uf2.attrs[:xyz].should == 123
  65. @uf2.attrs[:qwe].should be_nil
  66. end
  67. end # unique_fixture!(:name, overrides)
  68.  
  69. describe "fixture!(:name)" do
  70. before(:each) do
  71. @uf1 = @model.fixture!(:oleg)
  72. @uf2 = @model.fixture!(:oleg)
  73. @f1 = @model.fixture!(:oleg, {:xyz => 123})
  74. @f2 = @model.fixture!(:oleg, {:qwe => 345})
  75. end
  76. it "should return an unique fixture using fixture!(:name) with not overrides given" do
  77. @uf1.object_id.should == @uf2.object_id
  78. end
  79. it "should return a not unique fixture using fixture!(:name) with overrides given" do
  80. @f1.object_id.should_not == @f2.object_id
  81. end
  82. end # fixture!(:name)
  83.  
  84. end # regular
  85.  
  86. describe "with DSSV" do
  87. before(:each) do
  88. @model.dssv_fixtures(%{
  89. name autotype country
  90. spb St. Petersburg 5_000_000 proc { ("Rus" + "sia").to_sym }
  91. paris Paris, Île-de-France :symbol proc {%{France}}
  92. })
  93. end
  94.  
  95. it "should generate named fixtures" do
  96. spb = @model.fixture(:spb)
  97. spb.attrs.should == {:name => "St. Petersburg", :autotype => 5_000_000, :country => :Russia }
  98. paris = @model.fixture(:paris)
  99. paris.attrs.should == {:name => "Paris, Île-de-France", :autotype => :symbol, :country => "France" }
  100. end
  101.  
  102. describe "(malformed DSSV)" do
  103. it do
  104. lambda{ @model.dssv_fixtures(%{
  105. a b c
  106. tag1 a b c
  107. tag2 a b b2 c
  108. }) }.should raise_error
  109. end
  110. end
  111.  
  112. end # DSSV
  113.  
  114. describe "inherited fixtures" do
  115. before(:each) do
  116. @model2 = Class.new(@model){}
  117. @model3 = Class.new(@model2){}
  118.  
  119. @model.define_fixture(:base => true, :prop => 1)
  120. @model2.define_fixture(:child => true, :prop => 2)
  121. @model3.define_fixture(:grandchild => true, :prop => 3)
  122.  
  123. @model.define_fixture(:named, :named => :base, :named_prop => 1)
  124. @model2.define_fixture(:named, :named_prop => 2)
  125. @model3.define_fixture(:named, :named_prop => 3)
  126. end
  127.  
  128. it "should inherit default attributes" do
  129. @model2.fixture_attributes.should == {
  130. :base => true,
  131. :child => true,
  132. :prop => 2
  133. }
  134. @model3.fixture_attributes.should == {
  135. :base => true,
  136. :child => true,
  137. :grandchild => true,
  138. :prop => 3
  139. }
  140. end
  141.  
  142. it "should inherit named attributes" do
  143. @model2.fixture_attributes(:named).should == {
  144. :base => true,
  145. :child => true,
  146. :prop => 2,
  147. :named => :base,
  148. :named_prop => 2
  149. }
  150. @model3.fixture_attributes(:named).should == {
  151. :base => true,
  152. :child => true,
  153. :grandchild => true,
  154. :prop => 3,
  155. :named => :base,
  156. :named_prop => 3
  157. }
  158. end
  159. end # inherited fixtures
  160.  
  161. end # SimpleFixtures
Add Comment
Please, Sign In to add comment