Book Image

Instant RubyMine Assimilation

By : David L. Jones
Book Image

Instant RubyMine Assimilation

By: David L. Jones

Overview of this book

Ruby and Ruby on Rails applications can become very complex very quickly, with hundreds of different files to contend with during a typical day of development. With RubyMine, you can become much more productive and let the IDE take care of the menial tasks while you concentrate on the details and structure of your applications. Instant RubyMine Assimilation shows you how to integrate the RubyMine development environment into your everyday development process. With step-by-step instructions and practical examples, you will learn how to customize the environment precisely to suit your needs, debug and test your code, and get the most out of many other features of RubyMine. Starting with the installation of RubyMine on your preferred platform, this guide will walk you through creating your first program. You will be taken through everything from the development and testing process all the way to deploying your website for the world to enjoy. You will learn how to manage your project in RubyMine including integrating version control, testing, and debugging your projects. Furthermore, you will learn how to navigate complex projects, view database tables, and utilize the built-in console and deployment tools of RubyMine, all of which will help you become an expert developer This book will also teach you how to install and use Gems, change Ruby environments, and use code coverage and reports to enhance your testing. By the end of this book, you will be able to confidently deploy your Rails application to a server, all within the inviting environment that is RubyMine.
Table of Contents (7 chapters)

Testing your tech (Become an expert)


Testing your application is an important and often overlooked aspect of writing good code. This recipe will show how to write some tests for your application using the minitest-reporters Gem and run the tests interactively. This will give you an overview of using tests to make your application much more robust. In addition, we will explore the use of fixtures to create some reliable test data as input for your tests.

Getting ready

Open your Progeny Rails project in RubyMine.

How to do it...

In order to use the built-in testing tools inside of RubyMine, there is one Gem that we need to install first, which can be done using the following steps:

  1. Add the following line to your Gemfile:

    gem 'minitest-reporters'
  2. Navigate to Tools | Bundler | Install and the Gem will be installed and added to your project.

Now, we need to decide what our test will be, which can be done using the following steps. When we ran the Rails generators to create our first Species table in the recipe, Creating your first progeny (Should know), Rails also created some pre-made testing files for us automatically. These are all stored in the folder called Test.

  1. Expand this folder in the Project window and you will find a file called test_helper.rb.

  2. Open this file and add the following lines to the top of the file:

    require 'minitest/reporters'
    MiniTest::Reporters.use!
  3. Add the previous lines right below the third line, which should be something like this:

    require 'rails/test_help'
  4. Save the file and then expand the Models folder.

  5. Open the file contained inside called species_test.rb, which was automatically generated from the scaffold generators for us.

  6. Replace the following three commented-out lines:

      # test "the truth" do
      #   assert true
      # end

    Replace the previous code with the following testing code:

      test 'Full Species Assimilation' do
        @species = species(:not_assimilated)
        assert_equal @species.full_species, "Human 1 has not been assimilated.", 'Full Species Not Assimilated incorrect'
    
        @species = species(:assimilated)
        assert_equal @species.full_species, "Human 1 has been assimilated.", 'Full Species Assimilated incorrect'
      end

One more thing we need that these tests rely on is a file called fixture. Fixtures are a way of defining the data that is necessary for a particular database record that can be loaded before each test to populate databases. This is shown in the following steps:

  1. Expand the folder inside the test folder called fixtures.

  2. Open the file contained within called species.yml.

  3. Replace the text in this file with the following code:

    assimilated:
      name: Human
      identification: 1
      assimilated: true
      notes: Only partially assimilated
    
    not_assimilated:
      name: El-Aurian
      identification: 1
      assimilated: false
      notes: Guinan's Species

Now, in order to run the tests, we need to run another rake task specially designed for these types of tests. Perform the following steps to do so:

  1. Right-click (ctrl + click on Mac) on the word Species in the following code line in the species_test.rb file in RubyMine.

    class SpeciesTest < ActiveSupport::TestCase
  2. This will bring up a menu that will let you run the tests for this module, as shown in the following screenshot:

  3. If everything has gone correctly, you should see tests running in the bottom panels, as shown in the following screenshot:

The undefined method error is expected since we have not yet written the code for that. Perform the following steps for writing this code:

  1. Open the file from the app folder, the models folder, and the file called species.rb. This looks like a good place to add our model-level method called full_species. That method is written for you in the following step.

  2. Copy and paste the following code into this file, replacing the code that is there:

    class Species < ActiveRecord::Base
      def full_species
        "#{name} #{identification} has #{(assimilated? ? '' : 'not')} been assimilated."
      end
    end

    This is a simple method designed to merely combine some of the database fields into a common sentence that might be used in several Rails views across the application.

  3. Now that we have a method in place, lets re-run the tests as before. This time we can use the configuration pull-down menu at the top of the IDE and just hit the green arrow next to the menu to re-run the same tests. Unfortunately, we still have an error in our code. You will see a long red bar above the testing windows as well as an error message like the following:

    MiniTest::Assertion: Full Species Assimilated incorrect.

    --- expected

    +++ actual

    @@ -1 +1 @@

    -"Human 1 has been assimilated."

    +"Human 1 has been assimilated."

We can tell by the last two lines that we have an extra space where not might be shown depending on assimilation. Let's fix that now using the following steps:

  1. Replace the line in the species.rb model file:

        "#{name} #{identification} has #{(assimilated? ? '' : 'not')} been assimilated."
    

    with this:

        "#{name} #{identification} has#{(assimilated? ? ' ' : ' not ')}been assimilated."
  2. Re-run the tests and we should now see a green bar and all tests passing successfully.

How it works…

These tests are assuming that we have a Species method in Model called full_species. This method was written after the test was written This is called Test Driven Development (TDD) where the tests are written first and the methods to satisfy the tests come later.

This allows the developer to think about how someone would use a method in the rest of the code, which is basically writing the interface to the method first.

The testing program that we wrote is a simple assertion type test that compares two strings to see if they are equal. We defined the method to return a string, so we hard code a string that we expect and compare it with the returned string from our full_species method.

The line in the test @species = species(:not_assimilated) reads the species.yml file from the fixtures folder and creates a temporary species database model with the attributes from the .yaml file. This lets us test the various attributes of our model with a standard set of testing data that does not change from test to test.

There's more…

There is a button on the left-hand side of the testing panels called Toggle Auto-test. If you select this, then the tests will be executed continuously every few seconds so that you can continue to write tests and methods and check your status constantly.