Ask A Question

Notifications

Youโ€™re not receiving notifications from this thread.

How to write System Tests in Rails Discussion

Really loving this testing series. I've been using RSpec for years but it looks like Minitest now covers a lot of the functionality that made me choose RSpec back then.

One of the things I don't like about RSpec is the amount of setup and gems required. Going with the built-in testing framework avoids all that, which is a definite win for beginners and quick-starting projects.

Be great to see your comparison of Minitest and RSpec.

How about headless browsers for system tests? I see you can switch to :headless_chrome but do you still get screenshots with that? Also, is a headless browser any faster?

Reply

Yeah, one of the reasons I choose to use Minitest over Rspec is that it's mostly just pure Ruby. You have a couple wrappers around assert, but it's really just Ruby code. Less magic means it's more reliable to reason about.

Headless browsers I do believe can still generate screenshots. I will try it to make sure. The main reason you need a headless browser is on your server, you don't have a screen, so there's no windows to actually render. It's all virtual.

Reply

Headless browsers do work with screenshots. I use them for development on Cloud9. I have my Rails students use Cloud9 becaue a lot of them have Windows machines, and I don't want to deal with trying to get Rails installed on Windows.

Reply

My 2 cents: there's a way to display screenshots directly in iTerm2 (and likely in others terminals too): https://www.iterm2.com/documentation-images.html

I found it very useful to see what's going on when tests fail without having to saving or opening any file :)

Reply

Nice! ๐Ÿ™Œ

Reply

iTerm is so dope, thanks for sharing that! ๐Ÿ™

Reply

Great lesson.

At 11:50 you show a bootstrap modal for confirmations. How do you build that to replace the browser modal that is by default generated for a typical delete button?

Reply

That's actually using the data-confirm-modal gem to use Bootstrap instead. It's kind of hacky how it replaces the confirm method in Rails JS, but you can check out the source here. https://github.com/ifad/data-confirm-modal

Reply

Would you recommend using this method to test Vue in a Rails app?

Reply

Yeah, it'd be a great way to test and make sure your Vue app was integrated correctly with the Rails app. You can also look into things like Jest to test just the JS independently, without Rails.

Reply

In the gem file you have included capybara, how much is that used here and how much is just minitest?

--

Before posting I did some looking : )

System tests allow you to test user interactions with your application, running tests in either a real or a headless browser. System tests use Capybara under the hood.
(https://guides.rubyonrails.org/testing.html#system-testing)

..and the default install of a new rails app comes with capybara etc set up in gemfile and application_system_test.rb etc.

Is there a good, focused cheatsheet / resource with all the main methods available when using capybara with minitest in rails?

Reply

All the methods can be found in their docs: https://github.com/teamcapybara/capybara#the-dsl

Reply

I think it should be mentioned that system tests use whatever data you have already seeded into your local environment instead of your fixtures, which is what the controller tests use. This confused me at first. I was testing for the presence of fixture data, LOL.

Reply

I don't think that's right. We're using fixtures in this episode in our system tests. We're signing in as fixture users and visiting project fixtures for example.

Reply

Yeah, weird. I looked at the video again after posting that, and checked that I have the :fixtures and everything else set up just as you do, but my system tests are definitely pulling my local data.

Reply

2.54 "you should use url_helpers over path_helpers" - the Rails guide examples use path_helpers and url_helpers. Are they mistaken? https://guides.rubyonrails.org/testing.html#implementing-a-system-test

Reply

Issue to driver can be rolved by adding gem 'webdrivers', '~> 4.0' in gemfile

Reply

7th video is same as the 6th please check.

Reply

For anyone using i18n you can do something like this:

require "application_system_test_case"

class AccountsTest < ApplicationSystemTestCase
  test "Can register for an account" do
    visit new_account_registration_path

    assert_difference("Account.count") do
      fill_in I18n.t("rnew.name"), with: "Tom Hanks"
      fill_in I18n.t("rnew.email"), with: "thanks@gmail.com"
      fill_in I18n.t("rnew.company"), with: "Cast Away"
      fill_in I18n.t("rnew.password"), with: "password850$"
      fill_in I18n.t("rnew.password_confirm"), with: "password850$"

      click_button I18n.t("rnew.register")
    end
  end
end
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 81,842+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.