1
votes

I am new to Ruby & Rails and basically come from Java background hence I apologize in advance for my limited knowledge of RoR.

Ruby version = 2.1

Rails version = 4.2

While working on an existing Rails 4 application I came across the following test written in one of the test files. This test is trying to test the login page of the application but I am puzzled by following questions:

  1. Is MiniTest used for just unit testing or integration testing or both?
  2. Is this test actually a MiniTest test or is it just importing existing default Ruby test framework in the helper file and not actually using any of the MiniTest features?
  3. Can MiniTest actually test DOM elements such as input text in user input boxes, do button clicks etc?
  4. Is it actually hitting the application by starting the server internally?
  5. Can MiniTest alone be used to test a Rails application end-to end by running in headless mode?
  6. Can MiniTest alone be used to test a Rails application end-to end by launching a browser through selenium? I know Capybara can do this.
  7. I guess this test is running against localhost. How can I make this test point to a different host such www.dev-server.com?

login_test.rb

require 'integration_test_helper'
class LoginTest < ActionDispatch::IntegrationTest

  setup do
    @user = users(:admin)
  end

  test "login page is displayed on root path" do
    delete '/signout'
    get root_path
    assert redirect?
    assert_redirected_to '/signin'
    follow_redirect!
    assert_template :new
    assert_template %r{\Adevise/sessions/new\Z}
    assert_select "a[href=?]", users_path, count: 0
    assert_select "input[id=?]", 'login-email', count: 1
    assert_select "input[id=?]", 'login-password', count: 1
    assert_select "button[id=?]", 'login', count: 1
  end

end

integration_test_helper.rb

ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "minitest/reporters"
Minitest::Reporters.use! [Minitest::Reporters::JUnitReporter.new, Minitest::Reporters::SpecReporter.new(:color => true)]

class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

 ....


end
1
The title is a question but then you say you are puzzled by some other questions. What exactly is your question? If answers require opinions, your question will not be well-received, as those are no-no's on SO.Cary Swoveland
My question is more of related to MiniTest testing framework as I am not sure what all it offers. The questions in the title is a more generic one whereas the one in the post are more descriptive.Nital
MiniTest is just one of the testing frameworks you can use with Rails. There's a number of alternatives that can also do the job, it's just a matter of preference. There's nothing magical about the tests themselves, they just execute code, but each framework has a sort of philosophy about how to do the testing. This question is way, way too broad. Have you read the Rails testing guide?tadman
@tadman - I have been going through the Rails guides but haven't yet reached the Testing section yet. Will definitely look into it. Thanks for the pointer.Nital
Please stop using `backticks` to highlight words. They are for inline code only, not for adding emphasis.meagar

1 Answers

2
votes
  1. Is 'MiniTest` used for just unit testing or integration testing or both?

MiniTest is a generic framework for testing just like JUnit or PHPUnit or whatever. Its pretty sparse straight out of the box but can be extended for different tasks. Its a wrench, and you can use wrenches to build rocket ships or just toy cars.

  1. Is this test actually a MiniTest test or is it just importing existing default Ruby test framework in the helper file and not actually using any of the MiniTest features?

The Rails tests are all built on top of ActiveSupport::TestCase. ActiveSupport::TestCase is built to run on top of MiniTest or the older Test::Unit which provides the runner and assertions.

  1. Can MiniTest actually test DOM elements such as input text in user input boxes, do button clicks etc?

Yes and no. MiniTest itself does not include a document parser or a DOM simulator. If you add those - yes.

  1. Is it actually hitting the application by starting the server internally?

Yes. Integration tests actually use HTTP to perform requests on your app.

  1. Can MiniTest alone be used to test a Rails application end-to end by running in headless mode?

No, see above.

  1. Can MiniTest alone be used to test a Rails application end-to end by launching a browser through selenium? I know Capybara can do this.

No, see above.

  1. I guess this test is running against localhost. How can I make this test point to a different host such www.dev-server.com?

Is it possible; yes. Good idea? Not so much.

The reason you don't want to do this is:

  • Its sloooow.
  • You can't properly reset the state between tests without some very indecent exposure of your dev server.

This is basically what you are forced to do when doing javascript tests against a server out of your control. And that scenario is horrible.

What you might be looking for is to setup a CI environment such as Travis where you can run the tests.