I have a presenter class which is instantiated like this: class Blah def initialize(context) @context = context end def do_something_view_related @context.render :partial => "/...somewhere" end def do_something_else_view_related @context.content_tag :p, "fancy paragraph" end end class BlahController < ApplicationController def blah @blah = Blah.new(view_context) end end ... I''ve gotten around this in my specs by doing something like: describe Blah do it "is blaherrific" do context = stub(:render => "some content...", :link_to => "<a href="www.somewhere.com">somewhere</a>) blah = Blah.new(context) blah.do_someting_view_related.should == "some content..." end end But I would much rather actually be able to call upon the real view context in my specs so that my tests are more realistic. Is the best way to get a real-world view context in there to do something like: Blah.new(ActionView::Base.new) ? Or does RSpec have something magical already setup for this sort of thing? Muchas Gracias. Patrick J. Collins http://collinatorstudios.com
On Nov 2, 2011, at 9:46 PM, Patrick J. Collins wrote:> I have a presenter class which is instantiated like this: > > class Blah > > def initialize(context) > @context = context > end > > def do_something_view_related > @context.render :partial => "/...somewhere" > end > > def do_something_else_view_related > @context.content_tag :p, "fancy paragraph" > end > > end > > class BlahController < ApplicationController > > def blah > @blah = Blah.new(view_context) > end > > end > > ... > > I''ve gotten around this in my specs by doing something like: > > describe Blah do > > it "is blaherrific" do > context = stub(:render => "some content...", :link_to => "<a > href="www.somewhere.com">somewhere</a>) > blah = Blah.new(context) > > blah.do_someting_view_related.should == "some content..." > end > > end > > But I would much rather actually be able to call upon the real view context in > my specs so that my tests are more realistic. > > Is the best way to get a real-world view context in there to do something like: > > Blah.new(ActionView::Base.new) ? > > Or does RSpec have something magical already setup for this sort of thing?Nope. rspec-rails doesn''t know that you want to write presenters :) I''d say just go w/ the real deal. HTH, David
On Nov 2, 2011, at 9:52 PM, David Chelimsky wrote:> On Nov 2, 2011, at 9:46 PM, Patrick J. Collins wrote: > >> I have a presenter class which is instantiated like this: >> >> class Blah >> >> def initialize(context) >> @context = context >> end >> >> def do_something_view_related >> @context.render :partial => "/...somewhere" >> end >> >> def do_something_else_view_related >> @context.content_tag :p, "fancy paragraph" >> end >> >> end >> >> class BlahController < ApplicationController >> >> def blah >> @blah = Blah.new(view_context) >> end >> >> end >> >> ... >> >> I''ve gotten around this in my specs by doing something like: >> >> describe Blah do >> >> it "is blaherrific" do >> context = stub(:render => "some content...", :link_to => "<a >> href="www.somewhere.com">somewhere</a>) >> blah = Blah.new(context) >> >> blah.do_someting_view_related.should == "some content..." >> end >> >> end >> >> But I would much rather actually be able to call upon the real view context in >> my specs so that my tests are more realistic. >> >> Is the best way to get a real-world view context in there to do something like: >> >> Blah.new(ActionView::Base.new) ? >> >> Or does RSpec have something magical already setup for this sort of thing? > > Nope. rspec-rails doesn''t know that you want to write presenters :) > > I''d say just go w/ the real deal.I should qualify that: I''d say just _start_ with the real deal. If it turns out painful, then look for alternatives.
> > Is the best way to get a real-world view context in there to do something like: > > > > Blah.new(ActionView::Base.new) ? > > > > Or does RSpec have something magical already setup for this sort of thing? > > Nope. rspec-rails doesn''t know that you want to write presenters :) > > I''d say just go w/ the real deal.Ok cool, thanks. It turns out ActionView::Base.new is a bit of a pain to try to use in this case since it lacks configuration of view paths and such. So what appears to be the simplest way to get this functionality is to do: @context = ActionController::Base.new.view_context then you can do @context.render, @context.content_tag, etc. Patrick J. Collins http://collinatorstudios.com
If you put this on your spec_helper.rb //If you have your spec in the directory "spec/presenters" config.include ActionView::TestCase::Behavior, example_group: {file_path: %r{spec/presenters}} Then you can use a method, :view, that you can use as the view context like this let(:profile) { ProfilePresenter.new(@user, view) } ....... There is a Railscast about it. -- Posted via http://www.ruby-forum.com/.