Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require File.join(File.dirname(__FILE__), "spec_helper")
- describe SimpleFixtures do
- before(:each) do
- @model = Class.new do
- attr_accessor :attrs, :blk
- def initialize(attrs, &blk)
- @attrs = attrs
- @blk = blk
- end
- def save
- @saved = true
- end
- def saved?
- @saved
- end
- end
- @model.extend(SimpleFixtures)
- end
- describe "regular named fixtures" do
- before(:each) do
- model = @model
- @model.define_fixture( :name => "Person", :friend => proc { model.fixture!(:with_block) } )
- @model.define_fixture(:oleg, :name => "Oleg", :friend => proc { model.fixture!(:antares) } )
- @model.define_fixture(:antares, proc {{:name => "MK"}})
- @model.define_fixture(:with_block) do |overrides| # with block, but without a friend
- overrides.merge(:with_block => :i_am_over_it, :friend => nil)
- end
- end
- it "should have named fixtures" do
- @model.fixtures[:__default__].should_not be_empty
- @model.fixtures[:oleg].should_not be_empty
- @model.fixtures[:antares].should respond_to(:call)
- @model.fixtures[:with_block].should respond_to(:call)
- end
- it "should generate a named and default fixtures using Model#fixture and #fixture!" do
- f = @model.fixture(&@some_block)
- f.should_not be_saved
- f.attrs[:friend].should be_saved
- f.attrs[:name].should == "Person"
- f.attrs[:friend].should be_kind_of(@model)
- f.attrs[:friend].attrs[:name].should == "Person"
- f.attrs[:friend].attrs[:with_block].should == :i_am_over_it
- f.attrs[:friend].attrs[:friend].should == nil
- end
- describe "unique_fixture!(:name, overrides = {})" do
- before(:each) do
- @uf1 = @model.unique_fixture!(:oleg, {:xyz => 123})
- @uf2 = @model.unique_fixture!(:oleg, {:qwe => 345})
- end
- it "should return equal unique fixtures" do
- @uf1.object_id.should == @uf2.object_id
- end
- it "should set attributes provided first time" do
- @uf2.attrs[:xyz].should == 123
- @uf2.attrs[:qwe].should be_nil
- end
- end # unique_fixture!(:name, overrides)
- describe "fixture!(:name)" do
- before(:each) do
- @uf1 = @model.fixture!(:oleg)
- @uf2 = @model.fixture!(:oleg)
- @f1 = @model.fixture!(:oleg, {:xyz => 123})
- @f2 = @model.fixture!(:oleg, {:qwe => 345})
- end
- it "should return an unique fixture using fixture!(:name) with not overrides given" do
- @uf1.object_id.should == @uf2.object_id
- end
- it "should return a not unique fixture using fixture!(:name) with overrides given" do
- @f1.object_id.should_not == @f2.object_id
- end
- end # fixture!(:name)
- end # regular
- describe "with DSSV" do
- before(:each) do
- @model.dssv_fixtures(%{
- name autotype country
- spb St. Petersburg 5_000_000 proc { ("Rus" + "sia").to_sym }
- paris Paris, Île-de-France :symbol proc {%{France}}
- })
- end
- it "should generate named fixtures" do
- spb = @model.fixture(:spb)
- spb.attrs.should == {:name => "St. Petersburg", :autotype => 5_000_000, :country => :Russia }
- paris = @model.fixture(:paris)
- paris.attrs.should == {:name => "Paris, Île-de-France", :autotype => :symbol, :country => "France" }
- end
- describe "(malformed DSSV)" do
- it do
- lambda{ @model.dssv_fixtures(%{
- a b c
- tag1 a b c
- tag2 a b b2 c
- }) }.should raise_error
- end
- end
- end # DSSV
- describe "inherited fixtures" do
- before(:each) do
- @model2 = Class.new(@model){}
- @model3 = Class.new(@model2){}
- @model.define_fixture(:base => true, :prop => 1)
- @model2.define_fixture(:child => true, :prop => 2)
- @model3.define_fixture(:grandchild => true, :prop => 3)
- @model.define_fixture(:named, :named => :base, :named_prop => 1)
- @model2.define_fixture(:named, :named_prop => 2)
- @model3.define_fixture(:named, :named_prop => 3)
- end
- it "should inherit default attributes" do
- @model2.fixture_attributes.should == {
- :base => true,
- :child => true,
- :prop => 2
- }
- @model3.fixture_attributes.should == {
- :base => true,
- :child => true,
- :grandchild => true,
- :prop => 3
- }
- end
- it "should inherit named attributes" do
- @model2.fixture_attributes(:named).should == {
- :base => true,
- :child => true,
- :prop => 2,
- :named => :base,
- :named_prop => 2
- }
- @model3.fixture_attributes(:named).should == {
- :base => true,
- :child => true,
- :grandchild => true,
- :prop => 3,
- :named => :base,
- :named_prop => 3
- }
- end
- end # inherited fixtures
- end # SimpleFixtures
Add Comment
Please, Sign In to add comment