silverdr

silverdr

Is there an easy method of testing whether a particular template was rendered / passed to render. The guides I read all say about matching output strings as testing method for views/templates. I am looking for a way to test if the expected template is rendered, without taking into account or even knowing what its content is. Suggestions appreciated.

Showing Posts 1 to 10

hauleth

hauleth

Why testing such implementation? I always test only results, as it doesn’t matter how that result was achieved internally.

silverdr

silverdr OP

Because I want to test the logic, which renders different templates, according to internal rules/conditions. I don’t want to test the [current] content of template files. Templates’ content may evolve, be changed, renamed, reworded, adapted, … and I still want the logic tests to work rather than return bogus errors.

hauleth

hauleth

If the views can change, then what gonna happen if you change the source view and it will conditionally render different views? Show us a little more context and code (especially context) and maybe there is better solution. For example force to render parts with known content.

silverdr

silverdr OP

For an example imagine users with different access levels/roles. If the differences are subtle like “show this div instead of that one” then I have nothing against putting this kind of logic into the template. If OTOH those users are expected to have very different pages rendered for the same action then, especially with large templates with lots of elements, I would hate myself for shovelling everything into one template. In such cases I rather render() a template appropriate for given user, based on his access/role attributes. And for such cases I always test that my logic works, regardless of what the template contains. In the non-Phoenix world my main tool for this is:

https://relishapp.com/rspec/rspec-rails/docs/matchers/render-template-matcher

First two examples there. Another potential case: user with not activated account should get a different home-page than a holder of a fully active account. Sure I could e. g. redirect him to another action but it’s much more effective to simply render a different template. The rest the same as above.

derek-zhou

derek-zhou

Then you can abstract out the internal rules/conditions into a separate module and write unit test on it, right? If you don’t care about the template content why do you want to get the rendering involved in the unit test in the first place?

silverdr

silverdr OP

Because the result (regardless of the implementation) is that one or another template gets rendered. And with what I linked above this is very easy to test. And I don’t need to render the actual content. OTOH what the guides say is that the rendering should be involved (render_to_string() and match the output). For me it is enough to test what template was passed to be rendered, without caring what’s inside said template. Again - the examples in linked rspec doc show how to do exactly that.

derek-zhou

derek-zhou

So you don’t care about the rendering result, just there are some results? I think you can achieve this with 2 unit tests:

  • the one I mentioned above, ie, the picking of the the template is according to a spec
  • make a list of all legal templates, and render them one by one, This is to make sure that there is no missing template
silverdr

silverdr OP

I see. I was hoping for something similar to the rspec’s render matcher. Possibly as an additional dependency for test environment.

dimitarvp

dimitarvp

I see where you are coming from but I’d be very averse to approach the testing like you – that already sounds too much like browser automation testing to me, which is hard enough to do already.

One potential hack might be to have very specific data-* attributes of your elements in the different templates so you can just parse your rendered results with Floki or Meeseeks and assert that the appropriate data-* HTML attribute is present in the resulting markup.

silverdr

silverdr OP

I believe you misunderstood the intent. I also see close to no resemblance to automated browser testing. Another non-Elixir approach to this problem would be to mock the rendering entity and expect it to receive a message (get called) with appropriate parameters:

https://relishapp.com/rspec/rspec-mocks/docs

in “Message Expectations” paragraph. Maybe that’s something that can be done easier in Elixir/Phoenix?

So far the only somewhat viable approach seems to be the one suggested by @derek-zhou , but.. extracting the basically non-reusable logic into a separate module rather than testing it in its environment in-place seems more like a costly workaround than an elegant solution.

Right - it’s a hack :wink: And if I have to go the hacky way then there’s plenty of options. I could put even specific markup comments with “don’t remove” messages :wink: But this feels all wrong. The templates should IMHO not be concerned about the selection logic at all. The less should they be coupled to the way the selection logic is tested. Which is why I started this thread in the first place…

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews