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.
Trending in Questions
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
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
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
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hauleth
Why testing such implementation? I always test only results, as it doesn’t matter how that result was achieved internally.
silverdr
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
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
For an example imagine users with different access levels/roles. If the differences are subtle like “show this
divinstead 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 ratherrender()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
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
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
So you don’t care about the rendering result, just there are some results? I think you can achieve this with 2 unit tests:
silverdr
I see. I was hoping for something similar to the rspec’s render matcher. Possibly as an additional dependency for test environment.
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 withFlokiorMeeseeksand assert that the appropriatedata-*HTML attribute is present in the resulting markup.silverdr
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
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
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…