ruby on rails - Rspec Test Haml Template Output in Object Presenter -


i trying figure out class/method/instantiation process should use in rspec functioning view template includes both standard actionview herlper methods , haml helper methods.

i have implemented presenters in rails app done in railscast #287

i have presenter want output uses 1 of haml helpers.

here base presenter:

class basepresenter   attr_reader :object, :template    def initialize(object, template)     @object = object     @template = template   end end 

here example of theoretical presentable object , it's presenter class:

class fakeobject   def a_number     rand(10).to_s   end end  class fakepresenter < basepresenter   def output_stuff     # #content_tag standard actionview     template.content_tag(:span)       # #succeed haml gem       template.succeed('.')         object.a_number       end     end             end end 

if write test in rspec , #content_tag method being used, work, it's part of actionview. however, #succeed haml gem , isn't available current test.

test:

require 'rails_helper'  describe fakeobjectpresenter    let(:template){ actionview::base.new }   let(:obj){ fakeobject.new }   let(:presenter){ fakeobjectpresenterpresenter.new obj, template }    describe '#output_stuff'     it{ expect(presenter.output_stuff).to match(/<span>\d+<\/span>/) }   end end 

failures:

  1) fakeobjectpresenter #output_stuff       failure/error:        template.succeed('.')          object.a_number       nomethoderror:        undefined method `succeed' #<actionview::base:0x00560e818125a0> 

what class should instantiating template instead of actionview::base.new haml methods available in test?

if you'd play around this, can dump below code spec.rb file in rails app , run demo:

require 'rails_helper'  # define classes tested class fakeobject   def a_number     rand(10).to_s   end end  class basepresenter   attr_reader :object, :template    def initialize(object, template)     @object = object     @template = template   end end  class fakeobjectpresenter < basepresenter   def output_stuff     # #content_tag standard actionview     template.content_tag(:span)       # #succeed haml gem       template.succeed('.')         object.a_number       end     end             end end  # actual spec describe fakeobjectpresenter    let(:template){ actionview::base.new }   let(:obj){ fakeobject.new }   let(:presenter){ fakeobjectpresenter.new obj, template }    describe '#output_stuff'     it{ expect(presenter.output_stuff).to match(/<span>\d+<\/span>/) }   end end 


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -