test/errors_test.rb in reform-2.2.4 vs test/errors_test.rb in reform-2.3.0.rc1

- old
+ new

@@ -1,9 +1,13 @@ require "test_helper" +# TODO: +# This test should, at some point soon, only test the `Errors` object and its +# Rails-ish API. No validation specifics, etc. to be tested here. + class ErrorsTest < MiniTest::Spec - class AlbumForm < Reform::Form + class AlbumForm < TestForm property :title property :hit do property :title validation do @@ -27,27 +31,20 @@ end end # TODO: make band a required object. validation do - # required(:name).filled(:music_taste_ok?) - configure do - config.messages_file = "test/validation/errors.yml" + config.messages_file = "test/fixtures/dry_error_messages.yml" - def music_taste_ok?(value) + def good_musical_taste?(value) value != "Nickelback" - # errors.add(:base, "You are a bad person") if name == "Nickelback" end end - end - # validate :music_taste_ok? - # private - # def music_taste_ok? - # errors.add(:base, "You are a bad person") if name == "Nickelback" - # end + required(:name).filled(:good_musical_taste?) + end end validation do required(:title).filled end @@ -56,31 +53,36 @@ let (:album) do OpenStruct.new( :title => "Blackhawks Over Los Angeles", :hit => song, :songs => songs, # TODO: document this requirement, - - :band => Struct.new(:name, :label).new("Epitaph", OpenStruct.new), + :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) } - describe "incorrect #validate" do + describe "#errors without #validate" do + it do + form.errors.size.must_equal 0 + end + end + + describe "blank everywhere" do before { form.validate( "hit" =>{"title" => ""}, "title" => "", - "songs" => [{"title" => ""}, {"title" => ""}]) } # FIXME: what happens if item is missing? + "songs" => [{"title" => ""}, {"title" => ""}]) } # FIXME: what happens if item must be filled? it do form.errors.messages.must_equal({ :title => ["must be filled"], :"hit.title"=>["must be filled"], :"songs.title"=>["must be filled"], - :"band.label.name"=>["is missing"] + :"band.label.name"=>["must be filled"] }) end it do #form.errors.must_equal({:title => ["must be filled"]}) @@ -94,52 +96,58 @@ it do form.errors.messages.must_equal({ :title => ["must be filled"], :"hit.title" => ["must be filled"], :"songs.title"=> ["must be filled"], - :"band.label.name"=>["is missing"] + :"band.label.name"=>["must be filled"] }) + form.errors.size.must_equal(4) end end describe "#validate with main form invalid" do it do form.validate("title"=>"", "band"=>{"label"=>{:name => "Fat Wreck"}}).must_equal false form.errors.messages.must_equal({:title=>["must be filled"]}) + form.errors.size.must_equal(1) end end describe "#validate with middle nested form invalid" do before { @result = form.validate("hit"=>{"title" => ""}, "band"=>{"label"=>{:name => "Fat Wreck"}}) } it { @result.must_equal false } it { form.errors.messages.must_equal({:"hit.title"=>["must be filled"]}) } + it { form.errors.size.must_equal(1) } end 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"=>["must be filled"]}) } + it { form.errors.size.must_equal(1) } 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"=>["must be filled"], :"band.label.name"=>["is missing"]}) } + it { form.errors.messages.must_equal({:"songs.title"=>["must be filled"], :"band.label.name"=>["must be filled"]}) } + it { form.errors.size.must_equal(2) } end describe "#validate with nested form using :base invalid" do it do result = form.validate("songs"=>[{"title" => "Someday"}], "band" => {"name" => "Nickelback", "label" => {"name" => "Roadrunner Records"}}) result.must_equal false - form.errors.messages.must_equal({:"band.name"=>["You are a bad person"]}) + form.errors.messages.must_equal({:"band.name"=>["you're a bad person"]}) + form.errors.size.must_equal(1) end end describe "correct #validate" do before { @result = form.validate( @@ -151,15 +159,22 @@ it { @result.must_equal true } it { form.hit.title.must_equal "Sacrifice" } it { form.title.must_equal "Second Heat" } it { form.songs.first.title.must_equal "Heart Of A Lion" } + it do + skip "WE DON'T NEED COUNT AND EMPTY? ON THE CORE ERRORS OBJECT" + form.errors.size.must_equal(0) + form.errors.empty?.must_equal(true) + end end describe "Errors#to_s" do before { form.validate("songs"=>[{"title" => ""}], "band" => {"label" => {}}) } # to_s is aliased to messages - it { form.errors.to_s.must_equal "{:\"songs.title\"=>[\"must be filled\"], :\"band.label.name\"=>[\"is missing\"]}" } + it { + skip "why do we need Errors#to_s ?" + form.errors.to_s.must_equal "{:\"songs.title\"=>[\"must be filled\"], :\"band.label.name\"=>[\"must be filled\"]}" } end end