[rspec-users] view_context in specs

David Chelimsky dchelimsky at gmail.com
Wed Nov 2 22:53:55 EDT 2011


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.


More information about the rspec-users mailing list