test/errors_test.rb in reform-0.2.7 vs test/errors_test.rb in reform-1.0.0

- old
+ new

@@ -12,18 +12,28 @@ collection :songs do property :title validates :title, :presence => true end + property :band do # yepp, people do crazy stuff like that. + property :label do + property :name + validates :name, :presence => true + end + # TODO: make band a required object. + end + validates :title, :presence => true end let (:album) do OpenStruct.new( :title => "Blackhawks Over Los Angeles", :hit => song, - :songs => songs # TODO: document this requirement + :songs => songs, # TODO: document this requirement, + + :band => Struct.new(:name, :label).new("Epitaph", OpenStruct.new), ) end let (:song) { OpenStruct.new(:title => "Downtown") } let (:songs) { [song=OpenStruct.new(:title => "Calling"), song] } let (:form) { AlbumForm.new(album) } @@ -37,11 +47,13 @@ it do form.errors.messages.must_equal({ :title => ["can't be blank"], :"hit.title"=>["can't be blank"], - :"songs.title"=>["can't be blank"]}) + :"songs.title"=>["can't be blank"], + :"band.label.name"=>["can't be blank"] + }) end it do #form.errors.must_equal({:title => ["can't be blank"]}) # TODO: this should only contain local errors? @@ -53,39 +65,54 @@ it do form.errors.messages.must_equal({ :title => ["can't be blank"], :"hit.title" => ["can't be blank"], - :"songs.title"=> ["can't be blank"]}) - end # TODO: add another invalid item. + :"songs.title"=> ["can't be blank"], + :"band.label.name"=>["can't be blank"] + }) + end end + describe "#validate with main form invalid" do - before { @result = form.validate("title"=>"") } + before { @result = form.validate("title"=>"", "band"=>{"label"=>{:name => "Fat Wreck"}}) } it { @result.must_equal false } it { form.errors.messages.must_equal({:title=>["can't be blank"]}) } end + describe "#validate with middle nested form invalid" do - before { @result = form.validate("hit"=>{"title" => ""}) } + before { @result = form.validate("hit"=>{"title" => ""}, "band"=>{"label"=>{:name => "Fat Wreck"}}) } it { @result.must_equal false } it { form.errors.messages.must_equal({:"hit.title"=>["can't be blank"]}) } end - describe "#validate with last nested form invalid" do - before { @result = form.validate("songs"=>[{"title" => ""}]) } + describe "#validate with collection form invalid" do + before { @result = form.validate("songs"=>[{"title" => ""}], "band"=>{"label"=>{:name => "Fat Wreck"}}) } + it { @result.must_equal false } - it { form.errors.messages.must_equal({:"songs.title"=>["can't be blank"]}) } + it( "xxxx") { form.errors.messages.must_equal({:"songs.title"=>["can't be blank"]}) } end + + describe "#validate with collection and 2-level-nested invalid" do + before { @result = form.validate("songs"=>[{"title" => ""}], "band" => {"label" => {}}) } + + it { @result.must_equal false } + it { form.errors.messages.must_equal({:"songs.title"=>["can't be blank"], :"band.label.name"=>["can't be blank"]}) } + end + + describe "correct #validate" do before { @result = form.validate( "hit" => {"title" => "Sacrifice"}, "title" => "Second Heat", - "songs" => [{"title"=>"Heart Of A Lion"}] + "songs" => [{"title"=>"Heart Of A Lion"}], + "band" => {"label"=>{:name => "Fat Wreck"}} ) } it { @result.must_equal true } it { form.hit.title.must_equal "Sacrifice" } it { form.title.must_equal "Second Heat" } \ No newline at end of file