Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec view tests can't find partials that are in base namespace

EDIT:

I found that if I insert view.lookup_context.prefixes = %w[base] before render, the test knows the proper path. Is this the best/proper way of solving this?


I put all my partials in a base folder, and all the controller that have access to those partials inherit from the base_controller this all works great, but the generated view tests can't find the partials that are in the base folder.

Here is the error:

Failure/Error: render
     ActionView::Template::Error:
       Missing partial /admin_menu, circuits/admin_menu with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :png, :jpeg, :gif, :bmp, :tiff, :mpeg, :xml, :rss, :atom, :yaml, :multipart_form, :url_encoded_form, :json, :pdf, :zip], :handlers=>[:erb, :builder, :coffee, :jbuilder]}. Searched in:
         * "/Users/Mac/Folder/ProjectName/app/views"

How do I tell my view test where to look for the partials?

for completeness, here is the spec:

require 'spec_helper'

describe "circuits/index" do
  before(:each) do
    assign(:circuits, [
      stub_model(Circuit,
        :name => "Name",
        :description => "MyText"
      ),
      stub_model(Circuit,
        :name => "Name",
        :description => "MyText"
      )
    ])
  end

  it "renders a list of circuits" do
    view.lookup_context.prefixes = %w[base application]
    render
    assert_select "tr>td", :text => "Name".to_s, :count => 2
    assert_select "tr>td", :text => "MyText".to_s, :count => 2
  end
end
like image 715
Arel Avatar asked Oct 09 '13 20:10

Arel


2 Answers

I had a similar issue when using partial inheritance to add themes feature to my app. For Rails 4 it was a bit different than in the edited answer - I had to add this in my view specs:

before do
  view.lookup_context.view_paths.push 'app/views/themes/light'
end

where the themes/light folder contained some partials specific to the light theme.

like image 107
Matt Avatar answered Oct 21 '22 01:10

Matt


You should use

<%= render 'base/admin_menu' %>
like image 23
Jeremy Green Avatar answered Oct 21 '22 02:10

Jeremy Green