Skip to content

Testing

Isabel Hernandez edited this page May 31, 2022 · 13 revisions

Testing Code w/us Documentation

Running Tests

How to run all tests

rails test

How to run tests in a specific file (ex. want to test a specific controller)

rails test <filepath>

How to run a test in a specific file

rails test <filepath>:<linenumberoftest>

How to run all system tests

rails test:system

How to run a systems test

rails g system_test <filepath>

Writing Tests

How to Decide Which Test to Write

What is a unit test?

A unit test is to ensure the code does what it is intended to do. Unit tests are about isolating a single component/function and making sure that it behaves the way it should. (used for testing controllers and API endpoints)

What is a system test?

System tests allow test user interactions with your application, running tests in either a real or a headless browser. System tests use Capybara. Capybara is a DSL that provides stimulated web browser for use with your tests as a companion gem. The purpose of these tests is about functionality and the physical view of what the user will be experiencing.

What is a model’s test?

Tests that models validate as they should and that all methods work.

How to Generate Tests

How to generate a unit test

Unit Tests are automatically generated when controllers are generated with the file format /test/fixtures/_test.rb

Example of a simple unit test

test "should get index" do
    get controller_url
		# making sure the request was successful and also ensuring that the right response body has been generated
    assert_response :success
 end

Asserts that the response is one of the following types:

  • :success - Status code was 200
  • :redirect - Status code was in the 300-399 range
  • :missing - Status code was 404
  • :error - Status code was in the 500-599 range

How to generate a system test

rails generate system_test <testName>

which will create a skeleton file in your fixtures folder called _test.r

Example of a simple system test

test "visiting a url" do
		#visit the url
    visit entries_url
		#look for a h1 with the text "Code w/Us"
    assert_selector "h1", text: "Code w/Us"
end

More details and Examples in References

Fixtures - /test/fixtures

Fixture Files - Fixture files in Ruby serve as a dummy database. In cases where tests use queries, it will pull from or manipulate the data defined by .yml files in the /test/fixtures folder

Example:

As defined in the database schema: https://github.com/kbuffardi/CodeWitUs/blob/primary/db/schema.rb

the videos table contains these attributes

create_table "videos", force: :cascade do |t|
    t.string "user"
    t.string "video_id"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "vtitle"
    t.string "concepts"
    t.string "interests"
    t.string "cwlink"
  end

Therefore, in our /test/fixtures/videos.yml file we will define dummy data

one:
  user: 1
  video_id: "BVK6rCz_7SQ"
  vtitle: "Variables in C++ for Soccer Players"
  concepts: 1
  interests: 11
  cwlink: "https://codeworkout.cs.vt.edu/gym/workouts/955/practice"
	#created_at and updated_at are automatically populated/updated
	#when you create or update a model instance

two:
  user: 1
  video_id: "APtxCYdcNcQ"
  vtitle: "Variables in C++ for Constellations"
  concepts: 1
  interests: 1
  cwlink: "https://codeworkout.cs.vt.edu/gym/workouts/955/practice"
  #created_at and updated_at are automatically populated/updated
	#when you create or update a model instance

Now, we could theoretically create a test that can query or manipulate these dummy records using any CRUD (Create, Read, Update, and Delete) methods

Resources for Learning to Write Tests

  1. Ruby on Rails Testing Documentation - https://guides.rubyonrails.org/v5.1/testing.html
  2. Using Capybara with Rspec - https://github.com/teamcapybara/capybara#using-capybara-with-rspec
  3. Ruby on Rails Testing with Rspec (example for model tests) - https://medium.com/nerd-for-tech/ruby-on-rails-testing-with-rspec-writing-your-first-tests-6330920928fd

Issues Encountered

Since the docker container is dynamic, after rebuilding a new container, running tests in interactive mode will sometimes not recognize new changes added within that instance the first time.

In this case, you must exit interactive mode (ensure the container is still running) and run tests from the command line using this command: docker-compose exec web rails test

After this, you should see your test changes applied and can now run commands regularly in interactive mode

Remember to push your changes to git when working in interactive mode because if your container is deleted you will lose your changes.

Clone this wiki locally