Testing a partial nested in a subfolder

Hi, completely new to testing views and templates. My first test!

The template I’d like to get at is in a subfolder within templates. Here’s the code:

content = render_to_string(MyAppWeb.View, "template.html", conn: conn)

Get this error:

Could not render "template.html" for MyAppWeb.View, please define a matching clause for render/2 or define a template at "lib/myapp_web/templates/template"

:wave:

What’s in MyAppWeb.View and where exactly did you place the template?

The template is here:
lib > myapp_web > templates > subfolder > template.html.eex

The view contains:
use MyAppWeb, :view

The myapp_web.ex contains:

use Phoenix view, root: “lib/myapp_web/templates”,
namespace: MyAppWeb,
pattern: “**/*”

I thought there must be some simple standard way of testing partial templates. I’ve googled and couldn’t find any resources and I’ve got the latest beta of the Phoenix book too!

I think you need to have either an additional view module MyAppWeb.SubFolder or a custom render clause in some other view.

Yeah, looks like that’s correct. Pushed past that error now.
Something like:
content = render_to_string(MyAppWeb.MainView(not the partial!), "partial_template", conn: conn)

You could also edit the search pattern for views: https://hexdocs.pm/phoenix/1.3.4/Phoenix.View.html#__using__/1-options

1 Like