[rspec-users] view_context in specs

Patrick J. Collins patrick at collinatorstudios.com
Wed Nov 2 22:46:12 EDT 2011


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


More information about the rspec-users mailing list