require 'rspec_ext' require "#{__FILE__.dirname}/helper" describe "ModelHelper" do class MockModelHelperContext < Crystal::MockTemplateContext inherit Crystal::FormHelper end before :each do @t = MockModelHelperContext.new end it "form_for" do @t.form_for(:aname, nil) @t.buffer.should == "
" end it "error_messages" do @t.form_for(:aname, nil){|f| f.error_messages} @t.buffer.should == "
" model = {:errors => {:base => 'some error'}} @t.buffer = "" @t.form_for(:book, model){|f| f.error_messages} @t.buffer.should == %{
some error
} end it "field error" do model = { :title => "Super Hero", :errors => {:title => 'some error in title'} } @t.form_for(:book, model){|f| f.text_field(:title)} doc = @t.buffer.to_xhtml doc.css("form div.field_error_messages").first.content.should == "some error in title" doc.css("form span.field_with_errors input").first.should_be_equal_to :name => "book[title]", :value => "Super Hero" end it "field_helpers" do model = { :available => false, :title => "Super Hero" } @t.form_for(:book, model){|f| %{ #{f.check_box :available} #{f.file_field :title} #{f.hidden_field :title} #{f.password_field :title} #{f.radio_button :available} #{f.submit 'Ok'} #{f.text_field :title} #{f.text_area :title} } } doc = @t.buffer.to_xhtml doc.css("*[type='checkbox']").first.should_be_equal_to :name => "book[available]", :value => '1' doc.css("*[type='file']").first.should_be_equal_to :name => "book[title]" doc.css("*[type='hidden']").first.should_be_equal_to :name => "book[title]", :value => 'Super Hero' doc.css("*[type='password']").first.should_be_equal_to :name => "book[title]" doc.css("*[type='radio']").first.should_be_equal_to :name => "book[available]", :value => '1' doc.css("*[type='submit']").first.should_be_equal_to :value => 'Ok' doc.css("*[type='text']").first.should_be_equal_to :name => "book[title]", :value => 'Super Hero' doc.css("textarea").first.should_be_equal_to :name => "book[title]", :content => 'Super Hero' end end