spec/models/stachio/template_spec.rb in stachio-0.0.2 vs spec/models/stachio/template_spec.rb in stachio-0.0.3

- old
+ new

@@ -1,22 +1,18 @@ require 'spec_helper' module Stachio - class MockProxyObject - def teapot - "TEAPOT" + describe Template do + let(:presents) do + Class.new { def self.teapot; 'TEAPOT'; end } end - end - describe Template do let(:template) do - attrs = { :template_name => "my silly teapot", - :content => "I am a {{teapot}}", - } - template = Template.new(attrs) - template.proxied = MockProxyObject.new - template + tmpl = Template.new :template_name => "mrs.potts", + :content => "I am a {{teapot}}" + tmpl.presents = presents + tmpl end it "stores templates in the database" do template.save!.should be_true Template.count.should eql(1) @@ -37,18 +33,58 @@ template.should be_valid end it "fetches templates from the database using the template name" do template.save! - template.should == Template['my silly teapot'] + template.should == Template['mrs.potts'] end - it "composes messages using the content of the template" do - template.compose.should == "I am a TEAPOT" - end - it "reports an error when it can't compose the message" do template.content = "I am not a valid {{horseradish}}" - expect { template.compose }.to raise_error(Mustache::ContextMiss) + expect { template.render }.to raise_error(Mustache::ContextMiss) end + + describe '#render' do + it "renders using the content of the template" do + template.presents.should be_a_kind_of(Class) + template.render.should == "I am a TEAPOT" + end + + it "can render values from a hash" do + template.presents = {:teapot => 'TEAPOT' } + template.render.should == "I am a TEAPOT" + end + + it "renders nothing when presenting a nil" do + template.presents = nil + template.render.should == nil + end + + it "memoizes rendered text" do + template.render.should == "I am a TEAPOT" + template.rendered.should == "I am a TEAPOT" + + template.presents = {:teapot => 'HORSERADISH'} + + template.render.should == "I am a TEAPOT" + template.rendered.should == "I am a TEAPOT" + end + + it "can be reset when memoized" do + template.render.should == "I am a TEAPOT" + template.presents = {:teapot => 'HORSERADISH'} + + template.render.should == "I am a TEAPOT" + template.render(:force => true).should == "I am a HORSERADISH" + end + end + + describe '#present' do + it "sets attribute 'presents' & renders all at once" do + template.render.should == "I am a TEAPOT" + template.present(:teapot => 'TURNIP').should == "I am a TURNIP" + template.presents.should == {:teapot => 'TURNIP'} + end + end + end end