How do I test a reflex in StimulusReflex?

201 Views Asked by At

I created a small rails app using StimulusReflex and testing it with Rails' standard minitest framework.

I want to write a "controller" test that invokes a reflex and tests the controller response, but that seems to be impossible. Any hint on how to do this?

1

There are 1 best solutions below

1
On

There is a Gem you can add to help you with reflex testing stimulus_reflex_testing: https://github.com/podia/stimulus_reflex_testing

Then you will be able to have test such as this:

# app/reflexes/post_reflex.rb
class PostReflex < ApplicationReflex
  def find_post
    @post = Post.find(params[:id])
  end
end

reflex = build_reflex(method_name: :find_post, url: edit_post_url(post), params: { id: post.id })
reflex.run
reflex.get(:post) #=> returns the @post instance variable

The gem also includes some spec matchers. But people in the SR community mostly use system tests.