# Looking for resources on how to use RSpec with Hotwire

**URL:** <https://discuss.hotwired.dev/t/looking-for-resources-on-how-to-use-rspec-with-hotwire/3284>\
**Category:** General\
**Created:** [September 26, 2021, 10:53pm UTC](https://discuss.hotwired.dev/t/looking-for-resources-on-how-to-use-rspec-with-hotwire/3284 "2021-09-26T22:53:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![WriterZephos](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/writerzephos/32/2192_2.png) [@WriterZephos](https://discuss.hotwired.dev/u/WriterZephos)\
**Post date:** [September 26, 2021, 10:53pm UTC](https://discuss.hotwired.dev/t/looking-for-resources-on-how-to-use-rspec-with-hotwire/3284/1 "2021-09-26T22:53:08Z")

</div>

Building an app with Hotwire for the first time. I think I finally wrapped my head around the unique way it forces you to structure view markup, but now I need to write some tests and I haven’t been able to find a guide on using RSpec with Hotwire.

Can anyone refer me to some good resources?

Thanks!

---

<div class="post-metadata">

**Author:** ![faqndo97](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/faqndo97/32/2241_2.png) [@faqndo97](https://discuss.hotwired.dev/u/faqndo97)\
**Post date:** [September 27, 2021, 3:56am UTC](https://discuss.hotwired.dev/t/looking-for-resources-on-how-to-use-rspec-with-hotwire/3284/2 "2021-09-27T03:56:55Z")

</div>

Good to know that you’re starting to use it!

First thing is separate the two scenarios:

1. Turbo frame  
Normally the important things to check on frames is that you’re not rendering the layout and that you’re returning the turbo\_frame.

```auto
expect(response).to have_http_status(:ok)
expect(response).to render_template(layout: false)
expect(response.body).to include('<turbo-frame id="frame_id">')

```

1. Turbo stream  
You can check the [turbo stream test helpers](https://github.com/hotwired/turbo-rails/blob/main/lib/turbo/test_assertions.rb) that they created on turbo-rails, and use those kind of checks on the specs of actions that respond to turbo\_streams.  
Here something like that for rspec:

```auto
expect(response).to have_http_status(:ok)
expect(response.media_type).to eq Mime[:turbo_stream]
expect(response).to render_template(layout: false)
expect(response.body).to include('<turbo-stream action="append" target="flash">')

```

And remember add the `, as: :turbo_stream` on the action when you execute it.

This is what I use most of the time to test turbo interactions with rspec

---

<div class="post-metadata">

**Author:** ![WriterZephos](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/writerzephos/32/2192_2.png) [@WriterZephos](https://discuss.hotwired.dev/u/WriterZephos)\
**Post date:** [September 27, 2021, 5:22am UTC](https://discuss.hotwired.dev/t/looking-for-resources-on-how-to-use-rspec-with-hotwire/3284/3 "2021-09-27T05:22:18Z")

</div>

Thank you for that clear, concise response. This is very helpful!

---

<div class="post-metadata">

**Author:** ![BalazsDenes](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/balazsdenes/32/2977_2.png) [@BalazsDenes](https://discuss.hotwired.dev/u/BalazsDenes)\
**Post date:** [November 6, 2022, 7:46am UTC](https://discuss.hotwired.dev/t/looking-for-resources-on-how-to-use-rspec-with-hotwire/3284/4 "2022-11-06T07:46:26Z")

</div>

> [@faqndo97](#):
>
> specs of actions

Hi, I am also starting on Hotwire and TDD and I’m having trouble implementing these examples.  
For Turbo frame, it works with this feature test with js: true:

```auto
# spec/features/clinics_select_spec.rb
feature 'when user clicks create_clinic btn', js: true do
  scenario 'turbo frame loads form' do
    visit root_path

    click_link "new_clinic"

    expect(page).to have_field('clinic_name') # field from new_clinic form
    expect(page).to have_field('clinics_select') # field from original page, making sure that turbo frame was used
  end
end

```

However this feels rather fragile and prone to changes in the future.  
For Turbo stream, I implemented a broadcast on clinics that updates the select tag with a clinic when added to database. This works in the browser: clinics\_select is updated if I add a clinic through the console. The test however doesn’t work:

```auto
# spec/features/clinics_select_spec.rb
feature 'a clinic is added', js: true do
  scenario 'and turbostream updates select' do
    clinic1 = create(:clinic, name:'clinic1')
    visit root_path

    clinic1 = create(:clinic, name:'clinic2')

    expect(page).to have_css 'option', text:'clinic1'
    expect(page).to have_css 'option', text:'clinic2'
  end
end

```

I get the error:

```auto
Failures:

  1) a clinic is added and turbostream updates select
     Failure/Error: expect(page).to have_css 'option', text:'clinic2'
       expected to find visible css "option" with text "clinic2" but there were no matches. Also found "clinic1", which matched the selector but not all filters. 
     # ./spec/features/appointments/clinics_select_spec.rb:28:in `block (2 levels) in <top (required)>'

```

I would welcome any comment on these specs as I have little experience.  
Also it would be great if you could complete the examples above with the setup and action parts.  
Thanks!
