I have a helper method def partial_path_for(partial, options = {}) base_path = options[:base_path] || self.template.base_path "_partials/#{base_path}/#{partial}" end and use it in a template (haml) like = render :partial => partial_path_for(:details) I''d like to have a similar helper for view specs so I can do this describe ''web/admin/merchants/show.html.haml'' do before(:each) do @merchant = stub_model(Merchant, :name => ''Some Merchant'') assigns[:merchant] = @merchant end it ''should render the details partial'' do partial_path = partial_path_for(:details, :base_path => self.template.base_path) template.should_receive(:render).with(:partial => partial_path) render end it_should_behave_like ''merchant details'' end Unfortunately, if I debug right before the template.should_receive statement, self.template.base_path is nil. How can I get the path to the template? Thanks, Phillip
On 2010-03-25 1:42 PM, Phillip Koebbe wrote:> I have a helper method > > def partial_path_for(partial, options = {}) > base_path = options[:base_path] || self.template.base_path > "_partials/#{base_path}/#{partial}" > end > > and use it in a template (haml) like > > = render :partial => partial_path_for(:details) > > I''d like to have a similar helper for view specs so I can do this > > describe ''web/admin/merchants/show.html.haml'' do > before(:each) do > @merchant = stub_model(Merchant, :name => ''Some Merchant'') > assigns[:merchant] = @merchant > end > > it ''should render the details partial'' do > partial_path = partial_path_for(:details, :base_path => > self.template.base_path) > template.should_receive(:render).with(:partial => partial_path) > render > end > > it_should_behave_like ''merchant details'' > end > > Unfortunately, if I debug right before the template.should_receive > statement, self.template.base_path is nil. How can I get the path to > the template? >Right after I sent that message, I discovered I could set a variable before the initial describe statement and reuse it. This works: base_path = ''web/admin/merchants'' describe "#{base_path}/show.html.haml" do ... partial_path = call_some_method(:details, base_path) ... end But I''d really rather have the value coming from somewhere else. This method depends on me creating a variable, which I don''t like. Peace, Phillip> Thanks, > Phillip
On Mar 25, 1:50?pm, Phillip Koebbe <phillipkoe... at gmail.com> wrote:> Right after I sent that message, I discovered I could set a variable > before the initial describe statement and reuse it. This works: > > base_path = ''web/admin/merchants'' > describe "#{base_path}/show.html.haml" do > ? ? ?... > ? ? ?partial_path = call_some_method(:details, base_path) > ? ? ?... > end > > But I''d really rather have the value coming from somewhere else. This > method depends on me creating a variable, which I don''t like.On Mar 25, 1:50 pm, Phillip Koebbe <phillipkoe... at gmail.com> wrote:> Right after I sent that message, I discovered I could set a variable > before the initial describe statement and reuse it. This works: > > base_path = ''web/admin/merchants'' > describe "#{base_path}/show.html.haml" do > ... > partial_path = call_some_method(:details, base_path) > ... > end > > But I''d really rather have the value coming from somewhere else. This > method depends on me creating a variable, which I don''t like.I''ve been looking over the available methods and I discovered that I can get the base path via File.dirname(self.description) That got me thinking about various things, and I eventually wound up adding before {ensure_that_base_view_path_is_set_correctly} def ensure_that_base_view_path_is_set_correctly @controller.template.base_path File.dirname(self.class.description) end to lib/spec/rails/example/view_example_group.rb This accomplishes what I want, but I seriously doubt it is the right thing to do, so I created a ticket: https://rspec.lighthouseapp.com/projects/5645-rspec/tickets/973-templatebase_path-returns-nil-in-view-spec Phillip