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 2011-11-03 03:49
on 2011-11-03 04:21
On Nov 2, 2011, at 9:46 PM, Patrick J. Collins wrote: > end > @blah = Blah.new(view_context) > it "is blaherrific" do > 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 2011-11-03 04:26
On Nov 2, 2011, at 9:52 PM, David Chelimsky wrote: >> def do_something_view_related >> >> describe Blah do >> > > 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.
on 2011-11-03 04:50
> > 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
on 2012-01-10 00:37
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.